Batch Folder Renamer

bob332

Banned
Jan 25, 2002
597
0
0
what program will batch rename a bunch of folders? i already have a file renamer but it will not rename folders. any ideas??
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
You don't say what OS. If it's linux, there is a program called krename that you might check out. I haven't used it myself.

Of course you could probably roll your own fairly easily. I'm guessing you want something that will replace a pattern in the file/directory name with some other pattern?

#!/usr/bin/python

import glob
import re

if len(sys.argv) != 3:
[tab]print "USAGE: renamer old_pattern new_pattern"
[tab]sys.exit()

files = glob.glob('*' + sys.argv[1] + '*')

for file in files:
[tab]new_file = re.sub(sys.argv[1], sys.argv[2], file)
[tab]os.rename(file, new_file)




Note that I haven't actually tried this, the pattern matching part needs some refinement I'm sure.
Should have some more error checking as well.
But that's the basic idea.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Ok, here's a version that actually works!

#!/usr/bin/python

import glob
import re
import sys
import os

if len(sys.argv) != 3:
[tab]print "USAGE: renamer old_pattern new_pattern"
[tab]sys.exit()

files = glob.glob('*' + sys.argv[1] + '*')

for file in files:
[tab]new_file = re.sub(sys.argv[1], sys.argv[2], file)
[tab]print file, "\t\t", new_file
[tab]os.rename(file, new_file)



I've only done some very rudimentary testing. Don't blame me if it eats your data :D
Replace the [tab] with an actual tab.

Here's what this code will do for you:
Say you have some files and directories in your local directory:
stuff.f stuffit.f more_stuff.c more_stuff.h dir_stuff

Call renamer like this: renamer stuff STUFF

now you have:
STUFF.f STUFFit.f more_STUFF.c more_STUFF.h dir_STUFF

Of course, it would be nice to have more control of the pattern matching That's left as an exercise for the reader :D
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
I don't know of any program to batch folder renames, but you could write a windows script to do it. What are you trying to do? What are the folders supposed to be renamed to?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0


<< I don't know of any program to batch folder renames, but you could write a windows script to do it. What are you trying to do? What are the folders supposed to be renamed to? >>



Yea, I already wrote him a program to do it. Guess he lost interest.
 

bob332

Banned
Jan 25, 2002
597
0
0
no i haven't lost interest, i have just been very busy. i am also very green when it comes to programming. thank you for the help. this may be a dumb question, but what do i do with the code?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
You need to install python on your system. See www.python.org
Then, copy the code section of my post into a file. Replace the [tab] text with actual tabs.
Now, save the file.
You should now be able to run the program from a command line.
Double-clicking it should execute it, but it will just pop up the usage instructions cuz there won't be any variables passed in.

I've never used python on a windows system, so somebody might want to add to this.