Redirecting Output from within Batch file

349,858

Solution 1

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

Edit 2020-04-17

Every now and then you may want to repeatedly write to two or more files. You might also want different messages on the screen. It is still possible to to do this efficiently by redirecting to undefined handles outside a parenthesized block or subroutine, and then use the & notation to reference the already opened files.

call :sub 9>File1.txt 8>File2.txt
exit /b

:sub
echo Screen message 1
>&9 echo File 1 message 1
>&8 echo File 2 message 1
echo Screen message 2
>&9 echo File 1 message 2
>&8 echo File 2 message 2
exit /b

I chose to use handles 9 and 8 in reverse order because that way is more likely to avoid potential permanent redirection due to a Microsoft redirection implementation design flaw when performing multiple redirections on the same command. It is highly unlikely, but even that approach could expose the bug if you try hard enough. If you stage the redirection than you are guaranteed to avoid the problem.

3>File1.txt ( 4>File2.txt call :sub)
exit /b

:sub
etc.

Solution 2

if you want both out and err streams redirected

dir >> a.txt 2>&1

Solution 3

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.

Solution 4

@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

Solution 5

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:
349,858
user3085030
Author by

user3085030

Updated on March 05, 2021

Comments

  • user3085030
    user3085030 about 3 years

    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.

  • user3085030
    user3085030 over 10 years
    Thanks, that really helped. what would I need to add if I wanted to have the batch file output my name above whatever else its outputting, its just a formality, but I would like the output to have my name and date, at the start and end of the output.
  • Sam
    Sam about 8 years
    Love the solutions where I can set it for the remainder of the file
  • Jannes
    Jannes over 7 years
    Note that the call solution changes your %0 (to sub in this case) which may or may not be what you want.
  • dbenham
    dbenham over 7 years
    @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.
  • Honk
    Honk about 7 years
    echo whoami /user > log.log
  • Thariq Nugrohotomo
    Thariq Nugrohotomo about 7 years
    @dbenham your answer is great, but how if I also want to redirect stderr (error) to the file? Thank you.
  • dbenham
    dbenham about 7 years
    @ThariqNugrohotomo - >output.txt 2>&1
  • Aaron
    Aaron about 7 years
    +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
  • Moondra
    Moondra over 6 years
    Thanks this was useful. Quick question is there a reason why you are using .log vs .txt? Does that make a difference?
  • Moondra
    Moondra over 6 years
    @dbenham Is the third method using powershell commands? I've never seen : used in front of cmd commands.
  • dbenham
    dbenham over 6 years
    @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.
  • Anthony
    Anthony about 6 years
    @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.
  • G.Busato
    G.Busato almost 6 years
    You should explain that using '>' will overwrite the content in the file while using ">>" will append to it (to avoid cargo cult with other users !)
  • Jalal Kiswani
    Jalal Kiswani over 5 years
    I think that this is the most elegant way. Thanks Wesley!
  • CCarlos
    CCarlos over 5 years
    This worked for me, thanks. Any special considerations where this may fail? (e.g. output size overflow)
  • gunslingor
    gunslingor over 4 years
    The middle solution looks ideal for me because I'm calling many complicated batch files with the START command for parallel processing and I have only been concerned about how the CLI output looks; so I should be able to wrap each script in this, or each START command would be even more ideal if it's possible. Still, one question, can you split output and errors into two files with this method?
  • dbenham
    dbenham over 4 years
    @gunslinger - Sure, same as you would for a single command: >output.txt 2>err.txt (yourCommandList...). But if your command list includes START, then the STARTed command(s) will not be redirected.
  • Sandeep Singh
    Sandeep Singh about 4 years
    Hi, thanks for your answer! is there any way to redirect it on console as well?
  • Sandeep Singh
    Sandeep Singh about 4 years
    Hi, is there any way to redirect the output on console as well?
  • Kalpesh Soni
    Kalpesh Soni about 4 years
    thats what it does
  • Somebody
    Somebody almost 4 years
    if you want to append to the file use >> instead of >
  • Jake Henry
    Jake Henry almost 3 years
    Thanks this is the kind of solution I was looking for.
  • Kibonge Murphy
    Kibonge Murphy almost 2 years
    I must say. It worked like a charm. Used a batch file with these lines from Task Scheduler, Powershell and CMD without issues. The ">log.txt" can be replaced with ">>log.txt" to append to the file. In sum, nice little trick.