Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress error messages in Windows commandline

Tags:

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.

like image 898
charqus Avatar asked Nov 30 '13 09:11

charqus


People also ask

How do I supress errors in bash?

To suppress error output in bash , append 2>/dev/null to the end of your command. This redirects filehandle 2 (STDERR) to /dev/null .

How do you stop a code from command prompt?

You can also use the shortcut key Alt + F4 to close a Command Prompt window.

How do I stop a batch file from command line?

When pause suspends processing of the batch program, you can press CTRL+C and then press Y to stop the batch program.


2 Answers

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.

like image 195
Roger Rowland Avatar answered Sep 18 '22 08:09

Roger Rowland


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.

like image 24
user1976 Avatar answered Sep 19 '22 08:09

user1976