Solved

Dos batch file - dealing with a nul value

Posted on 2008-06-24
5
1,249 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now