logging batch files

J

Joe Vaughan

Is there a way to log everything from the beginning of a
batch file to the end while it processes? I want to
capture the progress of batch files in a log. Any idea
how?
 
R

Rob P

Jerold replied to my similar post several days ago with the following
information. It was exactly what I was looking for.


@ECHO ON
call :Logit>>batch.log 2>&1
exit /b 0
:Logit
COMMAND 1
COMMAND 2
ETC



Rob
 
M

Matthias Tacke

Jerold replied to my similar post several days ago with the following
information. It was exactly what I was looking for.

@ECHO ON
call :Logit>>batch.log 2>&1
exit /b 0
:Logit
COMMAND 1
COMMAND 2
ETC
That is a really nice trick from Jerold. This extends it a bit:

@echo off
if /I "%1" NEQ "/log" goto :LogIt
set LogFil=%~dpn0.log
shift
echo on
call :LogIt %1 %2 %3 %4 %5 %6 %7 %8 %9 >%LogFil% 2>&1
goto :eof
:LogIt
::your normal batch code

Drawback of this prependable fragment is, it garbles the %0 value and
the /log argument which enables logging has to be the first.
If the count of needed arguments is greater than 9 it won't work either.
The log file is created in the folder where the batch resides.
The log file name can easily be extended with %date%.

Otherwise prepend your batch with this snippet and you have a logging
option.
 
G

guard

That is a really nice trick from Jerold. This extends it a bit:

@echo off
if /I "%1" NEQ "/log" goto :LogIt
set LogFil=%~dpn0.log
shift
echo on
call :LogIt %1 %2 %3 %4 %5 %6 %7 %8 %9 >%LogFil% 2>&1
goto :eof
:LogIt
::your normal batch code

Drawback of this prependable fragment is, it garbles the %0 value and
the /log argument which enables logging has to be the first.
If the count of needed arguments is greater than 9 it won't work either.
The log file is created in the folder where the batch resides.
The log file name can easily be extended with %date%.

Otherwise prepend your batch with this snippet and you have a logging
option.

Another option is to always log the entire output to a file and then force
the display of any significant event to the console. This will let you know
what's going on during lengthy scripts without having to constantly monitor
the log file.

%.GetLogDate%
%.Log% 2>&1 YourScript.%#LogDate%.log YourScript.cmd

Inside YourScript.cmd, you can use

%.Show% ECHO:Starting [some long process]
Command1
Command2
 
D

David Wang [Msft]

I've found this tip very useful, and here's my addition to what Matthias
posted.

This will preserve the %0 as well as not have a 9 parameter limit, at the
cost that there are two env vars to clear out now -- so the only limitation
is that /log has to be first -- but honestly, that limitation only exists
because this adds logging by prepending a code snippet. It should not be
hard for a batch file who parses parameters to change that first IF to parse
parameters like the batchfile... and then you do have a completely
integrated solution.


@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogFil=%~dpn0.log
set LogCmd=%0
shift
:LogLoop
if /I ?%1? NEQ ?? set LogCmd=%LogCmd% %1&shift&goto :LogLoop
call %LogCmd% >%LogFil% 2>&1
set LogFil=
set LogCmd=
goto :eof
:LogIt

REM the start of your batch file

--
//David
IIS
This posting is provided "AS IS" with no warranties, and confers no rights.
//
Jerold replied to my similar post several days ago with the following
information. It was exactly what I was looking for.

@ECHO ON
call :Logit>>batch.log 2>&1
exit /b 0
:Logit
COMMAND 1
COMMAND 2
ETC
That is a really nice trick from Jerold. This extends it a bit:

@echo off
if /I "%1" NEQ "/log" goto :LogIt
set LogFil=%~dpn0.log
shift
echo on
call :LogIt %1 %2 %3 %4 %5 %6 %7 %8 %9 >%LogFil% 2>&1
goto :eof
:LogIt
::your normal batch code

Drawback of this prependable fragment is, it garbles the %0 value and
the /log argument which enables logging has to be the first.
If the count of needed arguments is greater than 9 it won't work either.
The log file is created in the folder where the batch resides.
The log file name can easily be extended with %date%.

Otherwise prepend your batch with this snippet and you have a logging
option.
 
M

Matthias Tacke

David Wang said:
I've found this tip very useful, and here's my addition to what Matthias
posted.

This will preserve the %0 as well as not have a 9 parameter limit, at the
cost that there are two env vars to clear out now -- so the only limitation
is that /log has to be first -- but honestly, that limitation only exists
because this adds logging by prepending a code snippet. It should not be
hard for a batch file who parses parameters to change that first IF to parse
parameters like the batchfile... and then you do have a completely
integrated solution.


@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogFil=%~dpn0.log
set LogCmd=%0
shift
:LogLoop
if /I ?%1? NEQ ?? set LogCmd=%LogCmd% %1&shift&goto :LogLoop
call %LogCmd% >%LogFil% 2>&1
set LogFil=
set LogCmd=
goto :eof
:LogIt

REM the start of your batch file
Hello David,
thanks for the acknowledgement and the extension. You inspired me to
also enhance my version. No vars, no external call.

@echo off
if /I [%1] NEQ [/log] goto :GoOn
call :LogIt %* >"%~dpn0.log" 2>&1
goto :eof
:LogIt
shift
echo on
:GoOn
::your normal batch code

Working with %* inside the batch would still include the /log option.

This drawback doesn't exist in a change of your version. Handling of
escape chars in arguments is OTOH problematic when setting to vars.

@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:~5% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file
 
D

David Wang [Msft]

How about this:

@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:/log=% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file


So, "/log" can be anywhere in the parameter, and it is not in %* ... of
course, it has that one variable. I guess it's either no new variables but
%* is changed, or one new variable and %* is unchanged. If %*:/log=%
worked, this would be perfect. I happen to have a parameter parser that is
not order specific, so I like allowing /log to be anywhere.

Ahh... one of the fun parts of tweaking with batch scripting to get the best
solution with least tradeoffs. :) This is also one of the reasons I like
this newsgroup now that I've found it.

--
//David
IIS
This posting is provided "AS IS" with no warranties, and confers no rights.
//
David Wang said:
I've found this tip very useful, and here's my addition to what Matthias
posted.

This will preserve the %0 as well as not have a 9 parameter limit, at the
cost that there are two env vars to clear out now -- so the only limitation
is that /log has to be first -- but honestly, that limitation only exists
because this adds logging by prepending a code snippet. It should not be
hard for a batch file who parses parameters to change that first IF to parse
parameters like the batchfile... and then you do have a completely
integrated solution.


@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogFil=%~dpn0.log
set LogCmd=%0
shift
:LogLoop
if /I ?%1? NEQ ?? set LogCmd=%LogCmd% %1&shift&goto :LogLoop
call %LogCmd% >%LogFil% 2>&1
set LogFil=
set LogCmd=
goto :eof
:LogIt

REM the start of your batch file
Hello David,
thanks for the acknowledgement and the extension. You inspired me to
also enhance my version. No vars, no external call.

@echo off
if /I [%1] NEQ [/log] goto :GoOn
call :LogIt %* >"%~dpn0.log" 2>&1
goto :eof
:LogIt
shift
echo on
:GoOn
::your normal batch code

Working with %* inside the batch would still include the /log option.

This drawback doesn't exist in a change of your version. Handling of
escape chars in arguments is OTOH problematic when setting to vars.

@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:~5% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file
 
M

Matthias Tacke

David Wang said:
How about this:

@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:/log=% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file


So, "/log" can be anywhere in the parameter, and it is not in %* ... of
course, it has that one variable. I guess it's either no new variables but
%* is changed, or one new variable and %* is unchanged. If %*:/log=%
worked, this would be perfect. I happen to have a parameter parser that is
not order specific, so I like allowing /log to be anywhere.
I have no time for further checking, but this should give position inde-
pendence of the /log argument. If there are quoted paramters the
replacement of at least the " is neccessary, otherwise the if will give
an error.

@echo off & setlocal
set LogCmd=%*
set LogChk=%*
set "LogChk=%LogChk:"=¬%"
set "LogChk=%LogChk:^=^^%"
set "LogChk=%LogChk:&=^&%"
set "LogChk=%LogChk:<=^<%"
set "LogChk=%LogChk:>=^>%"
set "LogChk=%LogChk:(=^(%"
set "LogChk=%LogChk:)=^)%"
set "LogChk=%LogChk:|=^|%"
if /I "%LogChk%" EQU "%LogChk:/log=%" goto :LogIt
echo on
call %0 %LogCmd:/log=% >"%~dpn0.log" 2>&1
goto :eof
:LogIt
REM the start of your batch file
Ahh... one of the fun parts of tweaking with batch scripting to get the best
solution with least tradeoffs. :) This is also one of the reasons I like
this newsgroup now that I've found it.
ACK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top