Reputation: 2255
I want to rename files based on the previous 2 directory names. For example, the file below:
C:\temp\288\Issue level 1\288 Temper inad.doc
I want it to be renamed
288-Issue level 1-288 Temper inad.doc
I have created the following powershell command :
Get-ChildItem "C:\temp\288\Issue level 1" -Filter *.doc -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}
However it only renames the the file with Issue level 1, I want it to include the 288 folder at the beginning too.
Upvotes: 1
Views: 91
Reputation: 58931
Just use the Parent
property of the Directory
to get the previous directory name. Also I would use a format string:
Get-ChildItem "C:\temp\288\Issue level 1" -Filter *.doc -Recurse |
Rename-Item -NewName { '{0}-{1}-{2}' -f $_.Directory.Parent.BaseName, $_.Directory.BaseName, $_.Name }
Upvotes: 2