1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Write Log of Output of Batch File

Discussion in 'DOS/Other' started by jprochas, Feb 21, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. jprochas

    jprochas Thread Starter

    Joined:
    Feb 21, 2008
    Messages:
    3
    I have a batch file that copies my files into 3 different drives and deletes the original. I want to have a copy of the output of the batch file so I have a record that I can access later of all the files I have copied and when. Not too keen on the batch file language quite yet but if anyone could offer some help I would really appreciate it.
     
  2. Sponsor

  3. devil_himself

    devil_himself

    Joined:
    Apr 7, 2007
    Messages:
    4,910
    jprochas , Welcome To TSG

    Can You Show Us Your Batch File?
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,775
    You can do this with XXcopy.
    XXCOPY c:\mysrc\ c:\mydest\ /s /Foc:\temp\mybackup.log
     
  5. jprochas

    jprochas Thread Starter

    Joined:
    Feb 21, 2008
    Messages:
    3
    Here's a copy of my batch file...I'm already using xcopy. Is there a difference between that and xxcopy? Or is there a way to just add a line of code at the end to write the whole output to log?
     

    Attached Files:

    • test.txt
      File size:
      3.7 KB
      Views:
      14,292
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,775
    XXcopy is a third party utility. More powerful than Microsoft's xcopy.
     
  7. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,775
    just put a redirect after all your xcopy commands.
    >> c:\somelogfile.txt

    With xxcopy, I believe you can tell it to delete the source as it copies over to the destination. That way you wouldn't need your delete cmd in there.
     
  8. jprochas

    jprochas Thread Starter

    Joined:
    Feb 21, 2008
    Messages:
    3
    Thanks Squashman! Used your redirect suggestion and worked great.
     
  9. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    If you need Date and Time you can add the following to the start of your file:
    Code:
    Echo.  >>c:\somelogfile.txt
    Echo.=================== >>c:\somelogfile.txt
    echo.%Date% >>c:\somelogfile.txt
    echo.%Time% >>c:\somelogfile.txt
    First line adds a blank line, 2nd line adds a separator line of equal signs, you can make this any character and as long as you like.

    You can also create a Daily logfile that contains the date in it's name. If your batch file runs more than once a day the same file will be used, with a separator added.

    Code:
    ::::::::::::::::::::::::::::::::::::::::::::::::::::
    ::Create a log file containing todays date
    ::LogPath is the path to the folder where the log file will be saved
    ::LogFileName is a descriptive name that will be added after the date
    ::LogFileExt is the extension to be used 
    ::First get the date into a variable. Then convert the '/' to '-' so
    ::the '/' won't be mistaken as a command line switch
    ::if the file exists, a blank line and a separator line of equal signs
    ::will be written at the end of the file in case the script runs more than
    ::once a day.
    ::::::::::::::::::::::::::::::::::::::::::::::::::::
    setlocal
    set LogPath=C:\Scripts\
    set LogFileExt=.log
    set LogFileName=Daily Backup%LogFileExt%
    ::use set MyLogFile=%date:~4% instead to remove the day of the week
    [COLOR="DarkRed"]set MyLogFile=%date%
    set MyLogFile=%MyLogFile:/=-%[/COLOR]
    set MyLogFile=%LogPath%%MyLogFile%_%LogFileName%
    ::Note that the quotes are REQUIRED around %MyLogFIle% in case it contains a space
    If NOT Exist "%MyLogFile%" goto:noseparator
    Echo.>>"%MyLogFile%"
    Echo.===================>>"%MyLogFile%"
    :noseparator
    echo.%Date% >>"%MyLogFile%"
    echo.%Time% >>"%MyLogFile%"
    :startbatch
    ::<your batch file>
    
    The output of %date% will vary depending on your Regional Settings. On my system the file will be named Tue 02-26-2008_Daily Backup.log
    If you want the names to sort in chronological order use these two lines to format as yyyy-mm-dd instead of the two colored lines above:
    Code:
    [COLOR="DarkRed"]set MyLogFile=%date:~4%
    set MyLogFile=%MyLogFile:~-4%-%MyLogFile:~,2%-%MyLogFile:~3,2%[/COLOR]
    This will create a file name of 2008-02-26_Daily Backup.log

    Type set /? and look at the section on substrings to see how it's used.
    Also, is this a typo?
    Code:
    xcopy /s/h/r/i/e/k/y "C:\Documents and Settings\jprochas\Desktop\Librarian\CONCEPT DETAILS" "\\gc3\engfiles\LIBRARIAN\SEND TO G\CONCEPT DETAILS"
    xcopy /s/h/r/i/e/k/y "C:\Documents and Settings\jprochas\Desktop\Librarian\CONCEPT DETAILS" "\\gc3\Users\jprochas"
    xcopy /s/h/r/i/e/k/y "C:\Documents and Settings\jprochas[COLOR="Red"][B]C[/B][/COLOR]\Desktop\Librarian\CONCEPT DETAILS" "\\gc3\engfiles\Jeff P"
    del /q "C:\Documents and Settings\jprochas\Desktop\Librarian\CONCEPT DETAILS"
    
    The red letter C seems like a typo, it's in a couple of places.
    You can replace "C:\Documents and Settings\jprochas with "%Userprofile% in your script which will make it a little shorter and (IMO) easier to read, and avoid typos. But, it will only work if it is run while you are logged into your account. You could also use a variable like this:

    Code:
    set FilePath=C:\Documents and Settings\jprochas\
    xcopy /s/h/r/i/e/k/y "%FilePath%Desktop\Librarian\CONCEPT DETAILS" "\\gc3\engfiles\LIBRARIAN\SEND TO G\CONCEPT DETAILS"
    xcopy /s/h/r/i/e/k/y "%FilePath%Desktop\Librarian\CONCEPT DETAILS" "\\gc3\Users\jprochas"
    xcopy /s/h/r/i/e/k/y "%FilePath%Desktop\Librarian\CONCEPT DETAILS" "\\gc3\engfiles\Jeff P"
    del /q "%FilePath%Desktop\Librarian\CONCEPT DETAILS"
    Or even like this:
    Code:
    set FilePath=C:\Documents and Settings\jprochas\Desktop\Librarian\
    xcopy /s/h/r/i/e/k/y "%FilePath%CONCEPT DETAILS" "\\gc3\engfiles\LIBRARIAN\SEND TO G\CONCEPT DETAILS"
    xcopy /s/h/r/i/e/k/y "%FilePath%CONCEPT DETAILS" "\\gc3\Users\jprochas"
    xcopy /s/h/r/i/e/k/y "%FilePath%Desktop\Librarian\CONCEPT DETAILS" "\\gc3\engfiles\Jeff P"
    del /q "%FilePath%CONCEPT DETAILS"
    HTH

    Jerry
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/685747