tferro82
asked on
Add date/timestamp via batch file
I'm using this batch file to add a 24hr formatted date/timestamp to some files. For some reason it doesnt work from 0 to 9 but does from 10 to 23. What am I missing here?
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
%windir%\system32\inetsrv\ appcmd.exe list wp > c:\perflogs\%computername% _W3WP_PID_ %mydate%_% mytime%.tx t
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
%windir%\system32\inetsrv\
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or be more reliable and don't use %date% and %time% that use the current users date format:
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
e.g. from there this gives you a reliable yyyy-mm-dd-hh-mm timestamp regardless of users date, time format etc. Looks a bit complicated but all it does is write a one line VBScript on the fly which it gets rather than using bits of %date% and %time% variables.
There are details my article on how you can get all the different bits of dates.
There are also other methods using WMIC etc. which will do it like this too.
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
e.g. from there this gives you a reliable yyyy-mm-dd-hh-mm timestamp regardless of users date, time format etc. Looks a bit complicated but all it does is write a one line VBScript on the fly which it gets rather than using bits of %date% and %time% variables.
There are details my article on how you can get all the different bits of dates.
There are also other methods using WMIC etc. which will do it like this too.
@echo off
echo wscript.echo year(date) ^& "-" ^& right(100+month(date),2) ^& "-" ^& right(100+day(date),2)^& "@" ^& right(100+hour(time),2) ^& "-" ^& right(100+minute(time),2) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmddhhmm=%%a
echo Now you can use this in your filename: %yyyymmddhhmm%
Steve
oBdA
You can't do the following:
set mydate=%mydate: =0%
because imagine if your date is:
20/07/2012
then you end up with:
2/7/212
The same applies for TIME aswell.
You can't do the following:
set mydate=%mydate: =0%
because imagine if your date is:
20/07/2012
then you end up with:
2/7/212
The same applies for TIME aswell.
C:\Temp>set mydate=20/07/2012
C:\Temp>echo %mydate: =0%
20/07/2012
oBdA
Oops! Beg you pardon....
I was reading it: 'set mydate=%mydate:0=%'. You are right to use 'set mydate=%mydate: =0%'.
I've just woke up!
Oops! Beg you pardon....
I was reading it: 'set mydate=%mydate:0=%'. You are right to use 'set mydate=%mydate: =0%'.
I've just woke up!
Also, if you can use a small free utility in your environment, then DOFF can be a great tool to add to your system.
http://www.jfitz.com/dos/index.html#DOFF
It would then allow you to simply do this to get the date formatted any way you desire (I did YYYYMMDD_hhmm for this example).
http://www.jfitz.com/dos/index.html#DOFF
It would then allow you to simply do this to get the date formatted any way you desire (I did YYYYMMDD_hhmm for this example).
@echo off
for /f "tokens=*" %%A in ('doff yyyymmdd_hhmi') do set MyStamp=%%A
%windir%\system32\inetsrv\appcmd.exe list wp > c:\perflogs\%computername%_W3WP_PID_%MyStamp%.txt
~bp
ASKER
this worked perfectly
@echo off
SET backuptime=%DATE:~6,4%-%DA
%windir%\system32\inetsrv\
0-9 will be single digit...