Solved

Dos batch file - dealing with a nul value

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

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…
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 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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 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