我正在开发一个PowerShell批处理文件。
我想重命名一定数量的文件夹中的所有文件。我需要将其重命名为文件夹名称的某个部分,后跟增量编号。到目前为止,我设法在powershell中创建了一个rename命令,它还为文件添加了数字。
Get-ChildItem -recurse -filter "*.jpg" | %{$x=1} {Rename-Item $_.FullName -NewName ('{0}-{1}.jpg' -f ($_.FullName.substring(18,8) -replace("-","")) ,$x++)}
这很好用,我想为每个单独的文件夹将数字重置为1。目前我一直在通过不同的文件夹编号。当我更换文件夹时如何将$ x重置为1?
答案 0 :(得分:2)
由于您无法确定每个目录是否枚举,我会创建一个哈希表来跟踪索引。类似的东西:
$Directories = @{}
Get-ChildItem -recurse -filter "*.jpg" | ForEach {
Rename-Item $_.FullName -NewName ('{0}-{1}.jpg' -f ($_.FullName.substring(18,8) -replace("-","")) ,++$Directories[$_.DirectoryName])
}