Discussion Forums > Technology

Batch file renaming (replacing strings) & directory listing

(1/2) > >>

Sakura90:
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? :-\

Bob2004:
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: ---
echo Listing all files in C: > filelist.txt
forfiles /p c:\ /s /c "cmd /c echo @file >> filelist.txt"

--- End code ---

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.

AnimeOokami:
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.

Sakura90:
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



--- Quote from: Bob2004 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: ---
echo Listing all files in C: > filelist.txt
forfiles /p c:\ /s /c "cmd /c echo @file >> filelist.txt"

--- End code ---

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.

--- End quote ---
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: ---
echo Listing all files in C: > filelist.txt
forfiles /p c:\ /m *.* /s /c "cmd /c echo @file >> c:\filelist.txt"
--- End code ---

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


--- Quote from: AnimeOokami 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.

--- End quote ---
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).

AnimeTurtle:
heh I just use the good ol' DOS dir command with the /b switch and direct the output to a text file:


--- Code: ---
dir /b >> myfile.txt
--- End code ---

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


--- Code: ---
ren "whatever01.mkv" "whatevernew01.mkv"
ren "whatever02.mkv" "whatevernew02.mkv"
--- End code ---

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

Navigation

[0] Message Index

[#] Next page

Go to full version