Solved

Batch file output

Posted on 2006-11-20
18
4,400 Views
Last Modified: 2013-11-13
Gday Guys,

Just a quick question and probably a simple one at that. I've set up a batch file at work for our data backup, and the batch file shuts the system down on completion. What I would like to ba able to do is output all the processes (ie everything on the screen whilst the batch is running) to a log file so I can review the process at a later date and check that it has all ran properly.
As I said, its probably something simple but for the life of me I cant work it out.

Thanks for your help guys and I'll look forward to hearing from you.

Regards
        Rhys
0
Comment
Question by:rhysdlowe
  • 5
  • 5
  • 4
  • +1
18 Comments
 
LVL 16

Expert Comment

by:Brian Pringle
ID: 17984233
You can simply pipe the output from each command to a text file.

YourCommand > file.txt             -- Replaces file.txt with the output of the command
YourCommand >> file.txt           -- Appends to file.txt with the output of the command

So, your batch file would look something like this.

Command1 > file.txt
Command2 >> file.txt
Command3 >> file.txt
Command4 >> file.txt

and etc.  You could even make it a little more fancy and name the file based on the date so that you have old versions of it, if you need.
0
 

Author Comment

by:rhysdlowe
ID: 17984428
Thanks for that, but its quite a long file (>50 lines). It actually zips up all the data, burns it to DVD, then takes the original data and copies it across a network. The reason I want to log the proces is to view any errors that occur in the compression, writing, or copying processes. I have an error logging system set up that lets you know if there was an error, but I need to view exactally what the error was after the batch has closed.
As you can imaging using the above method would be for one, untidy and a headache and a half, and two, probably wouldn't output the entire process (ie the zipping process, file by file). Although I haven't yet tried.

Thanks for your help but there has to be an easier way surely?
0
 
LVL 16

Expert Comment

by:Brian Pringle
ID: 17984432
It would put everything in the batch file, but I can see why it would be difficult for your environment.  I will see if I can find an alternative solution.
0
 
LVL 16

Expert Comment

by:Brian Pringle
ID: 17984475
I meant it would put all of the output in the log file.
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17984581
Let's say your batch file is called x.bat

Then from the command line type: x.bat > file.txt
Or if calling from another batch or cmd type: call x.bat > file.txt
0
 
LVL 16

Expert Comment

by:Brian Pringle
ID: 17984635
Raymun is correct.  I just tried it and it works just fine.  However, you need to turn echo on by adding the following line at the very beginning of the batch file if you want to be able to see the entire screen output.  

@echo on
0
 

Author Comment

by:rhysdlowe
ID: 17984774
Thansk Raymun, thats awesome! However, there are a couple of pauses at the begginning and the "press any key...." is not displayed. Is there a way to display the echos/processes etc to screen as well as write to log or am I pushing my lucka bit there :)
0
 
LVL 16

Accepted Solution

by:
Brian Pringle earned 42 total points
ID: 17984850
If the batch file is supposed to shut down the server, do you really want it to pause at the end?  Wouldn't that defeat the purpose of the batch file doing everything for you?

You could create a second batch file to call your backup batch file.

@echo off
BackupBatchFile.bat > BackupLog.txt
notepad BackupLog.txt
pause
exit

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:Raymun
ID: 17984869
the command PAUSE will prompt "press any key..." but since the '>' is redirecting all output to file.txt, that prompt will be in the file and not on the command screen.

There is no way to simultaneously output to both a file and the command screen but you can simulate it by inserting

TYPE file.txt

in your batch. When this command is called, it will display the contents of file.txt to the command screen.

What do you mean by it pauses? It may just seem like its pausing because its outputting to the text file and not to the screen.
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17985011
Forget my previous post. It's just messy. I assume that the reason you want to see the output to the screen is you want to see whats being put in file.txt.

So after calling x.bat > file.txt, open another console and just type: TYPE file.txt.

This will display the contents of file.txt in real-time and will do so even though x.bat has it locked.
0
 

Author Comment

by:rhysdlowe
ID: 17985028
Thanks for your help guys. It doesn't pause at the end, it pauses at the beggining, the reason for this is that it that it gives instructions for the user (make sure DVD in drive, all files closed ...... etc etc ....... press any key to continue). I can run the "backup.bat > log.txt" after this is displayed, I guess that may be the only way to do it, but it would have been nice to be able to view the process as well but I guess thats not going to work.

While ive got you guys working, the other idea I had was to pause the batch at the end only if an error had occured (if successful it would shutdown), however I have found when left on overnight the batch closes anyway, any ideas. I have included the coding for this idea below:
Note actual directory names have been removed for obvious reasons.

I probably should have included this in the first post, you guys would have had a better idea on what I was on about. But have a look at this and see what you think, as I said I intentionally created an error and let it go overnight and in the morning the batch had closed and I cant work out why????

...............................

echo off
cls

echo --------------------------------------------------------------------------
echo+
echo Backup will now begin, please take following steps to ensure all is successful:
echo 1. That all lower level workstations have fully completed the backup process
echo 2. That there is a DVD-RW in drive E: of this PC
echo 3. That all files being backed up have been closed
echo+
echo Note: you can abort this process at any time by pressing Ctl-C
echo+
echo --------------------------------------------------------------------------

echo This process may take up to 30 minutes, when you are ready to proceed
pause

set comperr=0
set burnerr=0
set serverr=0


echo+
echo+
echo+
echo ----------------------------------
echo+
echo ~Preparing for data compression~
echo+
echo ----------------------------------
cd\

"c:\program files\winzip\wzzip" -P -r -u "C:\backup.zip" "dir1\*.*" "dir2\*.*" "dir3\*.*" "dir4\*.*" "dir5\*.*"

If ErrorLevel 1 Goto ErrMsg

echo+
Echo ***Compression completed successfully***
echo+

Goto Burn

:ErrMsg

echo+
Echo ***AN ERROR HAS OCCURRED WHILST COMPRESSING DATA***
echo Please consult your system administrator
echo+
set comperr=1
goto Burn

:Burn

echo -----------------------------------
echo+
echo ~Preparing to write data to DVD~
echo+
echo -----------------------------------

cd "C:\"Program Files"\Ahead\Nero\"

NeroCmd.exe --erase --dvd --drivename e

NeroCmd.exe --write --dvd --drivename e --real --speed 2 --iso Backup --enable_abort --underrun_prot --create_iso_fs "C:\Backup.zip" --disable_eject --verify

If ErrorLevel 1 Goto ErrMsg2

echo+
Echo ***DVD written successfully***
echo+

goto backser

:ErrMsg2

echo+
Echo ***AN ERROR HAS OCCURRED WHILST BURNING DATA***
echo Please consult your system administrator
echo+
echo+
set burnerr=1
goto:backser

:backser

echo -----------------------------------
echo+
echo ~Preparing to copy data to server~
echo+
echo -----------------------------------

cd\
cd ".....dir"

xcopy "dir1\*.*" /y /E /D /c /R Z:\....                 <-maped network path
xcopy "dir2\*.*" /y /E /D /c /R Z:\.....
xcopy "dir3\*.*" /y /E /D /c /R Z:\.....
xcopy "dir4\*.*" /y /E /D /c /R Z:\.....
xcopy "dir5\*.*" /y /E /D /c /R Z:\.....


If ErrorLevel 1 Goto ErrMsg3

Goto Last

:ErrMsg3
echo+
Echo *****AN ERROR OCCURRED WHILST COPYING DATA TO SERVER*****
echo+
echo Please see system administrator
echo+
echo+
set serverr=1
goto Last

:Last

if %comperr%==1 goto errlog
if %burnerr%==1 goto errlog
if %serverr%==1 goto errlog

cls
echo+
echo+
echo+
echo+
echo ----------------------------------------------
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo+
echo BACKUP COMPLETED SUCCESSFULLY
echo+
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo ----------------------------------------------
echo+

shutdown -s -f -t 20 -c "Backup to server has been completed successfully. System will now shut down"

goto:eof


:errlog

echo+
echo+
echo+
echo+
echo+
echo ---------------------------------------
echo+
echo !!! WARNING! !!!! WARNING! !!!!
echo+
echo THE BACKUP WAS UNSUCCESSFUL, THE FOLLOWING ERRORS OCCURRED:
echo+

if %comperr%==1 echo *An error occurred in the compression stage, data may be corrupted!
if %burnerr%==1 echo *An error occurred in the burn process, DVD may be unusable!
if %serverr%==1 echo *An error occurred whilst copying to server, server backup may be incomplete!

echo+
echo For more information please consult your system administrator
echo+
pause
goto:eof

.......................

Cheers
       Rhys
0
 
LVL 4

Assisted Solution

by:Raymun
Raymun earned 42 total points
ID: 17985083
Remove all the echo'd instructions and pause command from backup.bat and put in another batch, say prompt.bat.
Then at the end of prompt.bat put

CALL backup.bat > log.txt

Then just run prompt.bat and again enter 'TYPE log.txt' in another console to see the contents of log.txt while the batch is running
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 42 total points
ID: 17985110
"There is no way to simultaneously output to both a file and the command screen"

Actually there is...using some unix tools like tee:
http://short.stop.home.att.net/freesoft/unix.htm
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17985119
Oh, and for your other idea, you can just change the end of backup.bat:

setlocal enabledelayedexpansion

if %comperr%==1 (
    echo *An error occurred in the compression stage, data may be corrupted!
    set result=failed
)

if %burnerr%==1 (
    echo *An error occurred in the burn process, DVD may be unusable!
    set result=failed
)

if %serverr%==1 (
    echo *An error occurred whilst copying to server, server backup may be incomplete!
    set result=failed
)

if "!result!"=="failed" pause
0
 

Author Comment

by:rhysdlowe
ID: 17985136
Thanks for your help guys, I should be able to work something out from here.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18152114
So, are you all set here?
This question is still open.  Please see http://www.experts-exchange.com/help.jsp#hs5 for your options on closing.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now