Rename file to include the modified date in the file name

I am trying to write a batch file that will copy jpg files from my digital camera to my hard drive. But I would also like to include the file date in the name.

For example: a picture taken on 1/23/11 is called P1230001.JPG will be renamed to P1230001_20110123.JPG.

+3
source share
2 answers

Here is a helper batch file that will do what you want. To rename and rename a file with an added date, one parameter is required. Hope this helps.

@echo off
setlocal

if "%1"=="" goto USAGE
set file_name=%1
set name=%~n1
set ext=%~x1

dir %file_name% | findstr /i %file_name% > y.tmp

for /f "tokens=*" %%i in (y.tmp) do (
set line=%%i
)

del y.tmp

set month=%line:~0,2%
set day=%line:~3,2%
set year=%line:~6,4%

ren %file_name% %name%_%year%%month%%day%%ext%

goto EOF

:USAGE

echo %0 file_name

:EOF
endlocal
+1
source

All Articles