anyone good at batch scripts? (filename renaming)

Our "pub" where you can post about things completely Off Topic or about non-silent PC issues.

Moderators: NeilBlanchard, Ralf Hutter, sthayashi, Lawrence Lee

Post Reply
shunx
Posts: 341
Joined: Sun Oct 06, 2002 1:20 pm
Location: Vancouver

anyone good at batch scripts? (filename renaming)

Post by shunx » Tue Aug 18, 2009 10:57 pm

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.

Kriz
*Lifetime Patron*
Posts: 51
Joined: Tue Sep 02, 2008 10:16 am
Location: Australia

Post by Kriz » Wed Aug 19, 2009 2:20 am

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:

xyz<xyz>*.*

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.

quikkie
Posts: 235
Joined: Tue Sep 20, 2005 5:21 am
Location: Soham, UK

Post by quikkie » Wed Aug 19, 2009 7:17 am

http://www.bulkrenameutility.co.uk/ - 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.

Vicotnik
*Lifetime Patron*
Posts: 1831
Joined: Thu Feb 13, 2003 6:53 am
Location: Sweden

Post by Vicotnik » Wed Aug 19, 2009 7:43 am

Total Commander has an excellent multi-rename tool.

Image

shunx
Posts: 341
Joined: Sun Oct 06, 2002 1:20 pm
Location: Vancouver

Post by shunx » Wed Aug 19, 2009 11:15 am

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!"
)

Post Reply