Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to capture batch file error msg's?  (Read 7494 times)

0 Members and 2 Guests are viewing this topic.

Panthers_Den

    Topic Starter


    Rookie
    How to capture batch file error msg's?
    « on: May 11, 2010, 08:32:15 AM »
    So in my batch file, I can capture the errorlevel (the error number), but the system is also outputting it's own error msg to the user, how do I capture that msg?

    OS: Windows 2003 Server

    Here is what I get when I run the batch file (I know what's causing it, I just want to log the msg I get back when it's run)

    Code: [Select]
    D:\>cm_build.bat
    System error 1219 has occurred.

    Multiple connections to a server or shared resource by the same user, using more
     than one user name, are not allowed. Disconnect all previous connections to the
     server or shared resource and try again..

    Here is what I get in the Logfile:

    Code: [Select]
    ------------------------------------------------
    -- Batch Ran on Tue 05/11/2010 at  8:57:05.97 --
    ------------------------------------------------
    Error: Tue 05/11/2010 at  8:57:06.12
    Error Location: UnMapped
    Error Msg: error during mapping, Error Number: 2

    Here's a snippet of the code:

    Code: [Select]
    @echo off
    set logLocation="d:\BuildLogs.txt"
    set deletedFoldersList="deletedfolders.txt"

    echo ------------------------------------------------ >> %logLocation%
    echo -- Batch Ran on %date% at %time% -- >> %logLocation%
    echo ------------------------------------------------ >> %logLocation%

    for /f "tokens=2-3" %%i in ('net use ^| find /i "Z:"') do (
      IF %ERRORLEVEL% EQU 0 (
        set local=%%i
        set remote=%%j
        goto :successfulRun
      ) ELSE (
        goto :UnMapped
      )
    )

    :UnMapped
     echo unmapped >> %logLocation%
     net use Z: \\a_drive\a_directory /USER:username password
      IF %ERRORLEVEL% NEQ 0 (
          set errorLoc=UnMapped
          set errorMsg=error during mapping, Error Number: %ERRORLEVEL%
          goto :errorFound
      )
    goto :successfulRun


    :successfulRun
    echo -- Batch run completed successfully on %date% at %time% -- >> %logLocation%
    goto :eof

    :errorFound
    echo Error: %date% at %time% >> %logLocation%
    echo Error Location: %errorLoc% >> %logLocation%
    echo Error Msg: %errorMsg% >> %logLocation%
    goto :eof

    So I'm curious if there's a way to put that system msg from the dos prompt:

    System error 1219 has occurred.

    Multiple connections to a server or shared resource by the same user, using more
     than one user name, are not allowed. Disconnect all previous connections to the
     server or shared resource and try again..

    into the log files? Being able to do caputer the dos system messages will go a long way in helping me to debug this batch file when it's run from scheduled tasks while the user is logged off.

    Thx!

    gpl



      Apprentice
    • Thanked: 27
      Re: How to capture batch file error msg's?
      « Reply #1 on: May 11, 2010, 09:15:09 AM »
      yes, it is more than possible, it is easy!
      the redirection symbol takes screen output and puts it into a file, eg
      Code: [Select]
      net use Z: \\a_drive\a_directory /USER:username password>logfile
      the single > will blank out the current contents of the file, using >> will append it to the end.

      The End

      Well, not quite, sometimes you cannot redirect the output like this because it is sent via the stderr channel, there is a fix for this, quote the channel number
      Code: [Select]
      net use Z: \\a_drive\a_directory /USER:username password 2>logfileI havent used the redirection of the error channel, so I am not sure if 2>>logfile would work! (if it doesnt, then use 2> to redirect to a temp file and append the temp file to your logfile)

      this is a useful resource http://ss64.com/nt/syntax-redirection.html

      Panthers_Den

        Topic Starter


        Rookie
        Re: How to capture batch file error msg's?
        « Reply #2 on: May 11, 2010, 09:42:11 AM »
        yes, it is more than possible, it is easy!
        the redirection symbol takes screen output and puts it into a file, eg
        Code: [Select]
        net use Z: \\a_drive\a_directory /USER:username password>logfile
        the single > will blank out the current contents of the file, using >> will append it to the end.

        The End

        Well, not quite, sometimes you cannot redirect the output like this because it is sent via the stderr channel, there is a fix for this, quote the channel number
        Code: [Select]
        net use Z: \\a_drive\a_directory /USER:username password 2>logfileI havent used the redirection of the error channel, so I am not sure if 2>>logfile would work! (if it doesnt, then use 2> to redirect to a temp file and append the temp file to your logfile)

        this is a useful resource http://ss64.com/nt/syntax-redirection.html

        Yea, I had tried

        Code: [Select]
        net use Z: \\a_drive\a_directory /USER:username password>>logfile
        which didn't work. I didn't think about trying 2>> (actually didn't even know what 2> did until now), so I just did that one and it worked, even with >>  :-)

        Code: [Select]
        net use Z: \\a_drive\a_directory /USER:username password 2>>logfile
        Thank you! :-)

        Glad it was an easy change. Thanks for the link too, that'll come in handy for sure.

        gpl



          Apprentice
        • Thanked: 27
          Re: How to capture batch file error msg's?
          « Reply #3 on: May 11, 2010, 09:44:53 AM »
          Thank you for the confirmation of 2>> - I thought it might work but had had no reason to try it before.

          That site has many very useful examples of batch usage

          Helpmeh



            Guru
          • Roar.
          • Thanked: 122
            • Yes
            • Yes
          • Computer: Specs
          • Experience: Familiar
          • OS: Windows 8
          Re: How to capture batch file error msg's?
          « Reply #4 on: May 11, 2010, 12:57:46 PM »
          By default, batch sees > (or >>) as 1> (or 1>>). That is what you see on screen as normal messages, 2> or 2>> will output error messages.
          Where's MagicSpeed?
          Quote from: 'matt'
          He's playing a game called IRL. Great graphics, *censored* gameplay.

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 836
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 7
          Re: How to capture batch file error msg's?
          « Reply #5 on: May 11, 2010, 08:24:32 PM »
          By default, batch sees > (or >>) as 1> (or 1>>). That is what you see on screen as normal messages, 2> or 2>> will output error messages.
          1 is the standard output stream.
          2 is the standard error stream.

          >,>> , <, and | can have a stream number prefixed to them. for example, if you use:

          program.exe 2>&1 | program2.exe

          then the standard error and standard output  of program.exewill both be piped to program2 as it's standard input.
          I was trying to dereference Null Pointers before it was cool.

          Panthers_Den

            Topic Starter


            Rookie
            Re: How to capture batch file error msg's?
            « Reply #6 on: May 12, 2010, 05:40:15 AM »
            program.exe 2>&1 | program2.exe


            Why the &1, what does that do?

            Sidewinder



              Guru

              Thanked: 121
            • Experience: Familiar
            • OS: Windows 7
            Re: How to capture batch file error msg's?
            « Reply #7 on: May 12, 2010, 10:30:41 AM »
            Why the &1, what does that do?

            For the answer to that and any other questions about redirection, check out this article

             8)
            It has been my experience that folks who have no vices have very few virtues

            - Abraham Lincoln

            Panthers_Den

              Topic Starter


              Rookie
              Re: How to capture batch file error msg's?
              « Reply #8 on: May 12, 2010, 01:15:54 PM »
              For the answer to that and any other questions about redirection, check out this article

               8)

              oh that's a helpful article, thanks :)