Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dos batch file - dealing with a nul value

Posted on 2008-06-24
5
Medium Priority
?
1,264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 348 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 35

Accepted Solution

by:
Gary Patterson earned 352 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 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