[英]batch renaming files using powershell
I am able to batch rename files in a working directory by using:我可以使用以下方法批量重命名工作目录中的文件:
Dir | %{Rename-Item $_ -NewName ("0{0}.wav" -f $nr++)}
However I want the file rename to start at something other than zero.但是我希望文件重命名从零开始。 Say 0500, and rename sequentially in order.说0500,依次重命名。
Dir | %{Rename-Item $_ -NewName ("0{500}.wav" -f $nr++)}
returns error.返回错误。
How can I tell rename to start at a number other than 0?如何告诉重命名从 0 以外的数字开始?
You can initialize the counter beforehand to 500. Also, you don't need to use a ForEach-Object loop ( %
) for this, because the NewName
parameter can take a scriptblock.您可以预先将计数器初始化为 500。此外,您不需要为此使用 ForEach-Object 循环 ( %
),因为NewName
参数可以采用脚本块。
Important here is that you need to put the Get-ChildItem
part in between brackets to let that finish before renaming the items, otherwise you may end up trying to rename files that have already been renamed.这里重要的是,您需要将Get-ChildItem
部分放在括号之间,以便在重命名项目之前完成,否则您最终可能会尝试重命名已重命名的文件。
$nr = 500
(Get-ChildItem -Path 'D:\Test' -Filter '*.wav' -File) | Rename-Item -NewName { '{0:D4}{1}' -f ($script:nr++), $_.Extension }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.