Tuesday, April 26, 2011

Another Example with Renaming Files

Renaming files is likely the most common task being automated with the help of batch files. Here we will discuss how to add the date of file's creation to its name. As far as you can see, the script to automate this task is quite simple, but you can enhance it with the help of our powerful batch files editor.
SETLOCAL ENABLEDELAYEDEXPANSION
@echo off
for /f %%i in ('dir /b') do (
set aaa=%time%
set a1=!aaa:~0,2!
set a2=!aaa:~3,2!
set a3=!aaa:~6,2!
ren "%%i" "!a1!-!a2!-!a3!_%%i"
)
endlocal
If you run this script, you will see that it adds the date before the name of the file. It is not hard to modify the script to make it add the date to the end of batch file.
And here is also another version of this task's automation:
@echo off
for /f %%i in ('dir /b') do (
set "file=%%i"
call :ert
)
goto :eof
:ert
ren "%file%" "%time:~0,2%-%time:~3,2%-%time:~6,2%_%file%"
Both of these scripts are quite quick, so in general you can use any of them.

No comments:

Post a Comment

Translate