Search This Blog

2016-12-15

PowerShell: Rename File but Keep Extension Name

Rename all the files in current directory by removing the whitespace character from the base filename
Dir | where { ! $_.PSIsContainer } | Rename-Item –NewName { $_.Basename.TrimEnd() + $_.extension } -verbose

Same as above, but includes all the subdirectories
Dir -recurse | where { ! $_.PSIsContainer } | Rename-Item –NewName { $_.Basename.TrimEnd() + $_.extension } -verbose

Rename all the files in current directory by appending "_old" to the end of the base filename
Dir -recurse | where { ! $_.PSIsContainer } | Rename-Item –NewName { $_.name + "_old" + $_.extension } -verbose

Rename all the files including subdirectories by inserting "old_" to the beginning of the base filename
Dir -recurse | where { ! $_.PSIsContainer } | Rename-Item –NewName { "old_" + $_.name + $_.extension } -verbose

Rename all the files including subdirectories by replacing "new" with "old" in the base filename
Dir -recurse | where { ! $_.PSIsContainer } | Rename-Item –NewName { $_.basename.replace("new","old") + $_.extension } -verbose

Rename all the *.trp files including subdirectories by removing the "00" at end of filename, i.e. replacing "00.trp" with ".trp" in the base filename
Dir -recurse -filter *.trp | Rename-Item –NewName { $_.name –replace “00.trp“,”.trp” } -verbose

Rename all the MP4 files by first 8 characters of the base filename
Dir *.mp4 | Rename-Item –NewName { $_.basename.substring(0,7) + $_.extension } -verbose

Rename all the files in current directory with substring starting location 9, plus 15 characters of the base filename
Dir -recurse | where { ! $_.PSIsContainer } | Rename-Item –NewName { $_.name.SubString(8,15) + $_.extension } -verbose

 

No comments: