2

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?

TevinTwoTimes
  • 103
  • 1
  • 10
  • For efficiency, I'f strongly advise that you incorporate a filter to ensure that you only pass the files which contain that character in the first instance. e.g. `gci -Filt *$* -File -Rec | rni …` – Compo Dec 21 '20 at 11:52

2 Answers2

2

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
lww
  • 624
  • 1
  • 7
  • 14
1

$ 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'}
Esperento57
  • 16,521
  • 3
  • 39
  • 45