Batch command to reorder file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dmmeek
Posts: 2
Joined: 30 Jul 2014 13:01

Batch command to reorder file name

#1 Post by dmmeek » 30 Jul 2014 13:18

I am by skill set not a programmer, but would greatly appreciate any help on creating a batch file. I have multiple PDF's in a folder and need to change the order of the file name. For example, the existing file is Monthly_Statement_07_2014_12345678.PDF, and need to reorder / change the name to 2014_07_12345678_Monthly_Statement.PDF. After a few Google searches I developed this script, but it doesn't appear to do anything.

@echo off & setlocal
for /f "tokens=1-5 delims=_" %%a in (*.pdf) do echo ren "%%a_%%b_%%c_%%d_%%e.pdf" "%%d_%%c_%%e_%%a_%%b.pdf"
::--- end

I created the file in Notepad and saved it as a .BAT. I am also running Windows 8.1 if that is a factor.

Thanks in advance.

Daniel

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch command to reorder file name

#2 Post by Squashman » 30 Jul 2014 13:27

Code: Select all

@echo off & setlocal
for /f "tokens=1-5 delims=_" %%a in ('dir /a-d /b *.pdf') do echo ren "%%a_%%b_%%c_%%d_%%~ne.pdf" "%%d_%%c_%%~ne_%%a_%%b.pdf"


remove the ECHO when the output looks correct.

dmmeek
Posts: 2
Joined: 30 Jul 2014 13:01

Re: Batch command to reorder file name

#3 Post by dmmeek » 31 Jul 2014 07:34

Thank you! Your script work perfectly.

Post Reply