Star Gazr1
asked on
batch file rename date
On Windows Server 2003
The following batch file below will rename a file called: filename.txt
to filenameDate.txt
*where date = todays date and is displayed as
MMDDYYYY (MM=Month DD = Day YYYY = year)
::BATCH FILE
@echo off
set today=%date:~4%
for %%a in ("filename.txt") do ren "%%~a" "%%~na%today:/=%%%~xa"
::END BATCH
The batch file runs daily and will always rename the file to the today's date.
I need to display the date as YYYYMMDD (year month day) instead of MMDDYYYY.
so the file will be renamed filenameYYYYMMDD.txt. Any ideas on how to do this? Thanks.
The following batch file below will rename a file called: filename.txt
to filenameDate.txt
*where date = todays date and is displayed as
MMDDYYYY (MM=Month DD = Day YYYY = year)
::BATCH FILE
@echo off
set today=%date:~4%
for %%a in ("filename.txt") do ren "%%~a" "%%~na%today:/=%%%~xa"
::END BATCH
The batch file runs daily and will always rename the file to the today's date.
I need to display the date as YYYYMMDD (year month day) instead of MMDDYYYY.
so the file will be renamed filenameYYYYMMDD.txt. Any ideas on how to do this? Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@qlemo. I get an error when trying to run your batch file:
error:
The following usage of the path operator in batch-parameter
substitution is invalid: %~xa"
error:
The following usage of the path operator in batch-parameter
substitution is invalid: %~xa"
ASKER
@TDLewis. Thanks for your reply. When I try your method:
@echo off
set today=for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
set today=%year%%month%%day%
for %%a in ("filename.txt.txt") do ren "%%~a" "%%~na%today:/=%%%~xa"
The file gets renamed but not with the year. I get the following
filename1226.txt I need it to be renamed to filename20121226.txt for today's date.
@echo off
set today=for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
set today=%year%%month%%day%
for %%a in ("filename.txt.txt") do ren "%%~a" "%%~na%today:/=%%%~xa"
The file gets renamed but not with the year. I get the following
filename1226.txt I need it to be renamed to filename20121226.txt for today's date.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@tdlewis when I run the command date /T
I get:
Wed 12/26/2012
I get:
Wed 12/26/2012
ASKER
@tdlewis:
When I add the four lines in to the batch file:
echo year: %year%
echo month: %month%
echo day: %day%
echo today: %today%
I get back
year:
month: 12
today: 1226
When I add the four lines in to the batch file:
echo year: %year%
echo month: %month%
echo day: %day%
echo today: %today%
I get back
year:
month: 12
today: 1226
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that worked, fantastic!. Thanks.
You see my point then perhaps about why not to use date /T or %date% .... different machine, different user, language, date format (ddd dd/mm/yyyy, mm/dd/yyyy etc. And it breaks.
Dragon is correct about the fragile usage of %date% and date /t. In a restricted environment it works as expected, but it might not work on other machines.
My code was only missing one out of 3 percent signs:
My code was only missing one out of 3 percent signs:
@echo off
REM %date% is ddd, mm/dd/yyyy
set yyyymmdd=%date:~-4%%date:~-10,2%%date:~-7,2%
for %%a in ("filename.txt") do ren "%%~a" "%%~na%yyyymmdd%%%~xa"
So personally I nearly always use a one line vbscript in the batch file. Really easy to do and 100% reliable as VBscript does dates natively.
If you click on my dragon-it link you should be able to see the article details. There are examples in there to get you any date and time format you want.
Steve