Before I provide some suggestions, I want to mention that you should put the redirection in front of the command rather than behind in order to avoid trailing spaces or other trouble, and to put quotation marks around any paths in order to avoid trouble with white-spaces or special characters, like this:
>>"output.log" echo %date%,%time% - %~1
But now for the solutions...
You could make use of command echos and a custom prompt holding date/time information:
@echo off
>> "output.log" echo %date%,%time% - %~1
>> "output.log" call :sub
>> "output.log" echo %date%,%time% - %~1
exit /b
:sub
prompt $D,$T$S-$S
echo on
command1
command2
...
commandN
@echo off
prompt
So you will get the following log entry:
28-Jun-2016,16:33:31.16 -
28-Jun-2016,16:33:31.16 - command1
logssssss <output of command1>
28-Jun-2016,16:33:31.82 - command2
logssssss <output of command2>
28-Jun-2016,16:33:32.28 - ...
logssssss <output of ...>
28-Jun-2016,16:33:36.07 - commandN
logssssss <output of commandN>
28-Jun-2016,16:33:36.07 -
All the empty lines are caused by the command echoing.
Note that the date/time information depends on the current locale settings.
To get rid of the command echoes and just receive the command outputs with date/time preceded, you could use a for /F
loop to capture the command outputs and force the date/time prompt prefix with the rem
command. But you cannot call a sub-routine like call :sub
then, because commands within for /F
are executed in another cmd
instance which is not run in a batch file context; also any environment changes within that cmd
instance is not reflected in the instance where this batch file is running in. Furthermore, you will receive the log string of the entire :sub
section once when it has finished rather than receiving it after every command.
If all this is no problem for you, you might want to use the following code:
@echo off
if /I "%~1"=="Recursive" goto :sub
prompt $D,$T$S-$S
>> "output.log" echo %date%,%time% - %~1
>> "output.log" (echo on & for /F delims^=^ eol^= %%# in ('"%~f0" Recursive') do rem/ %%#)
@echo off
>> "output.log" echo %date%,%time% - %~1
prompt
exit /b
:sub
command1
command2
...
commandN
The call :sub
command is replaced by a conditional goto :sub
command. When the batch file is called by for /F
, an argument Recursive
is delivered; if this string is recognised, execution is continued at the :sub
section.
The log entry will look like the following:
28-Jun-2016,16:33:31.16 -
28-Jun-2016,16:33:31.16 - rem/ logssssss <output of command1>
28-Jun-2016,16:33:31.82 - rem/ logssssss <output of command2>
28-Jun-2016,16:33:32.28 - rem/ logssssss <output of ...>
28-Jun-2016,16:33:36.07 - rem/ logssssss <output of commandN>
28-Jun-2016,16:33:36.07 -
To get rid of the empty lines, replace the command line...:
>> "output.log" (echo on & for /F delims^=^ eol^= %%# in ('"%~f0" Recursive') do rem/ %%#)
...by this one...:
>> "output.log" (for /F delims^=^ eol^= %%$ in ('^(for /F delims^^^=^^^ eol^^^= %%# in ^('"%~f0" Recursive'^) do rem/ %%#^)') do echo %%$)
...and remove the second line @echo off
immediately after this.