Customizing the logging of a Wiindows batch file

155 pts.
Batch files
Microsoft Windows Server 2003
Windows Batch Files
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

Answer Wiki

Thanks. We'll let you know when a new response is added.

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!


Discuss This Question: 10  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Pjb0222
    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:
  • Pjb0222
    So, using the link button didn't work.
    3,310 pointsBadges:
  • MicJ
    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 pointsBadges:
  • Pjb0222
    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 pointsBadges:
  • MicJ
    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 pointsBadges:
  • Labnuke99
    Here's a script file I have in my library. Maybe this will do the job for you.
    @echo off
    REM Jeffery Hicks
    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%
    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%
    This works well to timestamp log entries. In the IT trenches? So am I - read my IT-Trenches blog
    32,960 pointsBadges:
  • MicJ
    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 pointsBadges:
  • Troy Tate
    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 pointsBadges:
  • jack01reacher
    Well if all the bots are the same make a bot that counters them and you should have 1st every time ;-)
    10 pointsBadges:
  • dannydan
    That's because RoboCopy doesn't have the time stamp copying feature (and not sure when they are going to update their software with this feature). Try using GS Rich Copy 360. It can copy timestamp and has a file scheduling feature as a bonus, too! 
    10 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: