Need leading zero for batch script using %time% variable

Solution 1:

A very simple way is to just replace the leading space with zero:
echo %TIME: =0%
outputs:
09:18:53,45

Solution 2:

My Solution was to use the following idea:

SET HOUR=%TIME:~0,2%
IF "%HOUR:~0,1%" == " " SET HOUR=0%HOUR:~1,1%

Solution 3:

Similar idea to Dennis' answer. The problem is that the width of %time% is always the same, so it inserts a space in the beginning instead of returning a shorter string.

You can get rid of that with for:

for /f "delims= " %x in ("%time%") do set T=0%x

The rest is more or less the same, then.


Solution 4:

Using Jesse's Contribution, I just created a variable with the modified output. Then I reference that variable to build the hour portion.

set NantTime=%time: =0%
nant\bin\nant.exe -nologo+ -debug+ -verbose+ -D:project.config=debug /f:build\default.build -l:logs\architect-build-%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%NantTime:~0,2%-%time:~3,2%-%time:~6,2%.log 
pause

With the original source:

set hour=%time: =0%
set logfile=C:\Temp\robolog_%date:~-4%%date:~4,2%%date:~7,2%_%hour:~0,2%%time:~3,2%%time:~6,2%.log

Thanks Jesse. I would have voted if I had the reputation points.