#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
嗯,这可能不是一个坏主意,我会试试看。我只提到了增量数字(并且谢谢你说增量数字,我想不起这个词),因为它很快就可以了。我会试试这个,让你知道。 –
非常感谢你吉恩,这正是我所需要的,它的工作非常好。这对我来说已经解决了。 –
我还会补充一点,我觉得这个解决方案更舒服,因为移动部件更少,再次感谢 –