Link to home
Start Free TrialLog in
Avatar of LuckyLucks
LuckyLucks

asked on

Error ouput of failed batch script to a log

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
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Bill Prew
Bill Prew

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
Avatar of LuckyLucks

ASKER

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
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

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.
Did you create the C:\Logs folder?

~bp
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
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.
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
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.
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