如何在cmd / powershell中使用增量文件名创建批量空文件?

问题描述 投票:0回答:1

如果我要创建10个空的.txt文件,其名称模式为abcd01.txtabcd02.txt,...,abcd10.txt。如何在CMD / PowerShell中实现?

这是我要实现的目标:

for($i = 0; $i -lt 11; $i++){ New-Item -Path "C:\Users\FOO\Desktop\FOOBAR\abcd",$i,".txt" -ItemType File }

此创建的文件没有任何文件扩展名。 012,...,10

然后我将所有文件重命名为:

 Dir -filter * | %{Rename-Item $_ -NewName ("abcd{0}.cpp" -f $nr++)}

是否有单行命令将'n'空文件扩展为'.xyz'

powershell batch-file cmd batch-processing batch-rename
1个回答
0
投票

您可以在new-item中使用的变量中设置前导零的格式:

for($i = 0; $i -lt 11; $i++){
    $path = "C:\temp\test{0:00}.txt" -f $i
    New-Item -Path $path
}
© www.soinside.com 2019 - 2024. All rights reserved.