Pesquisar este blog

sexta-feira, 29 de setembro de 2017

How can i use the variable %TIME:~0,2% in a batch file so that times with a leading space do not cause errors?


I am trying to run a batch file, which runs an XSLT transformation against an XML file and writes out a second XML file.
This XML filename is determined by the following line in the batch file:
 ICS_%DATE:~-4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%_%TIME:~3,2%_DATA.xml
When the time has a leading space (that is, any time before 10:00 am), the variable %TIME:~3,2% returns a result with a leading space, which causes the filename to be truncated. The result file is empty.
If I run the batch after 10:00am, everything works fine. How can I generate a value similar to %TIME:~3,2%that works before 10:00am?




    
Couldn't you use a variable and then have an IF %TIME% < 10 add a 0 to the front (or remove if you need to)? If not you could try adding something before the time to force it to stay there. – David Starkey May 6 '13 at 15:15
    
It actually appears, after further testing, that a space is being added by the %TIME:~0,2% expression, and this is what is causing the error. This makes a lot more sense, because the value for the month is 05 right now, and that works fine. – Chad Dybdahl May 6 '13 at 16:08

7 Answers



This will solve the space in the name issue, and replace it with a zero so it sorts correctly in a list.
set name=ICS_%DATE:~-4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%_%TIME:~3,2%_DATA.xml
set name=%name: =0%



    
This is best solution I guess. I only wonder is it possible to rewrite it somehow in one-line without assigning value to variable? For example, I simply need to echo %time:~0,2%_%time:~3,2% and replace spaces with 0. – DarkSide Nov 19 '14 at 12:20

How about this one ?
@ECHO OFF
FOR /F "tokens=1-4 delims=., " %%i IN ('DATE /t') DO SET cpdate=%%k_%%j_%%i
FOR /F "tokens=1-4 delims=:"  %%b IN ('TIME /T') DO SET cptime=%%b_%%c

set filename=blabla_%cpdate%_%cptime%_%TIME:~-5,2%.xml

echo %filename%
The locale here is german, so you might have to adjust the order in "set cpdate..." for your needs.




    
Thanks for your response. The result of your suggestion is:blabla__05/06/2013_Mon_07_09 AM_55.xml, which is not what is desired. The desired format of the filename is blabla_2013_05_06_07_09_DATA.xml. – Chad Dybdahl May 6 '13 at 16:10


ok, in germany we separate the numbers in the date with a "." character. For me it produces blabla_2013_05_06_21_19_59.xml Try using FOR /F "tokens=1-3 delims=/., " %%i IN ('DATE /t') DO SET cpdate=%%k_%%j_%%i in the 2nd line. Post the output of date /t and time /t if it still does not work for you.
 I'll see if I can help. – Christian Nagel May 6 '13 at 19:22

Setting a variable for the hour value as follows solves the problem for me: FOR /F "tokens=* delims= " %%a IN ("%TIME:~0,2%") DO SET hour=%%a



    
Can you mark this as Accepted if you've solved your problem? I'm still confused as to whether your question has been answered correctly or not. – Nate Hekman May 6 '13 at 16:25
    
This doesn't do anything with/for/against leading zeros. – Endoro May 6 '13 at 16:26
    
It wasn't the leading zeroes. It was leading spaces. This does solve my problem, but I cannot accept it as an answer for two days. – Chad Dybdahl May 6 '13 at 16:35
    
In that case, all you needed to to was to enclose the filename in double-quotes. – Magoo May 6 '13 at 17:09
True, that would have prevented the error, but would not have resulted in the correct filename. – Chad Dybdahl May 6 '13 at 17:42

(This is an old question but this answer could be of help for somebody else.)
Using %time: =% gets rid of the spaces in the time variable. Example:
C:\>echo %time%
 04:00:00,00

C:\>echo %time: =%
04:00:00,00
You could save the time variable to another using this method and then work from that one:
set "tim2=%time: =%"




I use a simple trick to do this:
set h=1%TIME:~0,2%
set /a h=%h%-100




REM This .bat script gives you a parameter timetext with a zero instead of a space
@echo off

set timetext=%time:~0,2%%time:~3,2%
echo TIME BEFORE WITH SPACE %timetext%

set digit1=%time:~0,1%

if "%digit1%"==" " set digit1=0

set timetext=%digit1%%time:~1,1%%time:~3,2%
echo TIME AFTER WITH ZERO.. %timetext%

pause




This maybe more helpful in Dos Batch ~~ try this
if "%time:~0,1%" == " " (set dtstamp=0%time:~1,1%) ELSE set dtstamp=%time:~0,2%
echo dtstamp=%date:~6%%date:~3,2%%date:~0,2%_%dtstamp%%time:~3,2%

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.