Link to home
Start Free TrialLog in
Avatar of cma1
cma1

asked on

Want output of a batch file

I run a batch file (in a DOS window in WIN98-SE), called filecopy.bat
Essentially it copies lots of files to a backup directory.

The batch file sends to the screen various messages of output, like

"1 file copied"
 "8 files copied"
 "0 file copied"
  "Access denied"

But it scrolls by too quickly.

So I need a way to get the screen output sent to a .txt file.
(I asked this question in the WIN98 section, but no one could figure it out.)
Avatar of rin1010
rin1010


cma,

Do you need to log the output of all the commands in your batch file?
Different methods may be needed depending on the specific command,
but typically you can pipe the output to a file using redirection characters.
For example, if you have a batch file like this:

@echo off
copy file1.ext c:\temp
copy *.txt c:\temp

...you could log the screen output to a file named log.txt like this:

@echo off
copy file1.ext c:\temp > log.txt
copy *.txt c:\temp >> log.txt
notepad log.txt

The first "greater than" redirection character creates the log.txt file
and writes the batch command's output to the file.
Subsequently using two redirection characters appends the next command's output
to the file. If only one redirection character is used, it overwrites the log file if it exists,
creating a new log. You can do that each time, or use two characters on the first command
to append the results to a previously created log file. Or perhaps have your batch file
copy the log elsewhere if you need to save it.
(The last line is just to open the log file in Notepad.)

Other commands can be combined to format the output
depending on exactly what you want logged.
If this isn't what you need, post back with your batch file, if possible,
and which of its commands you need to log and we'll get it to work.
 


To slow the scroll there are several techniques like <ctrl>S or use of the KB key <pause>, but being lazy, I usually use similar redirects, sometimes with a command to CD, and usually with command to also redirect the date, maybe even the time if the logfile is to remain around awhile.

I usually code the same...
Except the last line would be either:
Type log.txt
or:
Type log.txt|more

In your case I'd use the latter, for you seem to want the scroll help. Drawback if not on Win2K is that to go back you'd have to manually reenter the command to display it on-screen. Advantage is you don't need windows. Without Windows running (perhaps exiting to DOS before Win9x kicks in) you could include the Edit command.

:-)    Thanx Rin, I do usually run in Windows, and keep a NotePad window open, on the side, to view it, but for some reason never thought to actually run it from within the batch command itself! My Gosh!
Avatar of cma1

ASKER

Rin, I am not quite sure what you are saying to do.

 I want a log of the screen output.
 Are you saying to insert > log.txt after each line,
or just the first line, with >> log.txt in the all subsequent lines or
> log.txt in the first line
>> log.txt in the second line
>>> log.txt in the third line, etc.?

I am simply trying to get a log of the screen output.
I thought that there was some simple DOS command that would do that.





@echo off
copy file1.ext c:\temp > log.txt
copy *.txt c:\temp >> log.txt
notepad log.txt

The first "greater than" redirection character creates the log.txt file
and writes the batch command's output to the file.
Subsequently using two redirection characters appends the next command's output
to the file. If only one redirection character is used, it overwrites the log file if it exists,
creating a new log. You can do that each time, or use two characters on the first command
to append the results to a previously created log file. Or perhaps have your batch file
copy the log elsewhere if you need to save it.
(The last line is just to open the log file in Notepad.)


Hi cma... Essentially what I was saying is the second scenario you mention;
append  > log.txt  to the end of the first line to be logged,
then append  >> log.txt  to each subsequent line.

Using only one redirection character will create a new file having the name specified,
(in this case log.txt) or will overwrite a file with that name if it exists.
Using two of these redirection characters will append to the file.

Alternatively, if you use two characters on the first line (as well as all other lines)
it will either create a new file having the name specified (if it doesn't exist)
or will append to a file with that name if it does exist. (e.g., if adding to a previous log)
So using one character either creates or overwrites and using two characters append.

Please wait just a little while and I'll provide some more info that I think you can use...
Was wondering if your batch script has many commands, or just a few,
and whether you need to log only the results of the COPY command...
(i.e., as opposed to XCOPY or any other commands you may have in the script.)
With your script I could offer some specific suggestions, though it's not necessary
and can still provide some examples which can be adapted to your situation.
But wanted you to know I saw your post and will respond further directly.
 
You all may want to look at this link for some background info where cma1 was redirected to this TA.
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20154035

Basically what cma1 is wanting is to log only the amount of files copied and any/all error messsages.
Avatar of cma1

ASKER

Crazyone has what I am after. I have many Copy commands in the batch file.
 I just want the screen output, which shows the result of each copy operation.
Umm everybody this does not have anything to do with this Q but I think it is improtant if you all would take a look at this link in Community Support.

https://www.experts-exchange.com/jsp/qShow.jsp?qid=20154895
> I asked this question in the WIN98 section:

https://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20154035

> but no one could figure it out.

Nonsense.  In that section, use of the '>' redirection was suggested, several times.

If you are trying to run 'MYPROG.BAT',
and want the output written to 'MYOUTPUT.TXT', type:

   MYPROG > C:\MYOUTPUT.TXT

That's all!
cma1 is correct when logging the command it does not log any of the error messages that appear on the screen. For example I ran this bat file from the Start > Run while in Win 2000 and this is the result:

the command in the bat file is this
copy C:\WINNT\system32\config\*.* c:\~~~

the Run line command is this
C:\~~~\copy.bat >c:\~~~\Filecopy.txt
------------------------------------------

The Log file

C:\WINNT\system32\config\USERDIFF
C:\WINNT\system32\config\SYSTEM.SAV
C:\WINNT\system32\config\SOFTWARE.SAV
C:\WINNT\system32\config\DEFAULT.SAV
C:\WINNT\system32\config\NTUSER.DAT
C:\WINNT\system32\config\USRCLASS.DAT
C:\WINNT\system32\config\SecEvent.Evt
C:\WINNT\system32\config\IExplore.evt
C:\WINNT\system32\config\SECURITY
C:\WINNT\system32\config\SYSTEM.ALT
C:\WINNT\system32\config\SAM
C:\WINNT\system32\config\SYSTEM
C:\WINNT\system32\config\SOFTWARE
C:\WINNT\system32\config\DEFAULT
C:\WINNT\system32\config\AppEvent.Evt
C:\WINNT\system32\config\SysEvent.Evt
       10 file(s) copied.
-------------------------------------------------

The DOS screen

C:\WINNT\system32\config\USERDIFF
C:\WINNT\system32\config\SYSTEM.SAV
C:\WINNT\system32\config\SOFTWARE.SAV
C:\WINNT\system32\config\DEFAULT.SAV
C:\WINNT\system32\config\NTUSER.DAT
C:\WINNT\system32\config\USRCLASS.DAT
C:\WINNT\system32\config\SecEvent.Evt
C:\WINNT\system32\config\IExplore.evt
C:\WINNT\system32\config\SECURITY
The process cannot access the file because it is being used by another process.
C:\WINNT\system32\config\SYSTEM.ALT
The process cannot access the file because it is being used by another process.
C:\WINNT\system32\config\SAM
The process cannot access the file because it is being used by another process.
C:\WINNT\system32\config\SYSTEM
The process cannot access the file because it is being used by another process.
C:\WINNT\system32\config\SOFTWARE
The process cannot access the file because it is being used by another process.
C:\WINNT\system32\config\DEFAULT
The process cannot access the file because it is being used by another process.
C:\WINNT\system32\config\AppEvent.Evt
C:\WINNT\system32\config\SysEvent.Evt
       10 file(s) copied.
---------------------------------------------------------

So the error messages are not making it to log file but cma1 wants these messages in the log file.
> the error messages are not making it to log file but cma1 wants these messages in the log file.

Under DOS, first use the 'CTTY' command to divert _all_ output, not just "standard output".
Then, run the '.BAT' command.
Then, use 'CTTY' again, to "cancel" redirection.
Avatar of cma1

ASKER

Crazyone, you have it exactly right.
MYPROG > C:\MYOUTPUT.TXT does not work for the reasons you cited.
ASKER CERTIFIED SOLUTION
Avatar of rin1010
rin1010

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's curious that 'CTTY' was first mentioned on 7/23 in this question,
but was first mentioned on 7/22 in your other question, at:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20154035

Plagiarism, or two great minds thinking alike?  :-)
You can simply log the Output on STDOUT and STDERR with:
Command  >log.txt 2>&1

For append use:
Command >>log.txt 2>>&1

("Command" stands for your instruction)

Who are you addressing regarding the plagiarism comments, Otta,
and what's your point?
 
> Who are you addressing regarding the plagiarism comments, Otta,

If the shoe fits you ...

> and what's your point?

The chronology speaks for itself ...

1 https://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20154035
1
1 From: Otta                                                              
1 Date: 07/22/2001 02:15PM PST
1 Use the 'CTTY' ("change TTY") to divert _all_ output.
____________

2 https://www.experts-exchange.com/jsp/qShow.jsp?ta=msdos&qid=20154458
2
2 From: Otta
2 Date: 07/22/2001 09:15AM PST
2
2 Under DOS, first use the 'CTTY' command

3 From: rin1010
3 Date: 07/23/2001 06:34PM PST
3
3 You can use the CTTY command to choose the output device ...

To repeat, plagiarism, or great minds thinking alike?
You make the call.

why not using the |more options?
type more /? in the command line.

Nushi.
It's time to clean up this topic area and that means taking care of this question. Your options at this
point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on
the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. PAQ the question because the information might be useful to others, but was not useful to you. To
use this option, you must state why the question is no longer useful to you, and the experts need to
let me know if they feel that you're being unfair.

3.  Ask Community Support to help split points between participating experts.  Just comment here with
details.

4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you
must state why the question is no longer useful to you, and the experts need to let me know if they
feel that you're being unfair.

If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.
 We also request that you review any other open questions you might have and update/close them.  Display
all your question history from your Member Profile to view details.

PLEASE DO NOT AWARD THE POINTS TO ME.

____________________________________________



Hi Experts:

In the event that the Asker does not respond, I would very much appreciate your opinions as to which
Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest
that I PAQ or delete the question.

Experts, please do not add further "answer" information to this question.  I will be back in about one
week to finalize this question.

Thank you everyone.

ComTech
Community Support Moderator @ Experts Exchange
PAQ it (content + cross_threads)
ComTech, please check:

https://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20154035

for an identical question, by the same author.
cma1, as it stands, you are in for 200 points in this question, 100 here and 100 in Windows98 Area.

It would be helpfull to get these questions to come to an end, and resolve both. At this moment, it very well could cost you 200 points.

Thank you,
ComTech
Community Support Moderator

Ps.  As it is now, I cannot delete either, as Experts have put time and effort into both.

I posted a message at theother siet as well.

These comments are addressed to "Otta" in response to the petulant complaints above...
I meant to remain indifferent to this puerile contention, but as Otta's accusations
appear directed at me personally and considering the recent activity and 'cross_threads'
I should reply for the record.

(My apologies to cma1 for cluttering your question with pettiness...
I see in your other question concerning logging these specific messages
that you may have thought you deleted that one, but hopefully
some of the methods in this thread will give you the necessary info.)

Otta; There'd be less spam if you'd simply say what you mean...
If you have problems and think something is wrong here, take it up with Customer Service...
-else have the courage to address someone directly and cut the innuendo.

Regarding the CTTY thing; I wrote and tested portions of those comments
during the weekend before you posted anything about it, unaware that cma
had a question elsewhere... I composed that long before you referenced ctty...
Then I receive notification that you've joined the thread and (unsurprisingly)
see you calling questioner cma's comments "nonsense" and repeating a suggestion
which cma had already informed you doesn't work...
(btw... nfernand posts the correct syntax;  "command.com /c batchfile.bat > log.txt"
if you want to see how to do it... But even so, 'your' method still doesn't provide a solution.)

You say it's "curious" that someone uses a command "mentioned" elsewhere by you...
That's your definition of plagiarism? I've dealt with many other questions regarding
the ctty command... Surely you don't think it's some undocumented secret
that only you're aware of? Are you implying nobody would've considered it
had you not brought it up? (you can call me al...) Note that I mention "redirection"
before you come in talking about it... How is that defined?
Regardless, you should take time to learn how to use a command
before commenting on it. Here's the gibberish from your two posts about it:

<<<<
Under DOS, first use the 'CTTY' command to divert _all_ output,
not just "standard output". Then, run the '.BAT' command.
Then, use 'CTTY' again, to "cancel" redirection.
>>>>

<<<<
Use the 'CTTY' ("change TTY") to divert _all_ output.
Run your '.BAT' file.
Use 'CTTY' to "cancel" the diversion.
Works under DOS!
>>>>

So what does that mean? How do you use the ctty command?
Where do you direct the output? In essence it means nothing...
You must supply a device name... The above will only generate an error
and waste someone's time... It won't work "under DOS" as you state
nor under Windows (which is what the question is about.)
So perhaps you can supply a working example (as I did)
of using ctty to log all of the screen output to a text file.
And btw... CTTY is an acronym for "Console TeleTYpe" ...
Not "change TTY" as you misstated.

And you still haven't expressed your point... Do you think you deserve some credit?
Fine with me... I suppose that's up to cma1... I've simply provided my notes on the
differing error messages and a few tested methods to first obviate some of the problem.
I wasn't even sure what messages needed logging until cma's confirmation
to the additional information CrazyOne posted. I provide methods to log all the messages
and the ctty thing is only a fraction of my input. I gave instructions and workarounds
in hopes of covering most situations. I tested everything proffered and it works as required.
I have no idea yet whether it will be of any use to cma... they're merely suggestions to try.
If you can find fault with any of it then please debug and redress it...
And if you can come up with a solution that works for cma, then that's even better.

Bottom line; keep your insinuations to yourself or complain in an appropriate manner.
A moderator monitors this ta to whom you can complain, who knows the subject
and probably doesn't want this unprofessionalism here. (Apologies to ComTech for mine.)
But if you want a couple more reasons why it's inconceivable
that I'd plagiarize your offered nonsense, please ask...
 
First, I would like to tell cma1, I did PAQ your question in Windows98 TA, as the Experts there agreed.  Do you wany me to add the other 100 points to this question?

Regards,
ComTech
Community Support
I don't see any plagiarism about CTTY, most commonly used by the terminal device. The valid ports are: CON, AUX, COM1-4, LPT1-3, and NUL.

It's late, and I refused to go into detail about CTTY and it's effect in Dos commands and batch files.  I will assure both, that it has been around for quite awhile, and have not known of any Copyrights.

Otta, just because you are aware of CTTY and it's uses, you're explination is not as explained enough for a new user, and that is how we have to look at most users, therefore they need more education and how to.

Before any of this started, I had an idea of who to award the question to, and since the user has not returned.  

The question will go to rin1010 for his intensive instructions.

As the user did not return to either question, the points here in this TA will be doubled.

Thanks all,
ComTech
Community Support

Just a note, I hold myself exclusivily responsible for my own actions.

Regards,
ComTech
This thread is over, but I'd like to point out for future surfers that Ugi's answer was right on the mark:

-Start--------------------------------------
You can simply log the Output on STDOUT and STDERR with:
Command  >log.txt 2>&1

For append use:
Command >>log.txt 2>>&1

("Command" stands for your instruction)

-End----------------------------------------

Very complete and concise. (e.g. manycopys.bat >log.txt 2>&1)

The batch file itself does not have to be modified.