TevinTwoTimes
TevinTwoTimes

Reputation: 103

Batch Replace Character in File Names

I have about 600 video files with the $ special character in the title.

I want to batch replace this with a letter s.

I found the powershell code online below and it works fine with replacing letters with other letters but fails when trying to replace the $ special character

get-childitem -recurse | rename-item -newname { $_.name -replace "",""}

I tried using the code below and it ends up adding an s to the end of the file type instead of replacing the $

get-childitem -recurse | rename-item -newname { $_.name -replace "$","s"}

$hortvid.mp4 becomes $hortvid.mp4s instead of shortvid.mp4

Any ideas on how to get this to work correctly?

Upvotes: 2

Views: 4159

Answers (2)

Esperento57
Esperento57

Reputation: 17472

$ is used for specify variable in powershell. And a string with double quote is evaluate in powershell like this :

$variable1="Hello"
$variable2="$variable1 world"
$variable2

if you dont want evaluate a character into a double quote string, you can backslash you caractere like the proposed solution of @lww. Or simply, you can use simple quote. Like this :

Get-ChildItem -recurse | Rename-Item -Newname { $_.Name -replace '$', 's'}

Upvotes: 1

lww
lww

Reputation: 661

Just use \ escape character:

When running line in the directory:

get-childitem -recurse | rename-item -newname { $_.name -replace "\$","s"}

input file:

$hortvid.mp4

output file is renamed:

shortvid.mp4

Upvotes: 2

Related Questions