3

I need to replace multiple strings in multiple filenames. Here's something representative of what my data looks like.

CAL aB12 AUG.docx
CAL cDe345 AUG.docx
CAL FGHiJKL6789 AUG.docx

I need to replace "CAL" with "Calendar" and "AUG" with "August" in the filenames.

The best I've been able to do is run two cmdlets (one for each replacement) chained together with a semicolon. This works, but I know it's crude.

gci | Rename-Item -NewName { $_ -replace "CAL", "Calendar" };
gci | Rename-Item -NewName { $_ -replace "AUG", "August" }

After extensive searching, I found StackOverflow 3403217. Due to a lack of knowledge and experience I haven't been able (and I've tried hard) to translate any of the five answers it provides into a single cmdlet that works for what it is that I'm trying to do.

p.s. I paste copies of the files of interest into C:\Temp and work from there. I'm using Windows 7.

Community
  • 1
  • 1
pwt12855
  • 41
  • 3

3 Answers3

1

try Something like this

$hashreplace = @{}
$hashreplace.'old1' = 'new1'
$hashreplace.'old2' = 'new2'

Get-ChildItem -Recurse -File -Path "c:\temp\" | % {
    $filename = $_.FullName
    $text = $_.Name
    $hashreplace.Keys | %{if ($text -ne $null) {$text = $text.Replace($_, $hashreplace[$_]) } }
    Rename-Item -Path $filename -NewName $text
}
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • Thank you, Esperento57. I appreciate your help. While I'm sure your solution is on the money, it is way beyond my current grasp. Show below is the solution that I can get my arms around. Get-Childitem | Rename-Item -NewName { ($_.name -replace "cal", "calendar") -replace "aug", "August" } – pwt12855 Jan 03 '17 at 07:27
1

All you need to do is daisy-chain the replacement operations:

gci | Rename-Item -NewName { $_.Name -replace "CAL","Calendar" -replace "AUG","August" }

As @Esperento57 mentioned in the comments the -replace operator does regular expression replacements. In your case it doesn't make a difference, but if your search strings contain special characters they may require escaping, e.g.:

gci | Rename-Item -NewName { $_.Name -replace [regex]::Escape("CAL"),"Calendar" -replace [regex]::Escape("AUG"),"August" }

Or you could do simple string replacements by daisy-chaining Replace() method calls:

gci | Rename-Item -NewName { $_.Name.Replace("CAL","Calendar").Replace("AUG","August") }
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
-1
Get-Content List.txt |

  Where-Object \\{ $_ -match '^\\\\' \\} |

  ForEach-Object \\{ $_ -replace

  '^\\\\(\S+).+','$1' \\}

You basically need to grab the text file and find matches for whatever String you want and run through a for loop in order to replace it. Why do it in Powershell? I don't know.

  • 1
    Why do it in Powershell? I originally tried doing it in a Command Prompt (cmd) session AND IT BOTCHED UP THE FILENAMES as shown below. What should I have done differently in my Command Prompt session? C:\TEMP>dir CAL ab123 AUG.doc CAL cde45 AUG.doc CAL fghijk6889 AUG.doc C:\TEMP>ren "CAL*Aug.doc" "calendar*August.doc" C:\TEMP>dir calendar3 August.doc calendar5 August.doc calendarjk6889 August.doc – pwt12855 Jan 03 '17 at 06:33
  • Thank you. I went with Powershell because what I want to do cannot be done in a Command Prompt session. Using: (ren "CAL*Aug.docx" "calendar*August.docx" botches up the filenames. Many people have suggested a Command Prompt session to me but when they give it a shot they change their minds. – pwt12855 Jan 03 '17 at 06:43
  • There were wildcard asterisks in my replies before I submitted them. – pwt12855 Jan 03 '17 at 06:47
  • @pwt12855 Haha, it was just a joke don't worry. I'd rather just do a quick C program and use it like that, but then again, it depends on the situation. Does my answer suffice? – Imagine Dragons Jan 03 '17 at 06:50
  • It just goes to show how humor-less I've become in bang-head-here mode with this. Tonight, I received your solution and another one. one from Get-Childitem | Rename-Item -NewName { ($_.name -replace "cal", "calendar") -replace "aug", "August" } – pwt12855 Jan 03 '17 at 07:09
  • As you can probably guess, the one that looks familiar is going to work better for me with as much as I currently know. I do thank you for your help. I am saving your solution for the day when I'm higher up on the Powershell learning curve and can put it into action. – pwt12855 Jan 03 '17 at 07:20
  • @pwt12855 No worries! Good luck with your project :) – Imagine Dragons Jan 03 '17 at 07:29
  • Now I see why I couldn't get solution 3 (nested replaces) in Stack Overflow 3403217 to work. Get-Childitem | Rename-Item -NewName { ($_.name -replace "cal", "calendar") -replace "aug", "August" } – pwt12855 Jan 03 '17 at 07:34