Page 1 of 1

anyone good at batch scripts? (filename renaming)

Posted: Tue Aug 18, 2009 10:57 pm
by shunx
can anyone write a script that does the following type of substitution:

abc-123.txt --> xyzxyz-123.txt
abc-456.txt --> xyzxyz-456.txt
abc-789.txt --> xyzxyz-789.txt

So I want to replace all instances of "abc" with "xyzxyz", and keep other things.

ren abc*.txt xyzxyz*.txt doesn't work because it will delete part of the original name due to the extra number of characters.

Posted: Wed Aug 19, 2009 2:20 am
by Kriz
I'd suggest you give ZTreeWin a try if you don't already use it. It's got a powerful renaming function that allows the kind of thing you're asking.

This is what I've come up with after a minute of testing, so it might not be the most direct way to do it, but appears to do the job:

(Before doing the following, it's worth testing this on a copy of the original files, or at least a small section of them)

1. Navigate to the directory containing the files you wish to rename, then press enter.
2. Tag the files individually using the T key on each file, or to tag all the files in the directory it's CTRL-T
3. Then to rename all the Tagged files, it's CTRL-R
4. When it asks what you want to rename the tagged files to, type in:


It will ask to confirm the change and display the original name and the new name.

Of course, you'll need to change xyz to what you want to use, but on your example list the above works for me.

If you have troubles, hit the F1 key at the Rename screen to access the detailed help on Renaming. There are many examples and more information on it's rename function so it's bound to have what you need there.

Posted: Wed Aug 19, 2009 7:17 am
by quikkie - works for me. It can do regular expressions, cut X characters from beginning or end of the filename, or find X and replace with Y and some other stuff I've never used.

Posted: Wed Aug 19, 2009 7:43 am
by Vicotnik
Total Commander has an excellent multi-rename tool.


Posted: Wed Aug 19, 2009 11:15 am
by shunx
The thing is that I will receive files with the same type of filenames incrementally, that's why I'm looking for a one-click solution where I click on a file and it will execute the renaming in the folder. This can be either a batch file or a trigger for a renaming utility. Can any of those utilities generate such a trigger?

edit: I spent some time and made a rough script that worked:

setlocal EnableDelayedExpansion
cd c:\containing_folder\
dir /b /on /a-d %1 > filenames.txt
FOR /F "tokens=1,2 delims=-" %%G IN (filenames.txt) DO (
set str=%%H
set str=xyzxyz-!str!
ren %%G-%%H "!str!"