Redirecting Output from within Batch file












74















I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.



I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?



Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.










share|improve this question





























    74















    I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.



    I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?



    Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.










    share|improve this question



























      74












      74








      74


      24






      I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.



      I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?



      Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.










      share|improve this question
















      I am creating a batch file with some simple commands to gather information from a system. The batch file contains commands to get the time, IP information, users, etc.



      I assembled all the commands in a batch file, and it runs, but I would like the batch file, when run to output the results to a text file (log). Is there a command that I can add to the batch that would do so?



      Keep in mind I do not want to run the batch from cmd, then redirect output ; I want to redirect the output from inside the batch, if that is possible.







      windows batch-file cmd batch-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 '18 at 11:40









      PLNech

      2,0871435




      2,0871435










      asked Dec 10 '13 at 1:09









      user3085030user3085030

      376133




      376133
























          8 Answers
          8






          active

          oldest

          votes


















          113














          The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.



          @echo off
          command1 >output.txt
          command2 >>output.txt
          ...
          commandN >>output.txt


          A better way - easier to write, and faster because the file is opened and positioned only once.



          @echo off
          >output.txt (
          command1
          command2
          ...
          commandN
          )


          Another good and fast way that only opens and positions the file once



          @echo off
          call :sub >output.txt
          exit /b

          :sub
          command1
          command2
          ...
          commandN





          share|improve this answer





















          • 1





            Love the solutions where I can set it for the remainder of the file

            – Sam
            Feb 2 '16 at 22:36






          • 3





            Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.

            – Jannes
            Dec 7 '16 at 16:41






          • 1





            @Jannes - True, but you can always get info about the running batch script if you add a modifier. For example, %~f0 always gives the full path to the batch script, even when inside a CALLed :subroutine.

            – dbenham
            Dec 7 '16 at 17:08






          • 3





            @ThariqNugrohotomo - >output.txt 2>&1

            – dbenham
            Mar 21 '17 at 10:11






          • 2





            @Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute cmd /? or help cmd from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.

            – dbenham
            Jan 14 '18 at 23:22



















          44














          if you want both out and err streams redirected



          dir >> a.txt 2>&1





          share|improve this answer



















          • 16





            +1. It's also worth pointing out that using >> will append to a.txt. To overwrite a.txt instead, use >. stackoverflow.com/q/4458231/1098302

            – Aaron
            Apr 19 '17 at 19:15





















          11














          I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)



          To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:



          @echo off
          > filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
          echo Your Name Here
          echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
          REM do some stuff here
          echo Your Name Here
          echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
          )


          Will create a file like what you see in this screenshot of the file in the target directory



          That will contain this output:



          Your Name Here
          Beginning Date/Time: 2016-09-16_141048.log
          Your Name Here
          Ending Date/Time: 2016-09-16_141048.log


          Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.






          share|improve this answer
























          • Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?

            – Moondra
            Jan 14 '18 at 18:50






          • 1





            @Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.

            – Anthony
            Apr 17 '18 at 18:32



















          8














          @echo off
          >output.txt (
          echo Checking your system infor, Please wating...

          systeminfo | findstr /c:"Host Name"
          systeminfo | findstr /c:"Domain"

          ipconfig /all | find "Physical Address"

          ipconfig | find "IPv4"
          ipconfig | find "Default Gateway"

          )

          @pause





          share|improve this answer


























          • I think that this is the most elegant way. Thanks Wesley!

            – Kiswanij
            Aug 23 '18 at 4:37



















          7














          echo some output >"your logfile"


          or



          (
          echo some output
          echo more output
          )>"Your logfile"


          should fill the bill.



          If you want to APPEND the output, use >> instead of >. > will start a new logfile.






          share|improve this answer































            3














            There is a cool little program you can use to redirect the output to a file and the console



            some_command  ^|  TEE.BAT  [ -a ]  filename 





            @ECHO OFF
            :: Check Windows version
            IF NOT "%OS%"=="Windows_NT" GOTO Syntax

            :: Keep variables local
            SETLOCAL

            :: Check command line arguments
            SET Append=0
            IF /I [%1]==[-a] (
            SET Append=1
            SHIFT
            )
            IF [%1]== GOTO Syntax
            IF NOT [%2]== GOTO Syntax

            :: Test for invalid wildcards
            SET Counter=0
            FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
            IF %Counter% GTR 1 (
            SET Counter=
            GOTO Syntax
            )

            :: A valid filename seems to have been specified
            SET File=%1

            :: Check if a directory with the specified name exists
            DIR /AD %File% >NUL 2>NUL
            IF NOT ERRORLEVEL 1 (
            SET File=
            GOTO Syntax
            )

            :: Specify /Y switch for Windows 2000 / XP COPY command
            SET Y=
            VER | FIND "Windows NT" > NUL
            IF ERRORLEVEL 1 SET Y=/Y

            :: Flush existing file or create new one if -a wasn't specified
            IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

            :: Actual TEE
            FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
            > CON ECHO.%%B
            >> %File% ECHO.%%B
            )

            :: Done
            ENDLOCAL
            GOTO:EOF

            :Count
            SET /A Counter += 1
            SET File=%1
            GOTO:EOF

            :Syntax
            ECHO.
            ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
            ECHO Display text on screen and redirect it to a file simultaneously
            ECHO.
            IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
            IF NOT "%OS%"=="Windows_NT" GOTO Skip
            ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
            :Skip
            ECHO.
            ECHO Where: "some_command" is the command whose output should be redirected
            ECHO "filename" is the file the output should be redirected to
            ECHO -a appends the output of the command to the file,
            ECHO rather than overwriting the file
            ECHO.
            ECHO Written by Rob van der Woude
            ECHO http://www.robvanderwoude.com
            ECHO Modified by Kees Couprie
            ECHO http://kees.couprie.org
            ECHO and Andrew Cameron








            share|improve this answer

































              2














              @echo OFF
              [your command] >> [Your log file name].txt


              I used the command above in my batch file and it works. In the log file, it shows the results of my command.






              share|improve this answer

































                1














                Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:



                if not "%1"=="STDOUT_TO_FILE"  %0 STDOUT_TO_FILE %*  >log.txt 2>&1
                shift /1





                share|improve this answer
























                • This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)

                  – CCarlos
                  Nov 23 '18 at 19:37











                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20484151%2fredirecting-output-from-within-batch-file%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                8 Answers
                8






                active

                oldest

                votes








                8 Answers
                8






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                113














                The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.



                @echo off
                command1 >output.txt
                command2 >>output.txt
                ...
                commandN >>output.txt


                A better way - easier to write, and faster because the file is opened and positioned only once.



                @echo off
                >output.txt (
                command1
                command2
                ...
                commandN
                )


                Another good and fast way that only opens and positions the file once



                @echo off
                call :sub >output.txt
                exit /b

                :sub
                command1
                command2
                ...
                commandN





                share|improve this answer





















                • 1





                  Love the solutions where I can set it for the remainder of the file

                  – Sam
                  Feb 2 '16 at 22:36






                • 3





                  Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.

                  – Jannes
                  Dec 7 '16 at 16:41






                • 1





                  @Jannes - True, but you can always get info about the running batch script if you add a modifier. For example, %~f0 always gives the full path to the batch script, even when inside a CALLed :subroutine.

                  – dbenham
                  Dec 7 '16 at 17:08






                • 3





                  @ThariqNugrohotomo - >output.txt 2>&1

                  – dbenham
                  Mar 21 '17 at 10:11






                • 2





                  @Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute cmd /? or help cmd from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.

                  – dbenham
                  Jan 14 '18 at 23:22
















                113














                The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.



                @echo off
                command1 >output.txt
                command2 >>output.txt
                ...
                commandN >>output.txt


                A better way - easier to write, and faster because the file is opened and positioned only once.



                @echo off
                >output.txt (
                command1
                command2
                ...
                commandN
                )


                Another good and fast way that only opens and positions the file once



                @echo off
                call :sub >output.txt
                exit /b

                :sub
                command1
                command2
                ...
                commandN





                share|improve this answer





















                • 1





                  Love the solutions where I can set it for the remainder of the file

                  – Sam
                  Feb 2 '16 at 22:36






                • 3





                  Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.

                  – Jannes
                  Dec 7 '16 at 16:41






                • 1





                  @Jannes - True, but you can always get info about the running batch script if you add a modifier. For example, %~f0 always gives the full path to the batch script, even when inside a CALLed :subroutine.

                  – dbenham
                  Dec 7 '16 at 17:08






                • 3





                  @ThariqNugrohotomo - >output.txt 2>&1

                  – dbenham
                  Mar 21 '17 at 10:11






                • 2





                  @Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute cmd /? or help cmd from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.

                  – dbenham
                  Jan 14 '18 at 23:22














                113












                113








                113







                The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.



                @echo off
                command1 >output.txt
                command2 >>output.txt
                ...
                commandN >>output.txt


                A better way - easier to write, and faster because the file is opened and positioned only once.



                @echo off
                >output.txt (
                command1
                command2
                ...
                commandN
                )


                Another good and fast way that only opens and positions the file once



                @echo off
                call :sub >output.txt
                exit /b

                :sub
                command1
                command2
                ...
                commandN





                share|improve this answer















                The simple naive way that is slow because it opens and positions the file pointer to End-Of-File multiple times.



                @echo off
                command1 >output.txt
                command2 >>output.txt
                ...
                commandN >>output.txt


                A better way - easier to write, and faster because the file is opened and positioned only once.



                @echo off
                >output.txt (
                command1
                command2
                ...
                commandN
                )


                Another good and fast way that only opens and positions the file once



                @echo off
                call :sub >output.txt
                exit /b

                :sub
                command1
                command2
                ...
                commandN






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 13 '18 at 15:40

























                answered Dec 10 '13 at 1:17









                dbenhamdbenham

                101k20180284




                101k20180284








                • 1





                  Love the solutions where I can set it for the remainder of the file

                  – Sam
                  Feb 2 '16 at 22:36






                • 3





                  Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.

                  – Jannes
                  Dec 7 '16 at 16:41






                • 1





                  @Jannes - True, but you can always get info about the running batch script if you add a modifier. For example, %~f0 always gives the full path to the batch script, even when inside a CALLed :subroutine.

                  – dbenham
                  Dec 7 '16 at 17:08






                • 3





                  @ThariqNugrohotomo - >output.txt 2>&1

                  – dbenham
                  Mar 21 '17 at 10:11






                • 2





                  @Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute cmd /? or help cmd from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.

                  – dbenham
                  Jan 14 '18 at 23:22














                • 1





                  Love the solutions where I can set it for the remainder of the file

                  – Sam
                  Feb 2 '16 at 22:36






                • 3





                  Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.

                  – Jannes
                  Dec 7 '16 at 16:41






                • 1





                  @Jannes - True, but you can always get info about the running batch script if you add a modifier. For example, %~f0 always gives the full path to the batch script, even when inside a CALLed :subroutine.

                  – dbenham
                  Dec 7 '16 at 17:08






                • 3





                  @ThariqNugrohotomo - >output.txt 2>&1

                  – dbenham
                  Mar 21 '17 at 10:11






                • 2





                  @Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute cmd /? or help cmd from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.

                  – dbenham
                  Jan 14 '18 at 23:22








                1




                1





                Love the solutions where I can set it for the remainder of the file

                – Sam
                Feb 2 '16 at 22:36





                Love the solutions where I can set it for the remainder of the file

                – Sam
                Feb 2 '16 at 22:36




                3




                3





                Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.

                – Jannes
                Dec 7 '16 at 16:41





                Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.

                – Jannes
                Dec 7 '16 at 16:41




                1




                1





                @Jannes - True, but you can always get info about the running batch script if you add a modifier. For example, %~f0 always gives the full path to the batch script, even when inside a CALLed :subroutine.

                – dbenham
                Dec 7 '16 at 17:08





                @Jannes - True, but you can always get info about the running batch script if you add a modifier. For example, %~f0 always gives the full path to the batch script, even when inside a CALLed :subroutine.

                – dbenham
                Dec 7 '16 at 17:08




                3




                3





                @ThariqNugrohotomo - >output.txt 2>&1

                – dbenham
                Mar 21 '17 at 10:11





                @ThariqNugrohotomo - >output.txt 2>&1

                – dbenham
                Mar 21 '17 at 10:11




                2




                2





                @Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute cmd /? or help cmd from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.

                – dbenham
                Jan 14 '18 at 23:22





                @Moondra - that is standard batch syntax for calling a labeled subroutine within the same script. Execute cmd /? or help cmd from the console command line for documentation. The trick of the third method is that the redirection on the CALL applies to all commands within the CALLed subroutine.

                – dbenham
                Jan 14 '18 at 23:22













                44














                if you want both out and err streams redirected



                dir >> a.txt 2>&1





                share|improve this answer



















                • 16





                  +1. It's also worth pointing out that using >> will append to a.txt. To overwrite a.txt instead, use >. stackoverflow.com/q/4458231/1098302

                  – Aaron
                  Apr 19 '17 at 19:15


















                44














                if you want both out and err streams redirected



                dir >> a.txt 2>&1





                share|improve this answer



















                • 16





                  +1. It's also worth pointing out that using >> will append to a.txt. To overwrite a.txt instead, use >. stackoverflow.com/q/4458231/1098302

                  – Aaron
                  Apr 19 '17 at 19:15
















                44












                44








                44







                if you want both out and err streams redirected



                dir >> a.txt 2>&1





                share|improve this answer













                if you want both out and err streams redirected



                dir >> a.txt 2>&1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 12 '15 at 17:59









                Kalpesh SoniKalpesh Soni

                3,34713239




                3,34713239








                • 16





                  +1. It's also worth pointing out that using >> will append to a.txt. To overwrite a.txt instead, use >. stackoverflow.com/q/4458231/1098302

                  – Aaron
                  Apr 19 '17 at 19:15
















                • 16





                  +1. It's also worth pointing out that using >> will append to a.txt. To overwrite a.txt instead, use >. stackoverflow.com/q/4458231/1098302

                  – Aaron
                  Apr 19 '17 at 19:15










                16




                16





                +1. It's also worth pointing out that using >> will append to a.txt. To overwrite a.txt instead, use >. stackoverflow.com/q/4458231/1098302

                – Aaron
                Apr 19 '17 at 19:15







                +1. It's also worth pointing out that using >> will append to a.txt. To overwrite a.txt instead, use >. stackoverflow.com/q/4458231/1098302

                – Aaron
                Apr 19 '17 at 19:15













                11














                I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)



                To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:



                @echo off
                > filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
                echo Your Name Here
                echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                REM do some stuff here
                echo Your Name Here
                echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                )


                Will create a file like what you see in this screenshot of the file in the target directory



                That will contain this output:



                Your Name Here
                Beginning Date/Time: 2016-09-16_141048.log
                Your Name Here
                Ending Date/Time: 2016-09-16_141048.log


                Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.






                share|improve this answer
























                • Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?

                  – Moondra
                  Jan 14 '18 at 18:50






                • 1





                  @Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.

                  – Anthony
                  Apr 17 '18 at 18:32
















                11














                I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)



                To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:



                @echo off
                > filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
                echo Your Name Here
                echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                REM do some stuff here
                echo Your Name Here
                echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                )


                Will create a file like what you see in this screenshot of the file in the target directory



                That will contain this output:



                Your Name Here
                Beginning Date/Time: 2016-09-16_141048.log
                Your Name Here
                Ending Date/Time: 2016-09-16_141048.log


                Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.






                share|improve this answer
























                • Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?

                  – Moondra
                  Jan 14 '18 at 18:50






                • 1





                  @Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.

                  – Anthony
                  Apr 17 '18 at 18:32














                11












                11








                11







                I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)



                To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:



                @echo off
                > filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
                echo Your Name Here
                echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                REM do some stuff here
                echo Your Name Here
                echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                )


                Will create a file like what you see in this screenshot of the file in the target directory



                That will contain this output:



                Your Name Here
                Beginning Date/Time: 2016-09-16_141048.log
                Your Name Here
                Ending Date/Time: 2016-09-16_141048.log


                Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.






                share|improve this answer













                I know this is an older post, but someone will stumble across it in a Google search and it also looks like some questions the OP asked in comments weren't specifically addressed. Also, please go easy on me since this is my first answer posted on SO. :)



                To redirect the output to a file using a dynamically generated file name, my go-to (read: quick & dirty) approach is the second solution offered by @dbenham. So for example, this:



                @echo off
                > filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log (
                echo Your Name Here
                echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                REM do some stuff here
                echo Your Name Here
                echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
                )


                Will create a file like what you see in this screenshot of the file in the target directory



                That will contain this output:



                Your Name Here
                Beginning Date/Time: 2016-09-16_141048.log
                Your Name Here
                Ending Date/Time: 2016-09-16_141048.log


                Also keep in mind that this solution is locale-dependent, so be careful how/when you use it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 16 '16 at 18:20









                AnthonyAnthony

                15617




                15617













                • Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?

                  – Moondra
                  Jan 14 '18 at 18:50






                • 1





                  @Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.

                  – Anthony
                  Apr 17 '18 at 18:32



















                • Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?

                  – Moondra
                  Jan 14 '18 at 18:50






                • 1





                  @Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.

                  – Anthony
                  Apr 17 '18 at 18:32

















                Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?

                – Moondra
                Jan 14 '18 at 18:50





                Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?

                – Moondra
                Jan 14 '18 at 18:50




                1




                1





                @Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.

                – Anthony
                Apr 17 '18 at 18:32





                @Moondra no it doesn't. It's just semantics in this case. That's not to say there aren't applications that require/grep based on file type, so just be aware of if/how your files are being used.

                – Anthony
                Apr 17 '18 at 18:32











                8














                @echo off
                >output.txt (
                echo Checking your system infor, Please wating...

                systeminfo | findstr /c:"Host Name"
                systeminfo | findstr /c:"Domain"

                ipconfig /all | find "Physical Address"

                ipconfig | find "IPv4"
                ipconfig | find "Default Gateway"

                )

                @pause





                share|improve this answer


























                • I think that this is the most elegant way. Thanks Wesley!

                  – Kiswanij
                  Aug 23 '18 at 4:37
















                8














                @echo off
                >output.txt (
                echo Checking your system infor, Please wating...

                systeminfo | findstr /c:"Host Name"
                systeminfo | findstr /c:"Domain"

                ipconfig /all | find "Physical Address"

                ipconfig | find "IPv4"
                ipconfig | find "Default Gateway"

                )

                @pause





                share|improve this answer


























                • I think that this is the most elegant way. Thanks Wesley!

                  – Kiswanij
                  Aug 23 '18 at 4:37














                8












                8








                8







                @echo off
                >output.txt (
                echo Checking your system infor, Please wating...

                systeminfo | findstr /c:"Host Name"
                systeminfo | findstr /c:"Domain"

                ipconfig /all | find "Physical Address"

                ipconfig | find "IPv4"
                ipconfig | find "Default Gateway"

                )

                @pause





                share|improve this answer















                @echo off
                >output.txt (
                echo Checking your system infor, Please wating...

                systeminfo | findstr /c:"Host Name"
                systeminfo | findstr /c:"Domain"

                ipconfig /all | find "Physical Address"

                ipconfig | find "IPv4"
                ipconfig | find "Default Gateway"

                )

                @pause






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 20 '15 at 20:35









                Wesley Bland

                6,74833249




                6,74833249










                answered Feb 20 '15 at 20:07









                saifsaif

                8111




                8111













                • I think that this is the most elegant way. Thanks Wesley!

                  – Kiswanij
                  Aug 23 '18 at 4:37



















                • I think that this is the most elegant way. Thanks Wesley!

                  – Kiswanij
                  Aug 23 '18 at 4:37

















                I think that this is the most elegant way. Thanks Wesley!

                – Kiswanij
                Aug 23 '18 at 4:37





                I think that this is the most elegant way. Thanks Wesley!

                – Kiswanij
                Aug 23 '18 at 4:37











                7














                echo some output >"your logfile"


                or



                (
                echo some output
                echo more output
                )>"Your logfile"


                should fill the bill.



                If you want to APPEND the output, use >> instead of >. > will start a new logfile.






                share|improve this answer




























                  7














                  echo some output >"your logfile"


                  or



                  (
                  echo some output
                  echo more output
                  )>"Your logfile"


                  should fill the bill.



                  If you want to APPEND the output, use >> instead of >. > will start a new logfile.






                  share|improve this answer


























                    7












                    7








                    7







                    echo some output >"your logfile"


                    or



                    (
                    echo some output
                    echo more output
                    )>"Your logfile"


                    should fill the bill.



                    If you want to APPEND the output, use >> instead of >. > will start a new logfile.






                    share|improve this answer













                    echo some output >"your logfile"


                    or



                    (
                    echo some output
                    echo more output
                    )>"Your logfile"


                    should fill the bill.



                    If you want to APPEND the output, use >> instead of >. > will start a new logfile.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 10 '13 at 1:15









                    MagooMagoo

                    60.2k54068




                    60.2k54068























                        3














                        There is a cool little program you can use to redirect the output to a file and the console



                        some_command  ^|  TEE.BAT  [ -a ]  filename 





                        @ECHO OFF
                        :: Check Windows version
                        IF NOT "%OS%"=="Windows_NT" GOTO Syntax

                        :: Keep variables local
                        SETLOCAL

                        :: Check command line arguments
                        SET Append=0
                        IF /I [%1]==[-a] (
                        SET Append=1
                        SHIFT
                        )
                        IF [%1]== GOTO Syntax
                        IF NOT [%2]== GOTO Syntax

                        :: Test for invalid wildcards
                        SET Counter=0
                        FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
                        IF %Counter% GTR 1 (
                        SET Counter=
                        GOTO Syntax
                        )

                        :: A valid filename seems to have been specified
                        SET File=%1

                        :: Check if a directory with the specified name exists
                        DIR /AD %File% >NUL 2>NUL
                        IF NOT ERRORLEVEL 1 (
                        SET File=
                        GOTO Syntax
                        )

                        :: Specify /Y switch for Windows 2000 / XP COPY command
                        SET Y=
                        VER | FIND "Windows NT" > NUL
                        IF ERRORLEVEL 1 SET Y=/Y

                        :: Flush existing file or create new one if -a wasn't specified
                        IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

                        :: Actual TEE
                        FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
                        > CON ECHO.%%B
                        >> %File% ECHO.%%B
                        )

                        :: Done
                        ENDLOCAL
                        GOTO:EOF

                        :Count
                        SET /A Counter += 1
                        SET File=%1
                        GOTO:EOF

                        :Syntax
                        ECHO.
                        ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
                        ECHO Display text on screen and redirect it to a file simultaneously
                        ECHO.
                        IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
                        IF NOT "%OS%"=="Windows_NT" GOTO Skip
                        ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
                        :Skip
                        ECHO.
                        ECHO Where: "some_command" is the command whose output should be redirected
                        ECHO "filename" is the file the output should be redirected to
                        ECHO -a appends the output of the command to the file,
                        ECHO rather than overwriting the file
                        ECHO.
                        ECHO Written by Rob van der Woude
                        ECHO http://www.robvanderwoude.com
                        ECHO Modified by Kees Couprie
                        ECHO http://kees.couprie.org
                        ECHO and Andrew Cameron








                        share|improve this answer






























                          3














                          There is a cool little program you can use to redirect the output to a file and the console



                          some_command  ^|  TEE.BAT  [ -a ]  filename 





                          @ECHO OFF
                          :: Check Windows version
                          IF NOT "%OS%"=="Windows_NT" GOTO Syntax

                          :: Keep variables local
                          SETLOCAL

                          :: Check command line arguments
                          SET Append=0
                          IF /I [%1]==[-a] (
                          SET Append=1
                          SHIFT
                          )
                          IF [%1]== GOTO Syntax
                          IF NOT [%2]== GOTO Syntax

                          :: Test for invalid wildcards
                          SET Counter=0
                          FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
                          IF %Counter% GTR 1 (
                          SET Counter=
                          GOTO Syntax
                          )

                          :: A valid filename seems to have been specified
                          SET File=%1

                          :: Check if a directory with the specified name exists
                          DIR /AD %File% >NUL 2>NUL
                          IF NOT ERRORLEVEL 1 (
                          SET File=
                          GOTO Syntax
                          )

                          :: Specify /Y switch for Windows 2000 / XP COPY command
                          SET Y=
                          VER | FIND "Windows NT" > NUL
                          IF ERRORLEVEL 1 SET Y=/Y

                          :: Flush existing file or create new one if -a wasn't specified
                          IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

                          :: Actual TEE
                          FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
                          > CON ECHO.%%B
                          >> %File% ECHO.%%B
                          )

                          :: Done
                          ENDLOCAL
                          GOTO:EOF

                          :Count
                          SET /A Counter += 1
                          SET File=%1
                          GOTO:EOF

                          :Syntax
                          ECHO.
                          ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
                          ECHO Display text on screen and redirect it to a file simultaneously
                          ECHO.
                          IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
                          IF NOT "%OS%"=="Windows_NT" GOTO Skip
                          ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
                          :Skip
                          ECHO.
                          ECHO Where: "some_command" is the command whose output should be redirected
                          ECHO "filename" is the file the output should be redirected to
                          ECHO -a appends the output of the command to the file,
                          ECHO rather than overwriting the file
                          ECHO.
                          ECHO Written by Rob van der Woude
                          ECHO http://www.robvanderwoude.com
                          ECHO Modified by Kees Couprie
                          ECHO http://kees.couprie.org
                          ECHO and Andrew Cameron








                          share|improve this answer




























                            3












                            3








                            3







                            There is a cool little program you can use to redirect the output to a file and the console



                            some_command  ^|  TEE.BAT  [ -a ]  filename 





                            @ECHO OFF
                            :: Check Windows version
                            IF NOT "%OS%"=="Windows_NT" GOTO Syntax

                            :: Keep variables local
                            SETLOCAL

                            :: Check command line arguments
                            SET Append=0
                            IF /I [%1]==[-a] (
                            SET Append=1
                            SHIFT
                            )
                            IF [%1]== GOTO Syntax
                            IF NOT [%2]== GOTO Syntax

                            :: Test for invalid wildcards
                            SET Counter=0
                            FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
                            IF %Counter% GTR 1 (
                            SET Counter=
                            GOTO Syntax
                            )

                            :: A valid filename seems to have been specified
                            SET File=%1

                            :: Check if a directory with the specified name exists
                            DIR /AD %File% >NUL 2>NUL
                            IF NOT ERRORLEVEL 1 (
                            SET File=
                            GOTO Syntax
                            )

                            :: Specify /Y switch for Windows 2000 / XP COPY command
                            SET Y=
                            VER | FIND "Windows NT" > NUL
                            IF ERRORLEVEL 1 SET Y=/Y

                            :: Flush existing file or create new one if -a wasn't specified
                            IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

                            :: Actual TEE
                            FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
                            > CON ECHO.%%B
                            >> %File% ECHO.%%B
                            )

                            :: Done
                            ENDLOCAL
                            GOTO:EOF

                            :Count
                            SET /A Counter += 1
                            SET File=%1
                            GOTO:EOF

                            :Syntax
                            ECHO.
                            ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
                            ECHO Display text on screen and redirect it to a file simultaneously
                            ECHO.
                            IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
                            IF NOT "%OS%"=="Windows_NT" GOTO Skip
                            ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
                            :Skip
                            ECHO.
                            ECHO Where: "some_command" is the command whose output should be redirected
                            ECHO "filename" is the file the output should be redirected to
                            ECHO -a appends the output of the command to the file,
                            ECHO rather than overwriting the file
                            ECHO.
                            ECHO Written by Rob van der Woude
                            ECHO http://www.robvanderwoude.com
                            ECHO Modified by Kees Couprie
                            ECHO http://kees.couprie.org
                            ECHO and Andrew Cameron








                            share|improve this answer















                            There is a cool little program you can use to redirect the output to a file and the console



                            some_command  ^|  TEE.BAT  [ -a ]  filename 





                            @ECHO OFF
                            :: Check Windows version
                            IF NOT "%OS%"=="Windows_NT" GOTO Syntax

                            :: Keep variables local
                            SETLOCAL

                            :: Check command line arguments
                            SET Append=0
                            IF /I [%1]==[-a] (
                            SET Append=1
                            SHIFT
                            )
                            IF [%1]== GOTO Syntax
                            IF NOT [%2]== GOTO Syntax

                            :: Test for invalid wildcards
                            SET Counter=0
                            FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
                            IF %Counter% GTR 1 (
                            SET Counter=
                            GOTO Syntax
                            )

                            :: A valid filename seems to have been specified
                            SET File=%1

                            :: Check if a directory with the specified name exists
                            DIR /AD %File% >NUL 2>NUL
                            IF NOT ERRORLEVEL 1 (
                            SET File=
                            GOTO Syntax
                            )

                            :: Specify /Y switch for Windows 2000 / XP COPY command
                            SET Y=
                            VER | FIND "Windows NT" > NUL
                            IF ERRORLEVEL 1 SET Y=/Y

                            :: Flush existing file or create new one if -a wasn't specified
                            IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

                            :: Actual TEE
                            FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
                            > CON ECHO.%%B
                            >> %File% ECHO.%%B
                            )

                            :: Done
                            ENDLOCAL
                            GOTO:EOF

                            :Count
                            SET /A Counter += 1
                            SET File=%1
                            GOTO:EOF

                            :Syntax
                            ECHO.
                            ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
                            ECHO Display text on screen and redirect it to a file simultaneously
                            ECHO.
                            IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
                            IF NOT "%OS%"=="Windows_NT" GOTO Skip
                            ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
                            :Skip
                            ECHO.
                            ECHO Where: "some_command" is the command whose output should be redirected
                            ECHO "filename" is the file the output should be redirected to
                            ECHO -a appends the output of the command to the file,
                            ECHO rather than overwriting the file
                            ECHO.
                            ECHO Written by Rob van der Woude
                            ECHO http://www.robvanderwoude.com
                            ECHO Modified by Kees Couprie
                            ECHO http://kees.couprie.org
                            ECHO and Andrew Cameron








                            @ECHO OFF
                            :: Check Windows version
                            IF NOT "%OS%"=="Windows_NT" GOTO Syntax

                            :: Keep variables local
                            SETLOCAL

                            :: Check command line arguments
                            SET Append=0
                            IF /I [%1]==[-a] (
                            SET Append=1
                            SHIFT
                            )
                            IF [%1]== GOTO Syntax
                            IF NOT [%2]== GOTO Syntax

                            :: Test for invalid wildcards
                            SET Counter=0
                            FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
                            IF %Counter% GTR 1 (
                            SET Counter=
                            GOTO Syntax
                            )

                            :: A valid filename seems to have been specified
                            SET File=%1

                            :: Check if a directory with the specified name exists
                            DIR /AD %File% >NUL 2>NUL
                            IF NOT ERRORLEVEL 1 (
                            SET File=
                            GOTO Syntax
                            )

                            :: Specify /Y switch for Windows 2000 / XP COPY command
                            SET Y=
                            VER | FIND "Windows NT" > NUL
                            IF ERRORLEVEL 1 SET Y=/Y

                            :: Flush existing file or create new one if -a wasn't specified
                            IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

                            :: Actual TEE
                            FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
                            > CON ECHO.%%B
                            >> %File% ECHO.%%B
                            )

                            :: Done
                            ENDLOCAL
                            GOTO:EOF

                            :Count
                            SET /A Counter += 1
                            SET File=%1
                            GOTO:EOF

                            :Syntax
                            ECHO.
                            ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
                            ECHO Display text on screen and redirect it to a file simultaneously
                            ECHO.
                            IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
                            IF NOT "%OS%"=="Windows_NT" GOTO Skip
                            ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
                            :Skip
                            ECHO.
                            ECHO Where: "some_command" is the command whose output should be redirected
                            ECHO "filename" is the file the output should be redirected to
                            ECHO -a appends the output of the command to the file,
                            ECHO rather than overwriting the file
                            ECHO.
                            ECHO Written by Rob van der Woude
                            ECHO http://www.robvanderwoude.com
                            ECHO Modified by Kees Couprie
                            ECHO http://kees.couprie.org
                            ECHO and Andrew Cameron





                            @ECHO OFF
                            :: Check Windows version
                            IF NOT "%OS%"=="Windows_NT" GOTO Syntax

                            :: Keep variables local
                            SETLOCAL

                            :: Check command line arguments
                            SET Append=0
                            IF /I [%1]==[-a] (
                            SET Append=1
                            SHIFT
                            )
                            IF [%1]== GOTO Syntax
                            IF NOT [%2]== GOTO Syntax

                            :: Test for invalid wildcards
                            SET Counter=0
                            FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
                            IF %Counter% GTR 1 (
                            SET Counter=
                            GOTO Syntax
                            )

                            :: A valid filename seems to have been specified
                            SET File=%1

                            :: Check if a directory with the specified name exists
                            DIR /AD %File% >NUL 2>NUL
                            IF NOT ERRORLEVEL 1 (
                            SET File=
                            GOTO Syntax
                            )

                            :: Specify /Y switch for Windows 2000 / XP COPY command
                            SET Y=
                            VER | FIND "Windows NT" > NUL
                            IF ERRORLEVEL 1 SET Y=/Y

                            :: Flush existing file or create new one if -a wasn't specified
                            IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

                            :: Actual TEE
                            FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
                            > CON ECHO.%%B
                            >> %File% ECHO.%%B
                            )

                            :: Done
                            ENDLOCAL
                            GOTO:EOF

                            :Count
                            SET /A Counter += 1
                            SET File=%1
                            GOTO:EOF

                            :Syntax
                            ECHO.
                            ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP
                            ECHO Display text on screen and redirect it to a file simultaneously
                            ECHO.
                            IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename
                            IF NOT "%OS%"=="Windows_NT" GOTO Skip
                            ECHO Usage: some_command ^| TEE.BAT [ -a ] filename
                            :Skip
                            ECHO.
                            ECHO Where: "some_command" is the command whose output should be redirected
                            ECHO "filename" is the file the output should be redirected to
                            ECHO -a appends the output of the command to the file,
                            ECHO rather than overwriting the file
                            ECHO.
                            ECHO Written by Rob van der Woude
                            ECHO http://www.robvanderwoude.com
                            ECHO Modified by Kees Couprie
                            ECHO http://kees.couprie.org
                            ECHO and Andrew Cameron






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 11 '17 at 17:14









                            T.S.

                            9,769103253




                            9,769103253










                            answered Apr 24 '15 at 12:06









                            AquaAlexAquaAlex

                            275413




                            275413























                                2














                                @echo OFF
                                [your command] >> [Your log file name].txt


                                I used the command above in my batch file and it works. In the log file, it shows the results of my command.






                                share|improve this answer






























                                  2














                                  @echo OFF
                                  [your command] >> [Your log file name].txt


                                  I used the command above in my batch file and it works. In the log file, it shows the results of my command.






                                  share|improve this answer




























                                    2












                                    2








                                    2







                                    @echo OFF
                                    [your command] >> [Your log file name].txt


                                    I used the command above in my batch file and it works. In the log file, it shows the results of my command.






                                    share|improve this answer















                                    @echo OFF
                                    [your command] >> [Your log file name].txt


                                    I used the command above in my batch file and it works. In the log file, it shows the results of my command.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jul 18 '18 at 2:42









                                    Pang

                                    6,9011664101




                                    6,9011664101










                                    answered Jul 18 '18 at 2:36









                                    Indra PermanaIndra Permana

                                    215




                                    215























                                        1














                                        Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:



                                        if not "%1"=="STDOUT_TO_FILE"  %0 STDOUT_TO_FILE %*  >log.txt 2>&1
                                        shift /1





                                        share|improve this answer
























                                        • This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)

                                          – CCarlos
                                          Nov 23 '18 at 19:37
















                                        1














                                        Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:



                                        if not "%1"=="STDOUT_TO_FILE"  %0 STDOUT_TO_FILE %*  >log.txt 2>&1
                                        shift /1





                                        share|improve this answer
























                                        • This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)

                                          – CCarlos
                                          Nov 23 '18 at 19:37














                                        1












                                        1








                                        1







                                        Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:



                                        if not "%1"=="STDOUT_TO_FILE"  %0 STDOUT_TO_FILE %*  >log.txt 2>&1
                                        shift /1





                                        share|improve this answer













                                        Add these two lines near the top of your batch file, all stdout and stderr after will be redirected to log.txt:



                                        if not "%1"=="STDOUT_TO_FILE"  %0 STDOUT_TO_FILE %*  >log.txt 2>&1
                                        shift /1






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 1 '18 at 15:46









                                        ilgitanoilgitano

                                        6113




                                        6113













                                        • This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)

                                          – CCarlos
                                          Nov 23 '18 at 19:37



















                                        • This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)

                                          – CCarlos
                                          Nov 23 '18 at 19:37

















                                        This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)

                                        – CCarlos
                                        Nov 23 '18 at 19:37





                                        This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)

                                        – CCarlos
                                        Nov 23 '18 at 19:37


















                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20484151%2fredirecting-output-from-within-batch-file%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Liquibase includeAll doesn't find base path

                                        How to use setInterval in EJS file?

                                        Petrus Granier-Deferre