How do I make a Windows batch script completely silent?

后端 未结 5 1819
天涯浪人
天涯浪人 2020-12-13 03:33

There has been variants of this question asked for generations, but despite writing some quite complicated Windows scripts, I can\'t seem to find out how to make them actual

相关标签:
5条回答
  • 2020-12-13 04:05

    If you want that all normal output of your Batch script be silent (like in your example), the easiest way to do that is to run the Batch file with a redirection:

    C:\Temp> test.bat >nul
    

    This method does not require to modify a single line in the script and it still show error messages in the screen. To supress all the output, including error messages:

    C:\Temp> test.bat >nul 2>&1
    

    If your script have lines that produce output you want to appear in screen, perhaps will be simpler to add redirection to those lineas instead of all the lines you want to keep silent:

    @ECHO OFF
    SET scriptDirectory=%~dp0
    COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat
    FOR /F %%f IN ('dir /B "%scriptDirectory%*.noext"') DO (
    del "%scriptDirectory%%%f"
    )
    ECHO
    REM Next line DO appear in the screen
    ECHO Script completed >con
    

    Antonio

    0 讨论(0)
  • 2020-12-13 04:06

    To suppress output, use redirection to NUL.

    There are two kinds of output that console commands use:

    • standard output, or stdout,

    • standard error, or stderr.

    Of the two, stdout is used more often, both by internal commands, like copy, and by console utilities, or external commands, like find and others, as well as by third-party console programs.

    >NUL suppresses the standard output and works fine e.g. for suppressing the 1 file(s) copied. message of the copy command. An alternative syntax is 1>NUL. So,

    COPY file1 file2 >NUL
    

    or

    COPY file1 file2 1>NUL
    

    or

    >NUL COPY file1 file2
    

    or

    1>NUL COPY file1 file2
    

    suppresses all of COPY's standard output.

    To suppress error messages, which are typically printed to stderr, use 2>NUL instead. So, to suppress a File Not Found message that DEL prints when, well, the specified file is not found, just add 2>NUL either at the beginning or at the end of the command line:

    DEL file 2>NUL
    

    or

    2>NUL DEL file
    

    Although sometimes it may be a better idea to actually verify whether the file exists before trying to delete it, like you are doing in your own solution. Note, however, that you don't need to delete the files one by one, using a loop. You can use a single command to delete the lot:

    IF EXIST "%scriptDirectory%*.noext" DEL "%scriptDirectory%*.noext"
    
    0 讨论(0)
  • 2020-12-13 04:08

    You can redirect stdout to nul to hide it.

    COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat >nul
    

    Just add >nul to the commands you want to hide the output from.

    Here you can see all the different ways of redirecting the std streams.

    0 讨论(0)
  • 2020-12-13 04:20

    Copies a directory named html & all its contents to a destination directory in silent mode. If the destination directory is not present it will still create it.

    @echo off
    TITLE Copy Folder with Contents
    
    set SOURCE=C:\labs
    set DESTINATION=C:\Users\MyUser\Desktop\html
    
    xcopy %SOURCE%\html\* %DESTINATION%\* /s /e /i /Y >NUL
    

    1. /S Copies directories and subdirectories except empty ones.

    2. /E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.

    3. /I If destination does not exist and copying more than one file, assumes that destination must be a directory.

    4. /Y Suppresses prompting to confirm you want to overwrite an existing destination file.
    0 讨论(0)
  • 2020-12-13 04:22

    Just add a >NUL at the end of the lines producing the messages.

    For example,

    COPY %scriptDirectory%test.bat %scriptDirectory%test2.bat >NUL

    0 讨论(0)
提交回复
热议问题