Solved

Dos batch file - dealing with a nul value

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

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 35

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

726 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