#let us first define the destination path
$path = "R:\WorkindDirectory"
#name of the file
$file = "machine.orgin"
#let us list the number of the files which are similar to the $file that you are trying to create
$list = Get-ChildItem $path | select name | where name -match $file
#let us count the number of files which match the name $file
$a = ($list.name).count
#if such count of the files matching $file is less than 0 (which means such file dont exist)
if ($a -lt 1)
{
New-Item -Path $path -ItemType file -Name machine.orgin
}
#if such count of the files matching $file is greater than 0 (which means such/similar file exists)
if ($a -gt 0)
{
$file = $file+$a
New-Item -Path $path -ItemType file -Name $file
}
注意:這項工作假定文件的名稱是串聯的。讓我們說使用這個腳本創建 machine.orgin
machine.orgin1
machine.orgin2
machine.orgin3
,之後再刪除machine.orgin2並重新運行相同的腳本,然後它不會工作。 在這裏,我給在那裏我曾試圖創建一個新的文件的例子,你可以安全地修改同使用copy-item
代替new-item
嗯,這可能不是一個壞主意,我會試試看。我只提到了增量數字(並且謝謝你說增量數字,我想不起這個詞),因爲它很快就可以了。我會試試這個,讓你知道。 –
非常感謝你吉恩,這正是我所需要的,它的工作非常好。這對我來說已經解決了。 –
我還會補充一點,我覺得這個解決方案更舒服,因爲移動部件更少,再次感謝 –