Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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
0
LuckyLucks
Asked:
LuckyLucks
  • 6
  • 5
  • 2
1 Solution
 
Steve KnightIT ConsultancyCommented:
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
0
 
Bill PrewCommented:
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
0
 
LuckyLucksAuthor 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
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Steve KnightIT ConsultancyCommented:
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

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

~bp
0
 
Bill PrewCommented:
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
0
 
LuckyLucksAuthor 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.
0
 
Bill PrewCommented:
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
0
 
LuckyLucksAuthor 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.
0
 
Bill PrewCommented:
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
0
 
LuckyLucksAuthor 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.
0
 
Bill PrewCommented:
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now