logging batch files

Discussion in 'Microsoft Windows 2000 CMD Promt' started by Joe Vaughan, Apr 1, 2004.

  1. Joe Vaughan

    Joe Vaughan Guest

    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?
     
    Joe Vaughan, Apr 1, 2004
    #1
    1. Advertisements

  2. On Thu, 1 Apr 2004 05:47:52 -0800, "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?


    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
     
    Jerold Schulman, Apr 1, 2004
    #2
    1. Advertisements

  3. Joe Vaughan

    Rob P Guest

    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" <> wrote in message
    news:171cb01c417ef$edeea880$...
    > 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?
     
    Rob P, Apr 1, 2004
    #3
  4. "Rob P" wrote:
    >"Joe Vaughan" <> wrote in message
    >news:171cb01c417ef$edeea880$...
    >> 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
     
    Matthias Tacke, Apr 1, 2004
    #4
  5. Joe Vaughan

    guard Guest

    "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
     
    guard, Apr 2, 2004
    #5
  6. 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" <> wrote in message
    news:c4hj4d$2nt$01$-online.com...
    "Rob P" wrote:
    >"Joe Vaughan" <> wrote in message
    >news:171cb01c417ef$edeea880$...
    >> 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
     
    David Wang [Msft], May 9, 2004
    #6
  7. "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
     
    Matthias Tacke, May 9, 2004
    #7
  8. 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" <> wrote in message
    news:c7la67$a5q$04$-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
     
    David Wang [Msft], May 11, 2004
    #8
  9. "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
     
    Matthias Tacke, May 11, 2004
    #9
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mick

    batch files appear to "hang"

    Mick, Jul 5, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    1
    Views:
    800
    Mark Zbikowski \(MSFT\)
    Jul 10, 2003
  2. Dave

    Troubled Batch files

    Dave, Jul 8, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    1
    Views:
    507
    Jerold Schulman
    Jul 8, 2003
  3. Abhishek

    Batch files

    Abhishek, Jul 10, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    1
    Views:
    586
    Mark V
    Jul 10, 2003
  4. Walter Briscoe

    Re: Batch files referencing localized characters failes...

    Walter Briscoe, Jul 31, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    8
    Views:
    721
    Richard Bonner
    Aug 13, 2003
  5. Replies:
    3
    Views:
    5,246
    Timo Salmi
    Aug 24, 2006
Loading...

Share This Page