• Creating a log file from a batch file (XP Pro SP1)

    Home » Forums » AskWoody support » Windows » Windows Vista, XP and earlier » Questions: Vista, XP back to 3.1 » Creating a log file from a batch file (XP Pro SP1)

    Author
    Topic
    #405871

    Is it possible to create a log file from a batch file? The next question would be an obvious “How?” Basically, I have a batch file that is manually run by a user (the file kills a bunch of tasks). I would like for the results of that file to be logged somehow so I can see what’s going on after the fact (are all of the processes that are being killed actually still running when the batch file is run). I have no idea how to make it log, and my attempts so far have been very sad, indeed.

    Becky

    Funny – the lounge spell checker doesn’t recognize the word “log” but it’s fine with “logged”

    Viewing 1 reply thread
    Author
    Replies
    • #837255

      I don’t know what your batch file is doing, but the usual method of “writing” a file from a batch file is the redirection symbol ( > ). In running a command that normally produces on-screen results, to redirect that output to a file you might use: thecommand > c:somefoldersomefile.txt to write a file of what would normally appear on screen. Am I in your ballpark or not?

      • #837261

        Yes! Thank you!! fanfare That gave me almost exactly what I wanted. The only problem is that I would like for the stuff in the batch file to append the log (I have five lines and had to create five log files for this to work). Not a huge problem, but if there’s an easy answer, I’d love to hear it. Here’s basically what my batch file does:

        taskkill /f /im outlook.exe
        taskkill /f /im winword.exe

        There are more processes that it will kill, but that’s the basic idea (this is to troubleshoot an issue with our Document Management System – it’s not closing down everything the way it should, so we’re trying to make sure it does).

        Thanks,
        Becky

        • #837275

          Use >> instead of >. For example:

          ECHO Put this at the end of the file>>MYLOG.LOG

          Will add the text to the end of the file.

          • #837524

            Absolutely fabulous. The tech support guy I was talking to said that it was not possible to log the results of a batch file to a log file. I KNEW it could be done (I’ve seen it before), but I’ve never had to do it myself.

            Thanks so much to both of you!!
            Becky

            • #838509

              Becky (oops! edited to give correct person!)

              Sack your Tech Support guy and get me to do your BATch files!

              John

              (I’ll need a job in October!)

            • #838583

              Thanks, but I WOULD be the tech support person who writes the BATch files. grin No stealing my job!! scold My boss, the sys admin, has delegated that job to me. Now that I’ve been writing them, I know at least as much as he does (at least about BATch files, anyway). Since I’ve never had any formal training on BATch files (or any programming type thing, for that matter), I have to learn what I need either by Googling or by asking questions here. shrug

              Thanks anyway!

            • #838911

              Becky

              Shall I say a little bit more about BATch files?

              There are two console output “streams”, one for ‘normal’ messages (STDOUT, stream 1) and one for ‘error’ messages (STDERR, stream 2), although it’s up to the programmer which s/he uses for what messages.

              You can direct the two streams to different files (either the output of a single command, or of the entire BATch file), as in

              MYBATCH 1>>STDOUT.LOG 2>>STDERR.LOG

              or to the same file (this syntax is correct, but by no means obvious)

              MYBATCH 1>>STDBOTH.TXT 2>>&1

              In both cases you could leave out the ‘1’ in front of the ‘>>’, and would usually do so except to make the redirection more obvious. Also, it would probably work (in this instance) if you replaced the ‘>>’ by a single ‘>’. It’s not common to direct all the output from a BATch file to a log file, but it’s occasionally useful. More usually you want to collect the console output (if any) from one or more programs, so you would use something like

              PROGRAM1 > OURLOG.TXT

              to create a new log file, and

              PROGRAM2 >> OURLOG.TXT

              to add any console output from the second program to the same file. Note that I have left out the ‘1’ and am letting any error messages written to STDERR to just appear on the screen.

              Hope that’s not too unintelligible?

              John

              PS Invoice follows!!

            • #839144

              Not unintelligible. Just a smidge over my head. grin I may be able to make some sense of it if I play around with a few batch files for a bit. You still can’t have my job, though! razz

              Thanks!
              Becky

            • #839421

              Grrrh!

              John

            • #839422

              Grrrh!

              John

            • #839145

              Not unintelligible. Just a smidge over my head. grin I may be able to make some sense of it if I play around with a few batch files for a bit. You still can’t have my job, though! razz

              Thanks!
              Becky

            • #838912

              Becky

              Shall I say a little bit more about BATch files?

              There are two console output “streams”, one for ‘normal’ messages (STDOUT, stream 1) and one for ‘error’ messages (STDERR, stream 2), although it’s up to the programmer which s/he uses for what messages.

              You can direct the two streams to different files (either the output of a single command, or of the entire BATch file), as in

              MYBATCH 1>>STDOUT.LOG 2>>STDERR.LOG

              or to the same file (this syntax is correct, but by no means obvious)

              MYBATCH 1>>STDBOTH.TXT 2>>&1

              In both cases you could leave out the ‘1’ in front of the ‘>>’, and would usually do so except to make the redirection more obvious. Also, it would probably work (in this instance) if you replaced the ‘>>’ by a single ‘>’. It’s not common to direct all the output from a BATch file to a log file, but it’s occasionally useful. More usually you want to collect the console output (if any) from one or more programs, so you would use something like

              PROGRAM1 > OURLOG.TXT

              to create a new log file, and

              PROGRAM2 >> OURLOG.TXT

              to add any console output from the second program to the same file. Note that I have left out the ‘1’ and am letting any error messages written to STDERR to just appear on the screen.

              Hope that’s not too unintelligible?

              John

              PS Invoice follows!!

            • #838584

              Thanks, but I WOULD be the tech support person who writes the BATch files. grin No stealing my job!! scold My boss, the sys admin, has delegated that job to me. Now that I’ve been writing them, I know at least as much as he does (at least about BATch files, anyway). Since I’ve never had any formal training on BATch files (or any programming type thing, for that matter), I have to learn what I need either by Googling or by asking questions here. shrug

              Thanks anyway!

            • #838510

              Becky (oops! edited to give correct person!)

              Sack your Tech Support guy and get me to do your BATch files!

              John

              (I’ll need a job in October!)

          • #837525

            Absolutely fabulous. The tech support guy I was talking to said that it was not possible to log the results of a batch file to a log file. I KNEW it could be done (I’ve seen it before), but I’ve never had to do it myself.

            Thanks so much to both of you!!
            Becky

        • #837276

          Use >> instead of >. For example:

          ECHO Put this at the end of the file>>MYLOG.LOG

          Will add the text to the end of the file.

      • #837262

        Yes! Thank you!! fanfare That gave me almost exactly what I wanted. The only problem is that I would like for the stuff in the batch file to append the log (I have five lines and had to create five log files for this to work). Not a huge problem, but if there’s an easy answer, I’d love to hear it. Here’s basically what my batch file does:

        taskkill /f /im outlook.exe
        taskkill /f /im winword.exe

        There are more processes that it will kill, but that’s the basic idea (this is to troubleshoot an issue with our Document Management System – it’s not closing down everything the way it should, so we’re trying to make sure it does).

        Thanks,
        Becky

    • #837256

      I don’t know what your batch file is doing, but the usual method of “writing” a file from a batch file is the redirection symbol ( > ). In running a command that normally produces on-screen results, to redirect that output to a file you might use: thecommand > c:somefoldersomefile.txt to write a file of what would normally appear on screen. Am I in your ballpark or not?

    Viewing 1 reply thread
    Reply To: Creating a log file from a batch file (XP Pro SP1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: