PC Review


Reply
Thread Tools Rate Thread

logging batch files

 
 
Joe Vaughan
Guest
Posts: n/a
 
      1st Apr 2004
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?
 
Reply With Quote
 
 
 
 
Jerold Schulman
Guest
Posts: n/a
 
      1st Apr 2004
On Thu, 1 Apr 2004 05:47:52 -0800, "Joe Vaughan" <(E-Mail Removed)>
wrote:

>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?


See MTEE at tip 5921 in the 'Tips & Tricks' at http://www.jsiinc.com

Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Reply With Quote
 
 
 
 
Rob P
Guest
Posts: n/a
 
      1st Apr 2004
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



"Joe Vaughan" <(E-Mail Removed)> wrote in message
news:171cb01c417ef$edeea880$(E-Mail Removed)...
> 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?



 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      1st Apr 2004
"Rob P" wrote:
>"Joe Vaughan" <(E-Mail Removed)> wrote in message
>news:171cb01c417ef$edeea880$(E-Mail Removed)...
>> 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?


>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.

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
Reply With Quote
 
guard
Guest
Posts: n/a
 
      2nd Apr 2004
"Matthias Tacke" wrote
> "Rob P" wrote:
> >"Joe Vaughan" wrote
> >> 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?

>
> >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.


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
 
Reply With Quote
 
David Wang [Msft]
Guest
Posts: n/a
 
      9th May 2004
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.
//
"Matthias Tacke" <(E-Mail Removed)> wrote in message
news:c4hj4d$2nt$01$(E-Mail Removed)-online.com...
"Rob P" wrote:
>"Joe Vaughan" <(E-Mail Removed)> wrote in message
>news:171cb01c417ef$edeea880$(E-Mail Removed)...
>> 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?


>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.

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm




 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      9th May 2004
"David Wang [Msft]" wrote:

>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


--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
Reply With Quote
 
David Wang [Msft]
Guest
Posts: n/a
 
      11th May 2004
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.
//
"Matthias Tacke" <(E-Mail Removed)> wrote in message
news:c7la67$a5q$04$(E-Mail Removed)-online.com...
"David Wang [Msft]" wrote:

>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


--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm


 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      11th May 2004
"David Wang [Msft]" wrote:

>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

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling multiple batch files from within a batch file yawnmoth Windows XP General 3 26th May 2008 06:47 PM
recording or logging command line entries or batch files Joe Vaughan Microsoft Windows 2000 Security 1 1st Apr 2004 05:17 PM
batch command doesn't work if called by another batch jonefer Windows XP General 3 20th Nov 2003 06:35 PM
non functional command when batch calls a batch jonefer Windows XP Accessibility 0 20th Nov 2003 12:55 AM
Photo Editor Batch supporting batch compression JPG files 29150 Freeware 2 23rd Jun 2003 06:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:32 AM.