Renaming subfolders within subfolders

Hello,

I know there are multiple topics about this on the Internet dot com, but I’m struggling hard figuring this out. I’m trying to rename a bunch of subfolders that are within subfolders. Basically, I have a folder called “clients”, and within that folder is a client folder for each client (there are like 3000 of them), and within each of those folders is a folder called “Historical Folder” that I need to rename “Historical Reporting.” I’m pretty sure this is doable with PowerShell, but I’ve personally never used it and have just been reading examples online and get myself lost. Any help on how to structure my code for this would be appreciated!

Rachel,
Welcome to the forum. :wave:t4:

There’s no strucure needed. Simply list the folders you want to rename with Get-ChildItem -Path '.\Client\*\*\Historical*' and rename all found folders wtih Rename-Item. :wink:

Thanks for the response! So I’m probably overthinking this or already just in weekend mode. I have the PowerShell window open and the top line is the path where the first batch of subfolders are. So basically it’s like Client Folder > 3000 client name subfolders > the 3000 subfolders I want to rename. The top line in the PowerShell window is the Client Folder. Do I have to use the above for each of the 3000 client name folders?

Sorry I’m so clueless on this. I know there’s a way this should work, but PowerShell is so new to me. Trying to expand my horizons a bit! :smiling_face:

Please keep in mind it is beyond the scope a forum to teach you a complex technology like scripting with PowerShell. You will have to learn the very basics of PowerShell by yourself first. This will save you from a lot of wasted time and frustration.

Regardless of that - I urgently recommend to use test data at first if you’re not comfortable with using PowerShell or scripting in general.
Next - you may use an IDE like PowerShell_ISE or VSCode instead of the pure console. There you will get some help like syntax highlighting and hints for error and bad styles. It will make it easier for you - especially in the beginning.

2 Likes

Hi, welcome to the forum :wave:

If you’re open to a non-PowerShell solution, then have a look at PowerRename, which is part of the PowerToys suite of utilities. It’s as simple as right-clicking the Clients folder in File Explorer, then typing the old and new names.

2 Likes

Completely understand! I can usually figure out these things just by reading examples. It’s how I learned VBA and SQL! I appreciate the help you’ve provided thusfar.

Anyway, I was able to get that script to list all of the actual client folders, but not the subfolders. You indicated using a * in the path above to get the subfolder names. Is that to work as a wildcard? Also, I see you didn’t end the path with a . I know in some cases that’s needed. Wondering if that may be why I’m not getting the subfolders I need?

I don’t know how you learned VBA and SQL but in my experience the vast majority of people learn coding with a lot of trial and error.

Why don’t you try it?

If you’re talking about a period " . " … that’s not necessary. Where do you need that?

As long as you use cmdlets with the verb Get like Get-ChildItem or Get-Process or Get-Content and so on, you cannot break anything because you only retrieve information - you’re not changing any. So I’d like to encourage you to try more. :wink: :+1:t4:

In generel if you have a piece of code you have an issue with it’s the best idea to share this code snippet along with an explanation what’s not working as expected or along with an error message you might get. That’s the fastest way of getting help in a forum.

2 Likes

So I did try the bit with the * thinking it was a wildcard, but it didn’t do anything which is why i asked here. Should have mentioned that!
I’m in this forum on my personal computer while trying to do renaming on my work computer. They have certain things blocked so this is my only option unfortunately, otherwise I would totally provide screenshots, code, etc. Not ideal. I’ll keep playing around with what you’ve provided me knowing I can’t break anything since I’m just retreiving information. Thanks again for getting me this far! :blush:

Yes. :wink:

Assuming your personal computer is a Windows machine you may start playing around with this:

Get-ChildItem -Path 'C:\Windows\System32\*PowerShell\*\Modules\*' -Directory

This will list all subfolders of the Modules folder of your Windows PowerShell installation. As you can see you can use wildcards for complete directory names or for parts of it.

1 Like

You’ve been most helpful. So far I’ve found two ways to pull the names of the client folders, one that runs fast and one that runs slow. :laughing: I’ve gotta be close to getting what I need.

Thanks the above to play around with on my personal computer. I’m sure I’m close to this now!

Now you make me curious … would you like to show the two versions? Maybe I can learn something new as well … :wink:

Of course I can’t replicate it now. I think it had to do with using a wildcard vs not using a wildcard.
Stupid question though - if the cursor is blinking after I enter the code and hit enter, is it thinking or waiting on me?

:sob:

When you’re talking about working in the console - It is thinking working or crashing. :wink: When it’s waiting for you the prompt will change to a “>>”.

Sounds like you’ve clicked the window and so it is waiting on you. Click the properties of the console and disable quick edit to prevent this

1 Like

Sorry. Popping this one back up. I’ve finally got this figured out mostly. I’m at the point where I’m going to rename the folder.

I have it written like this: Get-ChildItem '\pathway' | Rename-Item -Historical Reporting

I know the pathway works because I ran that bit on its own - it brings up the files I want to rename. In the PowerShell window, Historical is in gray text but Reporting is in white which makes me think that space is doing something. Do I need to use a dash insetad of a space or some other character to get the space in there?

Thanks!!

Get-ChildItem C:\1\clients* -Recurse|Where-Object {$_.name -match “history”}| rename-item -NewName ‘history_report’

I’m unsure what your question is about … spaces separate parameters from values and from each other. If you have spaces in one value or dashes you have to enclose those values in quotes. I’d recommend to always enclose string each string value in quotes and besides this always use named parameters instead of positional ones. It will make your much code easier to read and look more professional.

Regardless of that - when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: