Error ouput of failed batch script to a log

LuckyLucks
LuckyLucks used Ask the Experts™
on
Hi EEE,

  I wanted to output the error messages from a batch file to a log file. I will be running this in production as callout from a SSIS package so it probably shouldnt be displaying anything on the console (not sure if echo on) will do this and hence should be off. I would like to get the individual command captured in the log as well if possible.


Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can wrap commands in a ( ) and use the > commands to redirect all the output, and 2> for any errors, e.g. try:

(
echo %date% %time% Log started
copy x y
echo %date% %time% Done the copy
some command
echo %date% %time% Log completed
) > c:\logs\log.txt 2>&1

the last bit means sends normal output to that log.txt file and errors to the normal output which therefore ends up in the log too.

if you use echo off the commands will be hidden but the output will show.  if you use echo on then the commands will show too, good for debugging.

Steve
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
When you run the BAT file from SSIS, you can add the logging to the command line like this:

myscript.bat >c:\temp\logfile.txt 2>&1

That will send any normal or error output from the script to the specified log file for review.

~bp

Author

Commented:
If I wanted to do it within the batch script as below, it doesnt seem to work...could it be the additional ( ?


(

REM Define locations of files and folders
set ControlFile=batch_input.txt
set BaseDir=E:\Alphabetized

REM Read each line of the control file, pass it to a subroutin for processing
for /f "usebackq tokens=1-4 delims=," %%A in ("%ControlFile%") do (
  call :DoIt %%A %%B %%C %%D
)

REM Leave the script now
exit /b

REM Subroutine to break up the control line, and then copy the file as specified
:DoIt
  if not exist "%BaseDir%\" mkdir "%BaseDir%"
  if not exist "%BaseDir%\%~4\" mkdir "%BaseDir%\%~4"
  if not exist "%BaseDir%\%~4\%~3\" mkdir "%BaseDir%\%~4\%~3"
  copy /Y "%1" "%BaseDir%\%~4\%~3\%~2"
  exit /b

) > c:\logs\log.txt 2>&1
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Commented:
You would want the ( ) around the bit of code that you run.  As it stands you run the first bit which then calls the subroutine part.  Try it around like this:

Steve

(

REM Define locations of files and folders
set ControlFile=batch_input.txt
set BaseDir=E:\Alphabetized

REM Read each line of the control file, pass it to a subroutin for processing
for /f "usebackq tokens=1-4 delims=," %%A in ("%ControlFile%") do (
  call :DoIt %%A %%B %%C %%D
)

) > c:\logs\log.txt 2>&1 

REM Leave the script now
exit /b

REM Subroutine to break up the control line, and then copy the file as specified
:DoIt
  if not exist "%BaseDir%\" mkdir "%BaseDir%"
  if not exist "%BaseDir%\%~4\" mkdir "%BaseDir%\%~4"
  if not exist "%BaseDir%\%~4\%~3\" mkdir "%BaseDir%\%~4\%~3"
  copy /Y "%1" "%BaseDir%\%~4\%~3\%~2"
  exit /b

Open in new window

Author

Commented:
It just gives me an error in the log that : "The System can not find the file". If I take the () and the output line out, it runs perfectly fine...not sure what the issue is.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Did you create the C:\Logs folder?

~bp
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Or you could  use the technique I showed in http:#a39890451, which is nice because it allows you to capture the output to a file, or see it on the screen without modifying the BAT file, just by changing the command line you invoke it with.

~bp

Author

Commented:
1) Yes, created the C:/logs folder prior to run
2) I had a change of course and will not be running the batch from the SSIS, so unfortunately wont be able to wrap it around the SSIS call out to the batch.
IT / Software Engineering Consultant
Top Expert 2016
Commented:
Okay, seems like only a couple of places you could get that error,  Here's a version with a bit more error checking to see if the script can't find the control file, or any of the data files listed in the control file.

@echo off

REM Define locations of files and folders
set ControlFile=batch_input.txt
set BaseDir=E:\Alphabetized
set LogFile=c:\logs\log.txt

(
  REM Make sure the control file exists
  if not exist "%ControlFile%" (
    echo *ERROR* Control file [%ControlFile%] was not found!
    exit /b
  )  

  REM Read each line of the control file, pass it to a subroutin for processing
  for /f "usebackq tokens=1-4 delims=," %%A in ("%ControlFile%") do (
    call :DoIt %%A %%B %%C %%D
  )
) > "%LogFile%" 2>&1 

REM Leave the script now
exit /b

REM Subroutine to break up the control line, and then copy the file as specified
:DoIt
  REM Make sure this file actually exists, if so copy it
  if exist "%1" (
    if not exist "%BaseDir%\" mkdir "%BaseDir%"
    if not exist "%BaseDir%\%~4\" mkdir "%BaseDir%\%~4"
    if not exist "%BaseDir%\%~4\%~3\" mkdir "%BaseDir%\%~4\%~3"
    copy /Y "%1" "%BaseDir%\%~4\%~3\%~2"
  ) else (
    echo *WARNING* Data file [%1] was not found.
  )
  exit /b

Open in new window

~bp

Author

Commented:
I think the error may be occuring in the following steps as well :

1. creating the subdir at step
 if not exist "%BaseDir%\%~4\%~3\" mkdir "%BaseDir%\%~4\%~3"

2. copying the file over due to the renaming of the destination file
copy /Y "%1" "%BaseDir%\%~4\%~3\%~2"

How can I catch these errors?

In the test sample using code above there is no issues as I am supplying the source file whoch I know exists and its being copied over.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Any of those items that generated an error (bad drive letter, permissions error, etc) would log that error to the log.txt log file.

So you are running the above and it does not write anything to the log.txt file, but you think it is failing?  What are you looking at to determine that it didn't work?

~bp

Author

Commented:
I looked at the log file and it only had the following lines repeated:
1 file(s) copied.

There was no error and warnings. Only 0 files copied. The system cannot find the specified path. And no mention of which files caused this when either of the two scenarios listed above occured.


I proceeded to compare the contents of All and Alphabetized and noticed there were files that did not get copied to the Alphabetized. Looking at the ones that didnt transfer, I tried to copy them manually using the batch copy command exactly as in the script...I found that the subfolder has a space or something that looks like it at the end of its name, so when the full copy command is used it gives an error


copy /Y "Z:\process\abc1.pdf" "E:\Alphabetized\B\Business Herald  ExtraDaily Daily News \Business Herald  ExtraDaily Print_0342-2000
50223_20060423__5567.pdf"

The system can not find the path specified
0 file(s) copied.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Have you tried renaming the folder in question in Windows Explorer and retyping the whole name, so that any odd characters get replaced by normal characters?  It sounds like you may have a carriage return character of line feed character in the folder name which could cause problems.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial