155 pts.
 Customizing the logging of a Wiindows batch file
I am trying to customize the piped output of a batch file. I am currently using Robocopy to copy/move files to various directories based on the file naming convention. This part is working just fine. I am having problem though with customizing the logging of it. I have the batch file running as a service under Windows 2003R2 so it is running continuously. I need to include the file name, the destination directory it is being copied to and the time of day it was copied there. These don't appear to be options within Robocopy. I have since tried the same script using the copy and XCopy commands within Windows and am unable to get this working with these either. Ultimatly, I would like to have the log file as a CSV or TAB file to be able to further massage the data in Excel or a DB of some kind. Any help with this level of customization would be appreciated. I do not consider myself a scripter/coder by any means. Thanks

Software/Hardware used:
ASKED: June 5, 2009  8:11 PM
UPDATED: June 25, 2009  4:43 PM

Answer Wiki:
Logging in a batch file is quite easy (unless I am misunderstanding your question): Echo anything that you want logged, and add to the end of the line ">> yourfile.file" (without quotes). This will write a new line in the text file every time the batch is run. Use "> yourfile.file" to rewrite the file each time it runs. This can be inserted in nearly every process that runs from batch. You can also customize with your own text like "echo "Maintenance now starting" %t > myfile.txt Hope this helps! -Schmidtw
Last Wiki Answer Submitted:  June 11, 2009  2:49 pm  by  Schmidtw   11,205 pts.
All Answer Wiki Contributors:  Schmidtw   11,205 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Robocopy has a logging switch that includes the command options, list of files copied, status of the copy of each file, start and stop time of the copy session and a summary of the copy results. You can massage the file name of the log as desired.

Can you give an example of your copy and what a log filename should look like?

Adding the date time into a file name is covered by alot of people, here is one example:

 3,310 pts.

 
 3,310 pts.

 

What I am trying to get to is a line for each item copied/moved broken down as follows:
Date_Time(copied/moved)_filename_DestinationDirectory

My current syntax is
Robocopy:
C:Utilitiesrobocopy C:TEMPPageSorting C:TEMPPagesSent na*.pdf /R:3 /W:3 /MOV /NP /NS /IS /FP /NDL /NJH /NJS >>TodaysPages.log

XCopy
date /T >>C:TEMPTodaysPages.log
time /T >>C:TEMPTodaysPages.log
xcopy C:TEMPna*.pdf C:TEMPPagesSent /C /Y >>TodaysPages.log

 155 pts.

 

My suggestion is to use Robocopy’s built in logging and to leave the job header and job summary enabled. While there is more information in these items, they also have the information necessary to perform basic troubleshooting should an issue occur. The job header includes start date and time and the job summary includes completion date and time. Using the log append allows you to keep prior information. Or you can roll the log in your script as you require.

C:\Utilities\robocopy C:\TEMP\PageSorting C:\TEMP\PagesSent na*.pdf /R:3 /W:3 /MOV /NP /NS /IS /FP /NDL /LOG+:C:\Temp\TodaysPages.log

Once you have the log, you can strip information out as required with a for loop and the various string utilities (findstr, find, grep). Example:

FOR /F “tokens=3″ %a IN (‘findstr /i /r “[a-zA-Z0-9].cmd” C:\Temp\TodaysPages.log’) DO @ECHO %a>>C:\Temp\_MySrt.lst

You can also strip out date and time stamps using a for loop and filter them into your results.

You can also use the build in DATE and TIME variables to manually flag you log file.

 3,310 pts.

 

Thanks for this info. I would like to eliminate the need for post-parsing of the log file as it may be used for troublshooting in real time. The current batch file is running as a Windows service, running 7 X 24 with many files being moved during our production hours each day. I am looking for a way to make this as clean and as lean as possible at the creation of the log entries appended to the log file throughout the day. Perhaps taking this a step back to simplify things might help me.

Is there a way to do a simple command to copy a file (using any tool/app) that will create a simple entry in a log file to have a single line item for that file stating Date, Time, Filename, destination directory. If we can write the log file cleanly, there should be little to no post parsing of the log file needed.

 155 pts.

 

Here’s a script file I have in my library. Maybe this will do the job for you.

@echo off
::LogEntryDemo.bat
REM Jeffery Hicks  jhicks@sapien.com
REM This script doesn't really do much other than demonstrate
REM how to use the LogEntry routine to create a 
REM log file with a timestamped entry for each line.

REM define path to log file
set myLog=e:templog.txt

REM Delete the logfile if it already exists.
if exist %myLog% del %myLog% >NUL

Echo Working...
REM When sending something to the log routine, enclose the
REM message in quotes.
Call :LogEntry "Starting %0"
Call :LogEntry "Running DIR"
DIR %windir%*.* /s >NUL
Call :LogEntry "Finished DIR"
Call :LogEntry "Sleeping for 30 seconds"
Sleep 30
Call :logEntry "Getting NETSTAT information"
REM Sending results of a NETSTAT command to the log. Notice
REM I'm enclosing the output in quotes. Otherwise, only the
REM first part of the output would be recorded.
for /f "tokens=*" %%t in ('netstat ^|find /i "TCP"') do @Call :LogEntry "%%t"
Call :LogEntry "Finishing logging and opening %myLog%"
REM Display the log
start Notepad %MyLog%
GOTO :EOF

:LogEntry
REM Output will be like: Wed 11/15/2006 05:27 PM "Starting LogEntryDemo.bat" 
for /f "tokens=*" %%i in ('date /t') do @for /f "tokens=*" %%j in ('time /t') do @echo %%i%%j %1 >>%myLog%
GOTO :EOF

:EOF

This works well to timestamp log entries.

In the IT trenches? So am I – read my IT-Trenches blog

 32,645 pts.

 

Thanks for this thorough script. It helps but of course, as a basic scripter I have questions.

In trying to run this on my test workstation – WinXP, I ran your script as is. It did not work so I attempted to run this line by line. When running the line “Call :LogEntry “Starting %0″” from the Windows command line, the following error message appeared: “Invalid attempt to call batch label outside of batch script”

Have I missed something with this?

 155 pts.

 

You cannot run individual lines outside the script since there is not any context for some of the calls made in the script. You cannot just cut/paste from the window here due to some issues with this wiki. Unfortunately, you will need to retype the script exactly as shown.

 0 pts.