Page 1 of 1

Batch Renaming Script

Posted: 05 Jan 2011 19:33
by ArchAngel714
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!!

Re: Batch Renaming Script

Posted: 05 Jan 2011 19:57
by ghostmachine4
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.

Re: Batch Renaming Script

Posted: 06 Jan 2011 13:47
by ChickenSoup
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!"
)

Re: Batch Renaming Script

Posted: 06 Jan 2011 21:32
by ghostmachine4
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.

Re: Batch Renaming Script

Posted: 07 Jan 2011 01:54
by ghostmachine4
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"