|
Answer» 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!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.htmlQuote from: gpl 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
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.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 usageBy 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.Quote from: Helpmeh on May 11, 2010, 12:57:46 PMBy 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. Quote from: BC_Programmer on May 11, 2010, 08:24:32 PMprogram.exe 2>&1 | program2.exe
Why the &1, what does that do?Quote from: Panthers_Den on May 12, 2010, 05:40:15 AMWhy the &1, what does that do?
For the answer to that and any other questions about redirection, check out this article
Quote from: Sidewinder on May 12, 2010, 10:30:41 AMFor the answer to that and any other questions about redirection, check out this article
oh that's a helpful article, thanks
|