Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 214
Chad Dybdahl Avatar asked May 06 '13 15:05

Chad Dybdahl


People also ask

How do I run a batch file repeatedly?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.

What does %% do in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


2 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%
like image 150
foxidrive Avatar answered Oct 21 '22 10:10

foxidrive


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.

like image 24
Christian Nagel Avatar answered Oct 21 '22 10:10

Christian Nagel