Author Topic: Batch file renaming (replacing strings) & directory listing  (Read 982 times)

Offline Sakura90

  • Member
  • Posts: 523
  • Got panties? ♥
Ahhh, this is hard. Well, I need something like this.

I have a bunch of files (~1000) and a text file. Text file has lots of strings like this

"Original name" "New name"

I need a program to look recursively in a specified directory and change all instances of "Original name" to "New name", for both files and folders. The formatting of the text file can be different ofc, I don't care. But it has to be something along "old name" <separator> "new name". Note that "old name" and "new name" aren't a file name by itself, just part of the name of X number of files.

I normally use Bulk Rename Utility, great proggie. But it hasn't got a string replacer. It can just rename entire files from a text file, like this

Original File.mkv|New File.mkv

Doing that for thousands of files is a pain. I've found utilities more or less like the one I need, but they are all paid. I can't convert currency or get money out of the country, so even if I want I can't pay :P. It has to be a freeware. Any ideas?


Aside that I need something to make a list in a text file of all files in a directory (recursively and files only, not folders). The Windows "dir" command with the "/s" switch (for listing recursively a directory) always appends the full path before the files, even if I use the switch for bare format "/b" (removing the "/s" and leaving "/b" gives only the file names, like I want, but from a single folder). Unless there's a special way of using "dir", it simply doesn't work for me :(

And now that I think of it, it could be instead something to remove everything behind the first "\" counting from the right (to remove paths from the "dir" list) on all lines of the text file :P


Any ideas? :-\
« Last Edit: May 06, 2012, 05:24:18 PM by Sakura90 »
Quote from: Youko@TF
What does "[sic]" mean? I don't think anyone got sick in the article so why is it in there? Should I start writing and post "[dump]" when I leave to go take a shit then return?

Offline Bob2004

  • Member
  • Posts: 2562
Re: Batch file renaming (replacing strings) & directory listing
« Reply #1 on: May 06, 2012, 07:18:32 PM »
For dealing with lots of files, you usually want to use the forfiles command, assuming you're running Windows Vista or later (not sure if it's in XP). It's able to recursively scan through a directory, and for any files that match a pattern you specify, it runs a command (specified by you). Unfortunately, I don't think it's able to read the patterns it needs to match from a text file - you just pass it a regex, and it uses that. So it might not come in too useful for your first problem.

It should be easy enough to use it for your second problem though. A batch file containing something like this should work nicely:

Code: [Select]
echo Listing all files in C: > filelist.txt
forfiles /p c:\ /s /c "cmd /c echo @file >> filelist.txt"

This should print the filename of every file in C:, and in every subdirectory, to a text file called filelist.txt. I haven't tested it mind, and I'm too lazy to spend time adjusting it if it doesn't quite work, so you might need to fiddle around with the syntax a bit - but the principal is sound.

If you have a program that can solve your first problem when working with a single file at a time (ie. it can be given a file, and it will then scan through a text file of patterns and replace any that match with the correct replacement), then you can use the forfiles command to run that for every single file, passing it the filename for each one as needed. Just replace the echo command above with something like "cmd /c yourprogram.exe @FILE patternlist.txt". You can get it to only run on certain types of file as well, using the /m flag.

I've used it in the past to scan through my entire collection of music, search for any flac files, and convert them all into .ogg files with the same name, without touching any of my other music. I also saw a script someone else wrote using it to change the names of several thousand files to match a particular regex, but I can't remember how they did it.

Offline AnimeOokami

  • Member
  • Posts: 41
  • The big "bad" wolf.
Re: Batch file renaming (replacing strings) & directory listing
« Reply #2 on: May 06, 2012, 08:15:11 PM »
You could try Ant Renamer http://www.antp.be/software/renamer
It has String replacement and Multiple String replacement among other things :)

I have that and Bulk Rename Utility and I like both of them, although I don't use them often.

Offline Sakura90

  • Member
  • Posts: 523
  • Got panties? ♥
Re: Batch file renaming (replacing strings) & directory listing
« Reply #3 on: May 06, 2012, 09:08:57 PM »
EDIT: To anyone that cares, someone at CNET forums pointed me at this, PFrank. Awesome. It can do whatever you want. I made a txt with the names separated by commas (like I wanted), loaded it in the program and then ran the renaming process. Like a peach <3


For dealing with lots of files, you usually want to use the forfiles command, assuming you're running Windows Vista or later (not sure if it's in XP). It's able to recursively scan through a directory, and for any files that match a pattern you specify, it runs a command (specified by you). Unfortunately, I don't think it's able to read the patterns it needs to match from a text file - you just pass it a regex, and it uses that. So it might not come in too useful for your first problem.

It should be easy enough to use it for your second problem though. A batch file containing something like this should work nicely:

Code: [Select]
echo Listing all files in C: > filelist.txt
forfiles /p c:\ /s /c "cmd /c echo @file >> filelist.txt"

This should print the filename of every file in C:, and in every subdirectory, to a text file called filelist.txt. I haven't tested it mind, and I'm too lazy to spend time adjusting it if it doesn't quite work, so you might need to fiddle around with the syntax a bit - but the principal is sound.

If you have a program that can solve your first problem when working with a single file at a time (ie. it can be given a file, and it will then scan through a text file of patterns and replace any that match with the correct replacement), then you can use the forfiles command to run that for every single file, passing it the filename for each one as needed. Just replace the echo command above with something like "cmd /c yourprogram.exe @FILE patternlist.txt". You can get it to only run on certain types of file as well, using the /m flag.

I've used it in the past to scan through my entire collection of music, search for any flac files, and convert them all into .ogg files with the same name, without touching any of my other music. I also saw a script someone else wrote using it to change the names of several thousand files to match a particular regex, but I can't remember how they did it.
Excellent. Thanks. Just 2 things with forfiles. It needs the mask /m *.*, if not it lists folders and files. Also the filelist.txt must be pointed into a specific directory, if not, forfiles creates a different txt file inside every folder it analyzes (and each lists only the files in the folder the txt is sitting). So, for only files to be listed into a single txt it would be:

Code: [Select]
echo Listing all files in C: > filelist.txt
forfiles /p c:\ /m *.* /s /c "cmd /c echo @file >> c:\filelist.txt"

One problem solved ;D. Now I need something to analyze a filename and replace a string by another.

You could try Ant Renamer http://www.antp.be/software/renamer
It has String replacement and Multiple String replacement among other things :)

I have that and Bulk Rename Utility and I like both of them, although I don't use them often.
Great. Ant Renamer is *almost* what I'm looking for. It has a batch name replacer. BUT, it doesn't read from a given file, you have to put the string pairs from inside the program. Well, actually it has a .xml config file which you can edit by hand to create the "set" you want to use for renaming. <set header and name syntax> old name <tab key> new name <set end syntax>. The only problem is that it doesn't work with Japanese characters. Not even by saving the .xml as UTF-8 or ANSI. I have Japanese as default language, saving texts as ANSI actually saves as Shift-JIS, so in theory it should work (I know because programs that don't support Unicode just work with Japanese characters because they use the system's codepage).
« Last Edit: May 14, 2012, 04:02:20 AM by Sakura90 »
Quote from: Youko@TF
What does "[sic]" mean? I don't think anyone got sick in the article so why is it in there? Should I start writing and post "[dump]" when I leave to go take a shit then return?

Offline AnimeTurtle

  • Member
  • Posts: 1
Re: Batch file renaming (replacing strings) & directory listing
« Reply #4 on: May 29, 2012, 07:52:26 PM »
heh I just use the good ol' DOS dir command with the /b switch and direct the output to a text file:

Code: [Select]
dir /b >> myfile.txt

Then load up the text file in UltraEdit and use the column mode to add:

Code: [Select]
ren "whatever01.mkv" "whatevernew01.mkv"
ren "whatever02.mkv" "whatevernew02.mkv"

and Search & Replace where needed.  Works great since it uses RegEx in the S&R.  Then run it as a batch file.

Online Ixarku

  • Member
  • Posts: 4214
  • Professional Turd Polisher
Re: Batch file renaming (replacing strings) & directory listing
« Reply #5 on: June 07, 2012, 09:54:00 AM »
There are all sorts of ways to handle batch renaming of files.  When I've done this sort of thing in the past, I did it using MS Access.  I started with a simple VBA module to read a directory and write all of the file names into a table.  I had a second field in the table for the new file name, and I used Excel to manipulate my list and create new file names, then put those back into the table.  Another simple VBA procedure read the table, matched to the old file name, and replaced it with a new one.  VBA is pretty easy to learn, and the FileSystemObject is not difficult to work with.  You could do the same thing entirely in Excel using VBA, I just find it easier to work with recordsets in Access.
It took an hour to write; I figured it'd take an hour to read.

Offline Sakura90

  • Member
  • Posts: 523
  • Got panties? ♥
Re: Batch file renaming (replacing strings) & directory listing
« Reply #6 on: June 15, 2012, 12:14:54 AM »
For someone that could need something like this...

Someone other place I forgot (lol) told of of PFrank. As it advertises... it can do anything. By far the most complete renaming tool I ever saw :P. I used it for replacing a string in the names for another (romaji -> kanji in my case) and it went perfect ^_^

I made a csv file (just a txt with values separated by commas), loaded it in the program and poof, renaming done. Just as I wanted. Easy and no knowledge of scripts, programming, etc is needed. The renaming options are quite extensive.


Thanks anyway for you all :3
« Last Edit: June 15, 2012, 12:18:34 AM by Sakura90 »
Quote from: Youko@TF
What does "[sic]" mean? I don't think anyone got sick in the article so why is it in there? Should I start writing and post "[dump]" when I leave to go take a shit then return?