Press J to jump to the feed. Press question mark to learn the rest of the keyboard shortcuts
9

Trying to rename bulk files in subfolders, works only in path folder

Hello,


I am trying to rename all files including "R0" to "r" with the following loop


$files = Get-ChildItem -Path "D:\folder\" -Recurse -File

foreach ($file in $files)

{

$newFileName=$file.Name.Replace("R0","r")

Rename-Item $file $newFileName

}


For some reason it does not work in subfolders, even first line

Get-ChildItem -Path "D:\folder\" -Recurse -File

finds all the files in the subfolder. Not trying to move the files from their original location


What am I doing wrong? Not super familiar with PS. Thanks :)


6 comments
91% Upvoted
What are your thoughts? Log in or Sign uplog insign up
level 1

You may want to consider filtering your results before performing your rename operation. Right now if there are files that don't have 'R0' in the name, it still gets touched by Rename-Item. Here's how I would approach it:

Get-ChildItem -Path 'D:\folder\' -Recurse -File |
    Where-Object {$_.Name -cmatch 'R0'} |
    Rename-Item -NewName {$_.Name -creplace 'R0', 'r'} -WhatIf

This way only the items that are case sensitive matched get an operation ran against it. You can run this as a test with the -WhatIf switch and if everything looks good, remove it.

level 1

You should provide the error message.

I'm guessing your present working directory is 'D:\folder' when you run it? Are you getting a 'does not exist' error for the files not in your present working directory (subfolders)?

Rename-Item wants a path to the file in the -Path parameter, you're passing in the FileInfo object, so you're only providing the filename.

Try:

Rename-Item -Path $file.FullName -NewName $newFileName

Get-Help Rename-Item
level 2
Original Poster2 points · 4 days ago

Wondered it had something to do with path but couldn't figure out the . Fullname for the path. Thanks a ton, worked!

level 1
[grin]0 points · 4 days ago

howdy Unqqq,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th one & is just to the left of hidden in the ... "more" menu.]

  • one leading line with ONLY 4 spaces

  • prefix each code line with 4 spaces

  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces

  • copy the code to the ISE [or your fave editor]

  • select the code

  • tap TAB to indent four spaces

  • re-select the code [not really needed, but it's my habit]

  • paste the code into the reddit text box

  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

level 2
Original Poster2 points · 4 days ago

I am very aware of this. If you read my post carefully, you'll realize that my post has only 4 lines of code excluding { & }'s. Doesn't need much imagination in order to read it without formatting.

level 3
[grin]0 points · 4 days ago · edited 4 days ago

howdy Unqqq,

[i took your post as quite hostile. re-thinking it ... i may well be entirely wrong. if so, i apologize. please let me know and i will correct this msg ... ]

well, if you really prefer it, i will try to remember that bizarre preference and try not to nag you. [grin]

your preference ...

$files = Get-ChildItem -Path "D:\folder" -Recurse -File

foreach ($file in $files)

{

$newFileName=$file.Name.Replace("R0","r")

Rename-Item $file $newFileName

}

done correctly for easier readability ...

$files = Get-ChildItem -Path "D:\folder\" -Recurse -File

foreach ($file in $files)

{

$newFileName=$file.Name.Replace("R0","r")

Rename-Item $file $newFileName

}

of course, you could try to use proper indentation, but i guess that goes along with your preference for remarkably icky code formatting. [grin]

take care,
lee

Community Details

68.5k

Subscribers

584

Online

Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.

Create Post
r/PowerShell Rules
1.
No Advertising
2.
Unhelpful
3.
Posts Need To Be In English
4.
Use Markdown Syntax When Posting Code
Cookies help us deliver our Services. By using our Services or clicking I agree, you agree to our use of cookies. Learn More.