24

Let's say I already have a folder created on the next path file: "C:\users\charqus\desktop\MyFolder", and I run the next command on CMD:

mkdir "C:\users\charqus\desktop\MyFolder"

I get a message like this: "A subdirectory or file C:\users\charqus\desktop\MyFolder already exists".

Therefore, is there any command in the commandline to get rid of this returned messages? I tried echo off but this is not what I looking for.

charqus
  • 469
  • 1
  • 5
  • 15
  • 5
    Easy enough.. `IF NOT EXIST "C:\users\charqus\desktop\MyFolder\." mkdir "C:\users\charqus\desktop\MyFolder"` – Leptonator Sep 01 '15 at 14:38

2 Answers2

56

Redirect the output to nul

mkdir "C:\users\charqus\desktop\MyFolder" > nul

Depending on the command, you may also need to redirect errors too:

mkdir "C:\users\charqus\desktop\MyFolder" > nul 2> nul

Microsoft describes the options here, which is useful reading.

Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • So, if I use only once > nul, this will get me rid of warnings, and if I use > nul 2 > nul, this will get me rid of warnings and errors, am I right? – charqus Nov 30 '13 at 09:52
  • 3
    Yes, strictly speaking `>nul` redirects `stdout` and `2>nul` redirects `stderr`, which are the two output streams you usually see in a command window. Which one(s) is(are) used depends on the command itself. – Roger Rowland Nov 30 '13 at 10:08
  • 7
    To the OP, the second redirection can be `2>&1` which will send STDERR to the same place that STDOUT is already being redirected to. It's used more often in practice than using >nul twice, so you'll see it in code. – foxidrive Dec 01 '13 at 05:12
  • 2>nul mkdir "C:\users\charqus\desktop\MyFolder" is also possible and this style was required in teamcity – 79E09796 Oct 05 '15 at 14:31
6

A previous answer shows how to squelch all the output from the command. This removes the helpful error text that is displayed if the command fails. A better way is shown in the following example:

C:\test>dir
     Volume in drive C has no label.
     Volume Serial Number is 4E99-B781

     Directory of C:\test

    20/08/2015  20:18    <DIR>          .
    20/08/2015  20:18    <DIR>          ..
                   0 File(s)              0 bytes
                   2 Dir(s)  214,655,188,992 bytes free

C:\test>dir new_dir >nul 2>nul || mkdir new_dir >nul 2>nul || mkdir new_dir

C:\test>dir new_dir >nul 2>nul || mkdir new_dir >nul 2>nul || mkdir new_dir

As is demonstrated above this command successfully suppress the original warning. However, if the directory can not be created, as in the following example:

C:\test>icacls c:\test /deny "Authenticated Users":(GA)
processed file: c:\test
Successfully processed 1 files; Failed processing 0 files

C:\test>dir new_dir2 >nul 2>nul || mkdir new_dir2 >nul 2>nul || mkdir new_dir2
Access is denied.

Then as can be seen, an error message is displayed describing the problem.

user1976
  • 353
  • 4
  • 15