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%.txt
tferro82Asked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Because you're not working with quotes, and the hours don't have leading zeros but leading spaces. Try this:
@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)
set mydate=%mydate: =0%
set mytime=%mytime: =0%
%windir%\system32\inetsrv\appcmd.exe list wp >"c:\perflogs\%computername%_W3WP_PID_%mydate%_%mytime%.txt"

Open in new window

0
 
TazDevil1674Commented:
Maybe try something slightly different.

@echo off
SET backuptime=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%_%TIME:~0,2%-%TIME:~3,2%

%windir%\system32\inetsrv\appcmd.exe list wp > c:\perflogs\%computername%_W3WP_PID_%backuptime%.txt

0-9 will be single digit...
0
 
Steve KnightIT ConsultancyCommented:
Or be more reliable and don't use %date% and %time% that use the current users date format:

http://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%

Open in new window

Steve
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Paul TomasiCommented:
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.
0
 
oBdACommented:
C:\Temp>set mydate=20/07/2012

C:\Temp>echo %mydate: =0%
20/07/2012

Open in new window

0
 
Paul TomasiCommented:
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!
0
 
Bill PrewCommented:
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).

@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

Open in new window

~bp
0
 
tferro82Author Commented:
this worked perfectly
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.