ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

Renaming folders down a directory



  dCi 65 + C2 (<Sold)
Got a massive file directory, a user has the same folder within hundreds of folders, and he wants it renamed say from 'TEST1' to 'TEST2'

How can I do this without manually going through each one and changing it?

I've tried FileMonkey and BulkRenameUtility but neither seem to be much good.

Thanks.
 
  RB FF 182
Treesize?

TBH i think it's going to be a bugger,

You could just recreate the folders via a DOS script.
 
  dCi 65 + C2 (<Sold)
Tried TS too before I researched other ones.

The original query was about copying a directory but without the files, easy enough using xcopy, but I was genuinely stumped at this.

Would recreating not essentially be the same as search and replace? Can you even do that with a DOS script?
 
  dCi 65 + C2 (<Sold)
Cheers for the suggestion Darren but the free version only does single level renaming.

Do you fancy helping me out sn00p, not too handy with scripts. :(
 

sn00p

ClioSport Club Member
  A blue one.
Just as a quickie:


Code:
#!/usr/bin/perl -w

use File::Find;

$searchFolder = "/Users/homersimpson/test";
$matchName = "test2";
$replaceName = "test3";

finddepth(\&fileRenamer, "$searchFolder");

sub fileRenamer
{
	$sourceName = "$File::Find::dir/$_";
	
	if (-d $sourceName) {
		
		if ($_ eq $matchName) {
			$targetName = "$File::Find::dir/$replaceName";
			
			print("renaming $sourceName to $targetName\n");
			
			rename($sourceName, $targetName);
		}
	}
}

There are 3 variables at the top you need to change:

Code:
$searchFolder

This needs to be a full path to the root folder you want to rename from, it has to be a full path because I use the Find::dir to construct the source filename to use in the comparison.

Code:
$matchName

This is the name of the folder you want to match on, so if you want to rename all folders named "bob", change the value to "bob";

Code:
$replaceName

This is the replacement name, so if you set the match name to "bob" and the replace name to "steve" then any folder below the search root will be renamed to "steve".

You could convert this to take command line arguments easily if you wanted, this is just a quick bit of code to show you how to do what you want.

Try it on something safe before letting it loose on real data, and do it on a copy of the real data and not on your original!

(* no warranty is given, expressed or implied!)

Oh and this was written and tested (briefy) on unix, no idea how it will behave on windows (if it doesn't work straight off, then it'd be the slashes being the otherway round in paths) - it'd work fine "out of the box" under cygwin.
 
Last edited:

sn00p

ClioSport Club Member
  A blue one.
Thanks so much for taking the time to write that up! :)

I'll have a bash at work tomorrow, cheers pal!

Let me know how you get on.

Btw, before running it for the first time I'd put a # infront of the rename command, that way you'll see what folders it would have renamed and their new folder names but without it actually doing anything....a dry run.
 
  Clio 182
Code:
Dim fso,folders,Subfolders 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set objFolders = fso.GetFolder("c:\Temp\Test") 
set colsubfolders = objfolders.SubFolders 

For Each subfolder in colsubfolders
        RenameFolder(subfolder.Name) 
Next 

Sub RenameFolder(folder)
    strOriginalFolder = folder
    strNewFolder = folder & "2"
subfolder.Name = strNewFolder 
End Sub

Doh was too slow :) Oh well here is a VBscript to rename folders. As with Sn00pi won’t be held responsible if this screws up your system. If you want to try this just change the path from C:\Temp\Test to whatever your root folder is and it will go through all the folders and rename them from whatever they are called to something with a 2 on the end. I hope that is what you meant from you OP.
 
  dCi 65 + C2 (<Sold)
It didn't work sn00p. :(

Woodentop getting errors on lines 12 & 13

Sub RenameFolder(folder)
strOriginalFolder = folder ***
strNewFolder = folder & "2" ***
subfolder.Name = strNewFolder
End Sub

The folders I want renamed are 'rename' and I want them renamed to 'renamed'.

Sub RenameFolder(folder)
strOriginalFolder = rename
strNewFolder = renamed
subfolder.Name = strNewFolder
End Sub

Have I done it wrong?
 
  Clio 182
It didn't work sn00p. :(

Woodentop getting errors on lines 12 & 13

Sub RenameFolder(folder)
strOriginalFolder = folder ***
strNewFolder = folder & "2" ***
subfolder.Name = strNewFolder
End Sub

The folders I want renamed are 'rename' and I want them renamed to 'renamed'.

Sub RenameFolder(folder)
strOriginalFolder = rename
strNewFolder = renamed
subfolder.Name = strNewFolder
End Sub

Have I done it wrong?

You are getting errors on the lines you have changed... Are you just looking for folders called rename? If so the code needs to be changed to look for that. At the moment it will take what the name is and add whatever you want on the end.
 

sn00p

ClioSport Club Member
  A blue one.
What do you mean "it didn't work?" did the script give an error? If so, what error?

It worked fine for me.

I had a structure like:

Test/Test2/Test3/Test4/Test3
Test/Test3/Test2/Test3
Test/Test4

I set $searchFolder to say "/Users/me/Test" and then $matchName to "Test3" and $replaceName to "Test5" and got the result:

Test/Test2/Test5/Test4/Test5
Test/Test5/Test2/Test5
Test/Test4

If it didn't work and the script didn't error then it's most likely that you're running it under windows and that the slashes are the wrong way around. Btw, you'll probably have to escape them if you use standard windows slashes (\) because that's the escape char in strings, so C:\folder\test would actually be C:\\folder\\test, at a random guess perl should allow you to use unix slashes so you could use C:/folder/test.

It's important in my script to give the full path to the root folder because otherwise it won't work.

(Edit: Mines also case sensitive so a matchName of test2 would not pick up a folder named Test2)

Edit2:

perl will automagically convert path separators to the correct form for the host, so if you want the root to be:

C:\fred\jim\thefolder

set $searchFolder to

$searchFolder="C:/fred/jim/thefolder"

I see no reason why the script shouldn't work.
 
Last edited:
  dCi 65 + C2 (<Sold)
I am running it on XP. How do I compile it?

(You're dealing with a scripting noob, in case you hadn't guessed that already lol).

Your script didn't give an error, Woodentops did.
 

sn00p

ClioSport Club Member
  A blue one.
I am running it on XP. How do I compile it?

(You're dealing with a scripting noob, in case you hadn't guessed that already lol).

Your script didn't give an error, Woodentops did.

Install perl from:

http://win32.perl.org/wiki/index.php?title=Main_Page

and once installed (and ensuring perl.exe is in the path) and in the path with my script type "perl rename.pl" (assuming you named the script rename.pl)

Or

Download cygwin

http://www.cygwin.com/

This installs a unix type shell, when you install cygwin you get a list of things to install with it, in the list is perl, check that.

Once installed, run the cygwin prompt which gives you a unix style prompt.

To access the "windows filesystem" you can either use:

$sourceFolder = "C:/fred/jim/test" (not recommended)

or

$sourceFolder = "/cygdrive/c/fred/jim/test" (recocommended)

The second form is more "unix friendly".

Personally, I'd use cygwin because the script will definately work under it and it makes windows look like unix to the command prompt.

Edit: if you save the script as "C:\renamer.pl" then you can get to it by:

cd /cygdrive/c

then inside it you'll see renamer.pl

either type perl renamer.pl which will run the script via perl....

or

chmod 755 renamer.pl

(this will make it executable)

then you can just type

./renamer.pl

and it'll run.

It sounds a whole lot more complex than it actually is, you'll only have to do these steps once. Once you've done it you can just edit the script appropriately and then run it over and over if you need.
 
  dCi 65 + C2 (<Sold)
Lol! Gotta run it on a directory with 45,000 folders and is 83GB.

Just used a disk back up to create a copy of it, gonna test it on that first (didn't finish before home time so I'll update tomorrow)
 
  dCi 65 + C2 (<Sold)
It's not working. :(

#!/usr/bin/perl -w
use File::Find;
$searchFolder = "F:\\TEST Bridges\\Bear Consultancy Services\\3G North East\\NE-16 Bridges";
$matchName = "03 PI, GI and FTI";
$replaceName = "TESTTESTTEST";
finddepth(\&fileRenamer, "$searchFolder");
sub fileRenamer
{
$sourceName = "$File::Find::dir/$_";

if (-d $sourceName) {

if ($_ eq $matchName) {
$targetName = "$File::Find::dir/$replaceName";

print("renaming $sourceName to $targetName\n");

rename($sourceName, $targetName);
}
}
}

Is it because it's on the F drive?
 

sn00p

ClioSport Club Member
  A blue one.
It's not working. :(

#!/usr/bin/perl -w
use File::Find;
$searchFolder = "F:\\TEST Bridges\\Bear Consultancy Services\\3G North East\\NE-16 Bridges";
$matchName = "03 PI, GI and FTI";
$replaceName = "TESTTESTTEST";
finddepth(\&fileRenamer, "$searchFolder");
sub fileRenamer
{
$sourceName = "$File::Find::dir/$_";

if (-d $sourceName) {

if ($_ eq $matchName) {
$targetName = "$File::Find::dir/$replaceName";

print("renaming $sourceName to $targetName\n");

rename($sourceName, $targetName);
}
}
}

Is it because it's on the F drive?

No idea!

I'd use unix type slashes, so "F:/TEST Bridges/" etc.

You're trying to rename a folder named "03 PI, GI and FTI" to "TESTTESTTEST"?

It's possible it's case sensitivity, so change the script to:

Code:
#!/usr/bin/perl -w

use File::Find;

$searchFolder = "F:/TEST Bridges/Bear Consultancy Services/3G North East/NE-16 Bridges";
$matchName = "3 PI, GI and FTI";
$replaceName = "TESTTESTTEST";

finddepth(\&fileRenamer, "$searchFolder");

sub fileRenamer
{
	$sourceName = "$File::Find::dir/$_";
	
	if (-d $sourceName) {
		
		if (lc($_) eq lc($matchName)) {
			$targetName = "$File::Find::dir/$replaceName";
			
			print("renaming $sourceName to $targetName\n");
			
			rename($sourceName, $targetName);
		}
	}
}

I Just added "lc(...)" arond the string comparisons which will make them case insensitive.
 

sn00p

ClioSport Club Member
  A blue one.
Magic Johnson said:
Also, (being pestered again) is it possible to mass create and add folders within every folder with a certain name?
Sure,

You'd use the same script and when you find a match on the folder you'd just do a mkdir.
 
  dCi 65 + C2 (<Sold)
The double backslash worked for me in my test run on my local computer. The single forward slash isn't working either. Suckage!

It's getting an error but the cmd disappears before I can read it!

Begins with 'Can't' lol.

:(

EDIT: YAY got a screenie, took me about 10 mins to get! lol

eztxz.jpg
 
Last edited:
  Clio 182
This is why you should have used VB :) Looks like the slashes are messed up in the screenshot. Also if the file path is longer than 256 characters then you will hit problems reading the path.
 

sn00p

ClioSport Club Member
  A blue one.
It has obviously traversed down the tree, I'm guess that the problem is:

1) Either related to it being a network share
2) The path being incredibly long.
3) Windows.

I'd try installing cygwin+perl and use it from there, that way it'll behave like unix rather than the native perl you installed.

Windows file sharing (SMB) is crap at the best of times, is there any reason why you can't copy the data locally and use it from there? It'll be a hell of a lot quicker without all the SMB traffic you'll be creating.
 

sn00p

ClioSport Club Member
  A blue one.
This is why you should have used VB :) Looks like the slashes are messed up in the screenshot. Also if the file path is longer than 256 characters then you will hit problems reading the path.

The slashes are fine, perl will change the slashes to the host system separator - even with a mixture.
 


Top