0

Given a folder structure:

---- folder1#name
--------- folder2#name
---- folder3#name
--------- folder4#name
------------ folder5#name
------------ folder6#name
------------ folder7#name

I want to recurisvely go through them and just rename the folders such that I remove the # character from their names.

The reason I want to achieve this is because I am using this structure in my public asset folder for a web project and the # character of course causes issues as it means something else when it comes to URLs.

The easiest thing for me would be if the # did not exist.

How would I achieve this in Windows (I am currently on 10)

Edd Chang
  • 111
  • 3
  • Write a short PowerShell script or use a tool that supports mass renaming folders. For a PS script you will probably need ForEach, Where-Object, Get-ChildItem and Rename-Item. – Seth Apr 22 '22 at 06:23
  • Could you point me towards a tool that would help me achieve this? – Edd Chang Apr 22 '22 at 06:34
  • I'd just write a script. There are dozens of mass renaming tools. Most focus on files but some probably also support recursive searches, that might be the biggest hurdle. – Seth Apr 22 '22 at 06:45
  • Alright. Is there a certain language that would be the most preferrable. If Powershell, could you point me towards any relevant knowledge as I am not much aware with it. If not, that is also ok. – Edd Chang Apr 22 '22 at 07:02
  • Have you tried anything to remedy this yourself? As for help you can use Get-Help, Get-Command and Get-Member to discover most of the informaton. about_Foreach and/or ForEach-Object, cut that short previsouly, will also be of interest. As shown in [this](https://superuser.com/questions/1701689/change-folder-name-with-the-name-between-with-powershell) question you can probably do it with just Get-ChildItem and Rename-Item. – Seth Apr 22 '22 at 07:12
  • A powershell script is indeed the easiest way to go here. Do you want to remove the # or replace it with something else, eg. a space, dash or underscore? – LPChip Apr 22 '22 at 07:40

3 Answers3

2

This can be a challenging script to create, so I've taken the liberty to write it for you. It replaces the # by a space. Feel free to change that to anything you want.

# go to the parent folder
set-location -Path C:\temp\test\test#name

#create list and sort it backwards (we want to rename from the deepest folders to the lowest.
$folders = get-childitem -Recurse -Directory |sort-object -Descending

$folders | ForEach-Object {
    set-location $_.parent.FullName
    rename-item $_.name ($_.name -replace("#", " "))
}
LPChip
  • 59,229
  • 10
  • 98
  • 140
1

I ended up using a python script that does the job.

This needs to of course run in the exact folder the os.walk would.... walk?

import os

for root, dirs, files in os.walk(".", topdown=False):
    for name in dirs:
        print("old name: " + name)

        new_name = name.replace('#', '')

        print('new name ' + name)

        os.rename(os.path.join(root, name), os.path.join(root, new_name))
Edd Chang
  • 111
  • 3
-1

You can use PowerRename in PowerToys:

  1. Download PowerToys
  2. Open PowerToys settings
  3. Activate PowerRename
  4. Do as shown in the GIF on the link below (choose all parent folders, fill in # to be replaced and nothing to replace it): http://norway-yv.epizy.com/files/howto.gif (I swear it is not a virus, but it was "too large" to be embedded in the answer)
norway-yv
  • 574
  • 1
  • 4
  • 18