Solved

Dos batch file - dealing with a nul value

Posted on 2008-06-24
5
1,251 Views
Last Modified: 2010-04-21
I am using a batch file in windows xp.
I am trying to create a continuous string with date and time stamp in it.  I can do it with the following command:
set archivestamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%%TIME:~3,2%

the only catch is that if it is before 10 am it does not attach the time becuse the leading time value is some sort of nul value.

If I can just test to see if it is null and can get around it but nothing seems to work.  if %time:~0,1% == nul echo "test" will fail saying "echo was unexpected at this time"

I tried checking to the reverse and it gives me the same error when I check for a 1 or 2.

How can I get around this?
0
Comment
Question by:naglpa
5 Comments
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 87 total points
ID: 21858480
Try this:

set MyTime=%TIME%
if "%MyTime:~0,1%"==" " set MyTime=0%MyTime:~1%
set archivestamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%MyTime:~0,2%%MyTime:~3,2%

Open in new window

0
 
LVL 34

Accepted Solution

by:
Gary Patterson earned 88 total points
ID: 21858751
You are getting a blank in that first TIME position before 10.
This'll do it.
if "%time:~0,1%" == " " set archivestamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-0%TIME:~1,1%%TIME:~3,2%
if not "%time:~0,1%" == " " set archivestamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%%TIME:~3,2%
 
-Gary Patterson

Open in new window

0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 21883004
While I think you're pretty much covered by the earlier two responses, I'll throw this one-liner out as a possible, and hopefully useful, trick/solution for you at some later point.  Please note, this isn't necessarily a recommendation; more a proof of concept on my part to see if it could be done this way ...
for /F "delims=:./AP tokens=1,2,3,4,5,6" %T in ('set /p=06/27/2008:<nul&time /t') do set archiveSTAMP=%T%U%V%W%X

Open in new window

0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 21883031
... ooops, I inadvertently pasted the runtime syntax.  In a batch file, you'll of course need to double-up on certain percent-symbols like this -


for /f "delims=:./AP tokens=1,2,3,4,5,6" %%T in ('set /p^=%DATE:~4%:^<nul^&time /t') do set archiveSTAMP=%%T%%U%%V%%W%%X

Open in new window

0
 

Author Closing Comment

by:naglpa
ID: 31470283
Thanks guys - perfect answers
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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 …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 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