How to get Redirecting Output from within Batch file?

How to get  Redirecting Output from within Batch file?

Asked on October 24, 2018 in Windows.
Add Comment


  • 7 Answer(s)
    @echo off
    command1 >output.txt
    command2 >>output.txt
    ...
    commandN >>output.txt
    

    Easier to write, and faster in a better way.

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

    Another best way

    @echo off
    call :sub >output.txt
    exit /b
    :sub
    command1
    command2
    ...
    commandN
    
    Answered on October 24, 2018.
    Add Comment

     Both out and err streams redirected,

    dir >> a.txt 2>&1
    
    Answered on October 24, 2018.
    Add Comment

    To use the code,

    @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
    )
    

    It create the file like what we see in this screenshot of the file in the target directory.

    output is:

    Your Name Here
    Beginning Date/Time: 2016-09-16_141048.log
    Your Name Here
    Ending Date/Time: 2016-09-16_141048.log
    
    Answered on October 24, 2018.
    Add Comment

    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
    )
    

    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.

    Answered on January 19, 2019.
    Add Comment

    USE CODE:

    @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
    Answered on January 19, 2019.
    Add Comment

    To display a text on screen we have the ECHO command:

    ECHO Hello world

    This will show the following text on screen:

    Hello world

    When I say “on screen”, I’m actually referring to the “DOS Prompt”, “console” or “command window”, or whatever other “alias” is used

    Answered on February 17, 2019.
    Add Comment

    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
    
    Answered on February 21, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.