Saturday, November 21, 2009

Batch Program to Rename file with date & counter

One of my interface development requirements specified to take backups of a file with the prevailing sysdate appended.
Hence i sat down to see how this could be achieved with Batch Programming as my environment was Windows Platform.

An initial code was adapted from MSFN Forums & it behaved very well:

@Echo off
rem Original Batch File Code Location: http://www.msfn.org/board/lofiversion/index.php/t47812.html
rem Adapted By: Shalabhsneha Katdare (SHALABHKATDARE AT GMAILDOTCOM)
rem Reading input file
If "%1" EQU "" (
 Echo You must use parameter - name of file to rename.
 Goto :EOF
    ) ELSE (
 Set strFile2Rename=%1
    )
If not exist %strFile2Rename% (
    Echo %strFile2Rename% was not found!
    Goto :EOF
    )
For /f "usebackq delims=. tokens=1,2" %%i IN (`echo %strFile2Rename%`)    Do Set strFileExtension=%%j

rem Main code
call:ConvertDate
call:RenameFile
Goto:EOF
    
:ConvertDate    
rem Reading date format, converting and saving date
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 set A=%%a&set B=%%b&set C=%%c)
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
   set dd=%%d&set mm=%%e&set yy=%%f))
Goto:EOF

:RenameFile
ren %strFile2Rename% File_%dd%-%mm%-%yy%.%strFileExtension%
Echo File %strFile2Rename% is renamed to File_%dd%-%mm%-%yy%.%strFileExtension%

Usage:
cmd:\>rename2date filename.fileext
Output:
filename_sysdate.fileext

As you could see this was the perfect thing which i wanted. But soon i faced a major bottleneck!
The backup location was common for all the files. Hence in a given day, if i needed to take backup of the same file again, the batch program failed as the file formed upon renaming will be pre-existing from the backup taken earlier.

This evolved my initial requirement to a batch program doing rename with sysdate & counter added to it so that multiple renames in day could be supported for the same file.
Further modification to the initial above logic resulted into below code:

@Echo off
rem Original Batch File Code Location: http://www.msfn.org/board/lofiversion/index.php/t47812.html
rem Adapted By: Shalabhsneha Katdare (SHALABHKATDARE AT GMAILDOTCOM)
rem v1.1 [Added counter facility alongwith date. So we can rename a file multiple times on a given day!]
rem v1.0 [Plain rename facility without counter, i.e. on a given day the file can be renamed only once!]
rem Reading input file
set count=1
If "%1" EQU "" (
 Echo You must use parameter - name of file to rename.
 Goto :EOF
    ) ELSE (
 Set strFile2Rename=%1
    )
If not exist %strFile2Rename% (
    Echo %strFile2Rename% was not found!
    Goto :EOF
    )
For /f "usebackq delims=. tokens=1,2" %%i IN (`echo %strFile2Rename%`)    Do Set strFileExtension=%%j
For /f "usebackq delims=. tokens=1,2" %%i IN (`echo %strFile2Rename%`)    Do Set strFile=%%i
rem Main code
call:ConvertDate
call:RenFile
Goto:EOF
    
:ConvertDate    
rem Reading date format, converting and saving date
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 set A=%%a&set B=%%b&set C=%%c)
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
   set dd=%%d&set mm=%%e&set yy=%%f))
Goto:EOF

:RenFile
rem Renaming the file with date & counter
set FileCheck=%strFile%_%dd%-%mm%-%yy%_%count%.%strFileExtension%
If not exist "%FileCheck%" ( 
ren %strFile2Rename% %strFile%_%dd%-%mm%-%yy%_%count%.%strFileExtension% 
Goto :EOF
) ELSE (
set /a count=count+1 
call:RenFile)
Goto:EOF

Usage:
cmd:\>rename2date filename.fileext
Output:
filename_sysdate_counter.fileext

If you see the revision in code, i have just made a simple recursive call so that until a file (Filename formed post rename) is existing we would loop to increment the counter so that we finally arrive at unique filename with sysdate & a counter permitting us multiple renames on any given day for same file.

One thing which i struggled with Batch Programming is, its very sensitive to its syntax semantics. If a brace or two are not properly placed, the program would throw a annoying error of "The syntax of the command is incorrect"

It is difficult to trace where exactly is the syntax error coming from, but a quick echo ON helps & guides to correct directions.

So finally i have my batch program to rename a file with date & a counter :-D

Keywords:
how to rename multiple files, batch file rename, windows batch file rename, rename files, rename files windows, mass rename files, file rename program, file rename tool, quick file rename, batch file, batch file renamer, batch file renaming, batch file programming dos, windows batch file, file renamer, rename file, batch file commands, batch file command, bat files, bat file, batch files, multiple rename, batch script, batch scripting, batch program, windows scripting, windows scripts, DOS, etc

1 comment:

Anonymous said...

Didn't work for me ?Followed your steps exactly !