naglpa
asked on
Dos batch file - dealing with a nul value
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?
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%%
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
... 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
ASKER
Thanks guys - perfect answers
Open in new window