如何在批处理文件中使用PowerShell,而不是脚本,以重命名文件夹中的多个文件&所有子文件夹?

时间:2018-01-27 23:47:22

标签: powershell batch-processing

这个批处理文件* .bat,而不是脚本〜就是这样,只是针对批处理文件所在的文件夹,但是它下面的所有子文件夹呢?

这就是我的尝试:

powershell -C "gci | ForEach-Object % {rni $_.Name ($_.Name -replace '15June', '01July ')}"

1 个答案:

答案 0 :(得分:1)

Rename-Item接受管道输入,因此您不需要ForEach-Object。要包含子文件夹,请将参数-Recurse添加到Get-ChildItem

Get-ChildItem -Recurse | Rename-Item -NewName {$_.Name -replace '15June', '01July'}

从批处理文件中,您应该能够运行如下语句:

powershell.exe -Command "Get-ChildItem -Recurse | Rename-Item -NewName {$_.Name -replace '15June', '01July'}"

或(如果您想使用别名,不建议用于编写脚本):

powershell.exe -c "ls -r | ren -n {$_.Name -replace '15June', '01July'}"