[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add date/timestamp via batch file

Posted on 2012-08-23
8
Medium Priority
?
1,588 Views
Last Modified: 2012-08-24
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
0
Comment
Question by:tferro82
8 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 38325469
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
 
LVL 9

Expert Comment

by:TazDevil1674
ID: 38325496
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38325543
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
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
LVL 11

Expert Comment

by:paultomasi
ID: 38325685
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
 
LVL 85

Expert Comment

by:oBdA
ID: 38325703
C:\Temp>set mydate=20/07/2012

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

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38325798
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38326155
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
 

Author Closing Comment

by:tferro82
ID: 38329502
this worked perfectly
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question