Solved Exporting log file (from batch jobs)

January 22, 2018 at 03:06:48
Specs: Windows 7, Intel i3 / 16GByte RAM
Hi all,

On Windows7 64-bit, I'm using a batch script (*.bat or *.cmd file) for mass copying folders. Would like to get a log file saved into the target folder. At best, one single *.txt file, logging all copy jobs performed.

What should I add to the script (below) to get it right? Can anybody please help me? I don't know nuts on coding :-/ Thank you.

===================================
Batch-Description:
The pasted script (below) enables copying 8 Source folders on Drives E:\ and C:\ into a Target folder "Backup", on Drive V:\. In order to distinguish backups easily, the Actual Date is used as top folder's Name, created on top, like V:\Backup\22.01.2018\ where 8 source folders will be copied into.
===================================


****************Begin Script*********************

@echo off

set new_folder=%DATE:/=_%
set backup_folder=V:\Backup\%new_folder%

mkdir %backup_folder%

echo ### Backing up "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless"
robocopy "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless" "%backup_folder%\Paperless" /MIR

echo ### Backing up "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Online"
robocopy "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Online" "%backup_folder%\Online" /MIR

echo ### Backing up "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Insurance"
robocopy "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Insurance" "%backup_folder%\Insurance" /MIR

echo ### Backing up "E:\SIKO\Safe\Own\Travel\2017"
robocopy "E:\SIKO\Safe\Own\Travel\2017" "%backup_folder%\2017" /MIR

echo ### Backing up "E:\SIKO\Safe\Own\Travel\2018"
robocopy "E:\SIKO\Safe\Own\Travel\2018" "%backup_folder%\2018" /MIR

echo ### Backing up "E:\SIKO\Safe\Own\Bookmarks\Waterfox\"
robocopy "E:\SIKO\Safe\Own\Bookmarks\Waterfox" "%backup_folder%\Waterfox" /MIR

echo ### Backing up "E:\SIKO\Safe\Maps"
robocopy "E:\SIKO\Safe\Maps" "%backup_folder%\Maps" /MIR

echo ### Backing up "C:\Users\Goofy\AppData\Local\Microsoft\Outlook"
robocopy "C:\Users\Goofy\AppData\Local\Microsoft\Outlook" "%backup_folder%\Outlook" /MIR

echo Backup Complete!

@pause


**********End Script**********************



See More: Exporting log file (from batch jobs)

Report •

#1
January 22, 2018 at 04:16:29
✔ Best Answer
Use > to overwrite the contents of a file with a given output
Use >> to append a given output to a file.

example:

echo ### Backing up "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless" >> logfile.txt
robocopy "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless" "%backup_folder%\Paperless" /MIR >> logfile.txt
echo. >>logfile.txt

To save the log file in the current "backup folder", replace >>logfile.txt with >>%backup_folder%\logfile.txt
Keep in mind that if %backup_folder% contains spaces, you need to surround it in quotes.

You can also, instead of adding >>log.txt or whatever to the end of each line, you can just put a ( where you want the log file generation to start, and add )>>log.txt 2>&1 at the end. The 2>&1 will ensure that messages going through stderr also end up being put into the log file.

example:

(
echo ### Backing up "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless"
robocopy "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless" "%backup_folder%\Paperless" /MIR
echo.
)>>logfile.txt 2>&1

See: https://ss64.com/nt/syntax-redirect...

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Report •

#2
January 22, 2018 at 07:35:11
Thank you so much!

Good explained, easy for me to follow.

Wrapped the most comfy option over all 8 commands:

(
echo ### Backing up "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless"
robocopy "E:\SIKO\Safe\Own\Misc\Correspondence\Misc\Paperless" "%backup_folder%\Paperless" /MIR
2...
3...
4...
5...
6...
7...
8...
echo.
)>>%backup_folder%\logfile.txt 2>&1

And it works..... :-)

Just to let you know:

101

Writing "echo." (with a point), like:

echo.
)>>%backup_folder%\logfile.txt 2>&1

leads the log file to state at its end: "The command "echo." is either wrong written or couldn't be found."

102

Writing "echo" (without point), like:

echo
)>>%backup_folder%\logfile.txt 2>&1

leads the log file to state at its end: "Echo is turned off (OFF)."

103

Omitting "echo" at all, like:

)>>%backup_folder%\logfile.txt 2>&1

leads the log file to edit NO comments regarding "echo".


Must be funny syntax thingy. Meant to be ignored :-)

Cheers


Report •
Related Solutions


Ask Question