# Write Log of Output of Batch File



## jprochas

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.


----------



## devil_himself

jprochas , Welcome To TSG

Can You Show Us Your Batch File?


----------



## Squashman

You can do this with XXcopy.
XXCOPY c:\mysrc\ c:\mydest\ /s /Foc:\temp\mybackup.log


----------



## jprochas

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?


----------



## Squashman

XXcopy is a third party utility. More powerful than Microsoft's xcopy.


----------



## Squashman

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.


----------



## jprochas

Thanks Squashman! Used your redirect suggestion and worked great.


----------



## TheOutcaste

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


----------

