Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

Error level reporting not working in this batch job...

Down below is a batch job that isn't reporting errors if a file is in use and cannot be copied over to the destination servers.  The err log file is never generated...  Can anyone determine the problem?

Thanks.


@ECHO OFF
CALL v:\windows\BATCH\windowsLogStart.CMD STARTED %0
ECHO **********************************************************************>>V:\windows\windowsJOBS\glove\logs\gloveProd.log
CLS



@ECHO OFF
echo Date: %date% Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.log
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.log

CLS

ECHO Checking if file exist.

if exist "T:\hockey\football\glove\Prod\glove.exe" goto Found

:NotFound

rem What to do when file is Not Found

echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo File is unavailable >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto :EOF

:Found

rem What to do when file is Found

:Next_Step
echo Source Server on T:\hockey\football\glove\Prod\glove.exe>> V:\windows\windowsJOBS\glove\logs\gloveProd.log
dir "T:\hockey\football\glove\Prod\glove.exe" |find "glove.exe">> V:\windows\windowsJOBS\glove\logs\gloveProd.log
CLS
for %%K in ( Server1 Server2 Server3 Server4 ) do CALL :routine %%K
goto :done

:routine
:: %1 comes from the call line, not the command line
set zserver=%1

ECHO.
ECHO PREPARING TO COPY FILES : %zserver%
@echo off
ping -n 10  \\127.0.0.1 >NUL

echo. >>V:\windows\windowsJOBS\glove\logs\gloveProd.log
echo Destination Server:%zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.log
@echo on
xcopy "T:\hockey\football\glove\Prod\glove.exe" \\%zserver%\user01\windows\glove\prod\ /Y /Z /D
@echo off
dir \\%zserver%\user01\windows\glove\prod\glove.exe |find "glove.exe">> V:\windows\windowsJOBS\glove\logs\gloveProd.log
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.log

if errorlevel 5 goto diskwrite
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 1 goto abort2
if errorlevel 0 goto done

:diskwrite
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo DiskWrite error occurred during copy to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit

:lowmemory
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Insufficient memory to copy files, invalid drive or command-line syntax while copying to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit

:abort
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo CTRL+C pressed during copy to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit

:abort2
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Error during copy to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit


:exit
goto :EOF


:done

for %%J in ( Server5 Server6 Server7 ) do CALL :copynext %%J
goto :Finished


:copynext
:: %1 comes from the call line, not the command line
set srvnme=%1

ECHO.
ECHO PREPARING TO COPY FILES: %srvnme%
@echo off
ping -n 10  \\127.0.0.1 >NUL

echo. >>V:\windows\windowsJOBS\glove\logs\gloveProd.log
echo Destination Server:%srvnme% >> V:\windows\windowsJOBS\glove\logs\gloveProd.log
@echo on
net use \\%srvnme%\Eback$ /user:cmi\%username%
xcopy "T:\hockey\football\glove\Prod\glove.exe" \\%srvnme%\Eback$\ZENAPPS\W2KAPPS\DMIAPPS\glove\prod\ /Z /D
@echo off
dir \\%srvnme%\Eback$\ZENAPPS\W2KAPPS\DMIAPPS\glove\prod\glove.exe |find "glove.exe">> V:\windows\windowsJOBS\glove\logs\gloveProd.log
V:\windows\windowsJOBS\glove\logs\gloveProd.log
echo. >> V:\windows\windowsJOBS\AVM\logs\AVMProd.log

if errorlevel 5 goto diskwrite
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 1 goto abort2
if errorlevel 0 goto done

:diskwrite
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo DiskWrite error occurred during copy to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit

:lowmemory
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Insufficient memory to copy files, invalid drive or command-line syntax while copying to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit

:abort
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo CTRL+C pressed during copy to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit

:abort2
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Date: %date% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Time: %time% >>V:\windows\windowsJOBS\glove\logs\gloveProd.err
echo Error during copy to %zserver% >> V:\windows\windowsJOBS\glove\logs\gloveProd.err
goto exit

:exit
goto :EOF


:Finished

echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.log
echo Completed Job >>V:\windows\windowsJOBS\glove\logs\gloveProd.log
ECHO **********************************************************************>>V:\windows\windowsJOBS\glove\logs\gloveProd.log
echo. >> V:\windows\windowsJOBS\glove\logs\gloveProd.log

CALL v:\windows\BATCH\windowsLogEnd.CMD FINISHED %0
EXIT
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

You have to understand how things work - there are TWO outputs, Standard Output (StdOut) and Standard Error (StdErr).  Standard output (successes, informational output) is typically written to StdOut (which is represented by a 1 and can be omitted when redirecting output as it is assumed).  ERRORS are reported to StdErr, represented by a 2.  So you could do this, for example:

echo info 1>> success.log 2>> failure.log

which will put all success/info messages to a file called success.log and all failures/error messages to failure.log.

OR you could do this:

echo info 1>> script.log 2>>&1

which would put ALL output from both standard error and standard out to the same file.

In short, append 2>>&1 to every line you want to record in the file.
Also, it would be a bit easier to read if you put the log file into a variable - for example:

SET LogFile=V:\Windows\WindowsJOBS\glove\logs\gloveProd.err

Then replace the >>V:\Windows\WindowsJOBS\glove\logs\gloveProd.err
with
>>%logfile%
Avatar of mystikal1000
mystikal1000

ASKER

So for example, I would do this?

echo. >> %logfile%
echo. >> %logfile%
echo Date: %date% >>%logfile%
echo Time: %time% >>%logfile%
echo DiskWrite error occurred during copy to %zserver% >> %logfile%
Kinda... you still need to take standard error if you want to get the errors - so it would look like this:

SET LogFile=V:\Windows\WindowsJOBS\glove\logs\gloveProd.err
echo. >> %logfile% 2>>&1
echo. >> %logfile% 2>>&1
echo Date: %date% >>%logfile% 2>>&1
echo Time: %time% >>%logfile% 2>>&1
echo DiskWrite error occurred during copy to %zserver% >> %logfile% 2>>&1
Thanks I will try that...
I think the issue is that your errorcheck for the xcopy results occur way after the xcopy. The errorlevel you're checking is probably for the dir or find command in the line above.

Could you try moving the errorlevel stuff up to the line underneath the xcopy?

HTH,

Wallsy
Your right Wallsy, but where should I put the Dir and find command so it will output the same?
forgive me if stepping on toes,  I think Wallsy is saying to put the errorlevel checking before anything else after what you want to check for errors.  If you want the code based on xcopy errorlevel it's like this:
++++++
xcopy "T:\hockey\football\glove\Prod\glove.exe" \\%zserver%\user01\windows\glove\prod\ /Y /Z /D
if errorlevel 5 goto diskwrite
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 1 goto abort2
if errorlevel 0 goto done
+++++

then if you want the dir when it's good,  put it under the label:
:done


ALSO: (separate item below)
ASKER CERTIFIED SOLUTION
Avatar of K_2K
K_2K

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No toes stepped on at all K_2K!

I must have missed the reply, because I completely forgot about this question... Some of these sods at my work have decided I need to EARN my obscene rate!  ;)

Wallsy