1

My task is to replace some characters in file names in about 10000 files I have including sub folders.

So in my example I want to change the character # to hash.

My file location is "C:\Users\JHB Laptop\Documents" with the main file called test "C:\Users\JHB Laptop\Documents\test".

Sub folders are "C:\Users\JHB Laptop\Documents\test\New folder1\New folder2\New folder3\New folder4\New folder5"

I adapted this code from user:331812 Malcolm

{cd "C:\Users\MyName\Documents\MyDirectory"
# by default, -replace is case-insensitive (equivalent to -ireplace)
# for case-sensitive, use -creplace
Dir | Rename-Item –NewName { $_.name –replace "_"," " }}

and this is my amendment:

cd "C:\Users\JHB Laptop\Documents\test"

by default, -replace is case-insensitive (equivalent to -ireplace)

for case-sensitive, use -creplace

Dir | Rename-Item –NewName { $_.name –replace "#","(hash)" }

This part works well but of course stops at the last folder in the string

And this is where my knowledge stops. I see that -recurse plays a part in sub folders, but I cannot work out where it fits in project.

DavidPostill
  • 162,382
lwaxg10
  • 13

1 Answers1

1

Try: dir -Recurse | Rename-Item –NewName { $_.name –replace "#","(hash)" }

dir is the old command prompt method. That technically just runs Get-ChildItem, as does ls. Those are called aliases (Get-Alias). Use help dir to get help with a command, it explains -Recurse. I like to do help dir -sh which will open help in a seperate window with search so you can reference it. -sh is short for -ShowWindow, since there are no other parameters/switches starting with sh powershell assumes you meant ShowWindow

gregg
  • 6,307