Automatically batch renaming files according to a formula including parent folder name using Powershell
问题 How can I batch rename files in powershell using the following code: $nr=1;Get-ChildItem -Filter *.jpg | Rename-Item -Newname {"PPPPPPP_{0:d3}.jpg" -f $global:nr++} where PPPPPPP is the name of parent folder containing these files. Expected Output : PPPPPPP_001.jpg PPPPPPP_002.jpg PPPPPPP_003.jpg Files are located in C:\USER\MAIN\BLABLABLA\PPPPPPP folder. 回答1: Get the parent directory's name via $_.Directory.Name inside the script block. Cast the $nr variable to [ref] so that you can modify