Batch Renaming Script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ArchAngel714
Posts: 1
Joined: 05 Jan 2011 19:30

Batch Renaming Script

#1 Post by ArchAngel714 » 05 Jan 2011 19:33

I am very new to using MS-DOS as a scripting language so please forgive my (hopefully) easy question. What I am trying to do is take all the files in a folder that are of the following specific format and rename the file slightly (add some periods to it). Below are the details

Input file format
Text with Spaces (01032011).avi
Output file format
Text with Spaces 01.03.2011.avi

Example:
"Modern Family (01032011).avi" is renamed to "Modern Family 01.03.2011.avi"

This will need to be done automatically i.e. a windows schedule task can run it every 30 minutes.

Thanks so much!!

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch Renaming Script

#2 Post by ghostmachine4 » 05 Jan 2011 19:57

download gawk for windows and do this

Code: Select all

BEGIN{        q="\042" # double quote  }
{
        o=$0
        match ( $0 , /\([ \t]*[0-9]{8}[ \t]*\)/, a )
        date = substr($0, RSTART, RLENGTH)
        gsub(/\(|\)| /,"",date);
        date=substr(date,0,2)"."substr(date,3,2)"."substr(date,5)
        $0 = substr(o,0,RSTART-1) date substr(o,RSTART+RLENGTH)
        cmd = "rename " q o q " " q $0  q
        print cmd
        # system(cmd) #remove the # to use this command
}

copy and paste the above in an editor and rename it to rename.awk, then on the command line

Code: Select all

C:\test>dir /B text*avi
Text with Spaces  with     many        spaces (       01032011 ).avi
Text with Spaces (rerun ) (01032011).avi
Text with Spaces(01032011).avi

C:\test>dir /B text*avi | awk --re-interval -f rename.awk
rename "Text with Spaces  with     many        spaces (       01032011 ).avi" "Text with Spaces  with     many        spaces 01.03.2011.avi"
rename "Text with Spaces (rerun ) (01032011).avi" "Text with Spaces (rerun ) 01.03.2011.avi"
rename "Text with Spaces(01032011).avi" "Text with Spaces01.03.2011.avi"


It is able to take care of the above situations. remove the comment (#) to do the actual rename.
Last edited by ghostmachine4 on 06 Jan 2011 21:30, edited 2 times in total.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Batch Renaming Script

#3 Post by ChickenSoup » 06 Jan 2011 13:47

This will do it in batch. This assumes that you will not have a file named "Modern Familiy (rerun) (01032011).avi"

Code: Select all

@SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=1,2 delims=()" %%a in ('dir /b /D C:\yourdir ^| findstr /r "([1234567890]*)\.avi\>"') do (
   set string=%%b
   REM echo !string!
   REM echo !string:~0,2!.!string:~2,2!.!string:~4,4!
   set fullstring=%%a !string:~0,2!.!string:~2,2!.!string:~4,4!.avi
   set fullstring=!fullstring:  = !
   rename "C:\yourdir\%%a(%%b).avi" "!fullstring!"
)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch Renaming Script

#4 Post by ghostmachine4 » 06 Jan 2011 21:32

ChickenSoup wrote: This assumes that you will not have a file named "Modern Familiy (rerun) (01032011).avi"

good point. now my code takes care of that.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch Renaming Script

#5 Post by ghostmachine4 » 07 Jan 2011 01:54

ChickenSoup wrote:This will do it in batch. This assumes that you will not have a file named "Modern Familiy (rerun) (01032011).avi"

Code: Select all

 findstr /r "([1234567890]*)\.avi\


and it also assumes that there are no files like "Modern Family (0).avi"

Post Reply