jpk2000
asked on
How to save file with date as part of the file name?
I am creating a BAT file that needs to rename an existing file to a file name that has today's date as part of it's name. Example: REN example.TXT example.08021999.TXT. This procedure will run once a day after the original file (i.e. "example.TXT") is downloaded. Is there a %date% variable that can be used similar to using %username% in the home directory in an NT user profile?
Thanks much.
Thanks much.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's one I prepared earlier for another question on EE (Y2K compliant!):
@echo off
:: Rename a file passed as a parameter to it's creation date-time
:: Mark Crossley Nov 1998
:: mark@carmine.demon.co.uk
setlocal
:: set for UK date format dd/mm/yy hh:mi
:: %%i=dd, %%j=mm %%k=yy %%l=hh %%m=mi
:: Change the order in the CALL RENIT to your locale
for /f "tokens=1-5 delims=/: " %%i in ('dir /tc "%1"^|findstr %~n1') do call :renit %1 %%k %%j %%i %%l %%m
goto end
:renit
if %2 LSS 80 (set yr=2000) else (set yr=1900)
set /a yr=yr+%2
rename "%1" "%~dp1%yr%%3%4-%5%6%~x1"
goto :eof
:end
endlocal
=======
Mark
@echo off
:: Rename a file passed as a parameter to it's creation date-time
:: Mark Crossley Nov 1998
:: mark@carmine.demon.co.uk
setlocal
:: set for UK date format dd/mm/yy hh:mi
:: %%i=dd, %%j=mm %%k=yy %%l=hh %%m=mi
:: Change the order in the CALL RENIT to your locale
for /f "tokens=1-5 delims=/: " %%i in ('dir /tc "%1"^|findstr %~n1') do call :renit %1 %%k %%j %%i %%l %%m
goto end
:renit
if %2 LSS 80 (set yr=2000) else (set yr=1900)
set /a yr=yr+%2
rename "%1" "%~dp1%yr%%3%4-%5%6%~x1"
goto :eof
:end
endlocal
=======
Mark
Good Luck