Batch File Rename (reorder filename parts)

Microsoft Windows xp professional w/serv...
April 14, 2010 at 14:49:10
Specs: Windows XP
Hi. I have a folder of .pdf files I'd like to rename using a batch file. Each filename consists of three words separated by an underscore:


Would like to rename with this result:


Am still pretty new at batch file programming and appreciate the help!

See More: Batch File Rename (reorder filename parts)

Report •

April 14, 2010 at 17:11:58
@echo off & setlocal
for /f "tokens=1-3 delims=_" %%a in ('dir /b *.pdf') do echo ren "%%_%%b_%%c.pdf" "%%c_%%b_%%a.pdf"
::--- end
i left the safety "on" (echo). If satisfied, just remove the "echo" from the command-string.

Report •

April 14, 2010 at 17:36:14
you can use Python or Perl , if you are allowed to
eg Python
import os,glob
for files in glob.glob("*.pdf"):
    name,extension = os.path.splitext(files)
    newname='_'.join([c,b,a]) + "."+extension

GNU win32 packages | Gawk

Report •

April 15, 2010 at 10:43:36
Nbrane, looks like it's almost there. Right now, %%c contains the file extension ".pdf". Do I need to use the period as a delimiter as well? And how would that be implemented?

Ghostdog, thanks a lot. Not a Python user yet, but starting in a couple of months.

Report •

Related Solutions

April 15, 2010 at 12:21:44
Yeah, my mistake, my apologies. Assuming none of the filenames contains a dot (other than the extension), the dot would be the easiest way:
for /f "tokens=1-3 delims=_." %%a in ('dir /b *.pdf') do echo ren "%%a_%%b_%%c.pdf" "%%c_%%b_%%a.pdf"
(i take it you saw my other obvious mistake and fixed it. I'm such a dummy!)

Report •

April 15, 2010 at 14:16:12
Sweet. It's working perfectly now on my computer.

Last question: when I implement in a folder on a network (not my local computer) I'm getting the error message "The filename, directory name, or volume label syntax is incorrect." I'm renaming the same files except this time the files and batch file are in a folder on the network -- that's the only different. Apologize in advance if this is a no brainer. Like I said, I'm still fairly new at this. What needs to change to avoid this message?

Report •

April 15, 2010 at 16:53:30
I don't know! How are you "implementing" the batch on network folders? ie: are you running it on the remote machine, or local machine with a path to the remote, and is remote a mapped drive or a unc like \\machn\c\work.
I tried it here (over network, using mapped drive) and got no errors (call mine swap.bat):
cd H:\test
no errors.
try running it with echo ON and see what it's looking at.

Report •

Ask Question