[Batch] suppress output

Discussion in 'Scripting' started by sulasno, Jan 13, 2011.

  1. sulasno

    sulasno MDL Novice

    Jan 8, 2011
    7
    0
    0
    I have the following lines in a batch file;

    @echo off
    if not exist "%userprofile%\desktop\Tools"\nul mkdir "%userprofile%\desktop\Tools"

    How do I suppress the following lines if the folder exists ?

    A subdirectory or file xxxxxxxxxx already exists.
     
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #2 Calistoga, Jan 13, 2011
    Last edited by a moderator: Apr 20, 2017
    It's how you pipe the output to the nul file.

    The following code works as expected:
    Code:
    @echo off
    set tools=%userprofile%\Desktop\Tools
    if not exist "%tools%" mkdir "%tools%" >> nul
     
  3. rEApEAt

    rEApEAt MDL Senior Member

    Jan 5, 2011
    355
    168
    10
    #3 rEApEAt, Jan 13, 2011
    Last edited by a moderator: Apr 20, 2017
    If you like spaghetti you may also try this:

    Code:
    @echo off
    if exist "%userprofile%\desktop\Tools"\nul goto option1
    if not exist "%userprofile%\desktop\Tools"\nul goto option2
    
    :option1
    (whatever)
    
    :option2
    mkdir "%userprofile%\desktop\Tools"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. veey

    veey MDL Novice

    Jan 17, 2011
    6
    0
    0
    Looks like you are always trying to create an existing directory.
    BTW output of mkdir cannot be suppressed by >nul
     
  5. Phazor

    Phazor MDL Expert

    Sep 1, 2009
    1,145
    517
    60
    #5 Phazor, Jan 19, 2011
    Last edited by a moderator: Apr 20, 2017
    Try 2>nul...:)

    Code:
    md "%userprofile%\desktop\Tools" 2>nul
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    105
    10
    #6 Compo, Mar 29, 2011
    Last edited by a moderator: Apr 20, 2017
    Nobody is piping or redirecting output, this old method of using \nul was used to identify the existence of a directory with command.com as the console.

    But like this:
    Code:
    IF NOT EXIST "%USERPROFILE%\DESKTOP\TOOLS\NUL" MKDIR "%USERPROFILE%\DESKTOP\Tools"
    I'd suggest this:
    Code:
    @ECHO OFF
    SETLOCAL
    (SET UDESK=%USERPROFILE%\DESKTOP)
    DIR /B /AD "%UDESK%\TOOLS" 1>NUL 2>&1 || MD "%UDESK%\TOOLS"