Solved

Error ouput of failed batch script to a log

Posted on 2014-02-26
13
329 Views
Last Modified: 2014-03-05
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
Comment
Question by:LuckyLucks
  • 6
  • 5
  • 2
13 Comments
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:LuckyLucks
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 

Author Comment

by:LuckyLucks
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Did you create the C:\Logs folder?

~bp
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:LuckyLucks
Comment Utility
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
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
 

Author Comment

by:LuckyLucks
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:LuckyLucks
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select menu in BATCH and then hit ENTER 36 61
Vbscript 8 65
Batch File to find and replace a string 15 89
Windows batch files 3 23
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now