I'm testing the existence of a folder, and, depending on its existence, I want to run different commands:
DIR %MYDIR%\tmp > test.txt
IF ERRORLEVEL 1 (
echo/FOLDER DOES NOT EXIST
) else (
echo/FOLDER EXISTS
)
The problem is that if the folder doesn't exist I'm getting this error in addition to the standard output:
The system cannot find the file specified.
I'd like to display the correct output without getting the error.
Use
exist
instead:How about this:
"> nul" means to redirect standard output to the file nul (the bit bucket).
"2>" is used to redirect standard error (descriptor 2). So "2>&1" is used to redirect standard error to means that standard output (descriptor 1 -- so "> null and 1> null are be the same). Alternatively you could use "2> nul".
You might have run into a common problem that I have seen many times in my own scripts: not expecting a space in the file path.
The value of the MYDIR environment variable might have a space in it. When there is are spaces in a path, most commands see it as multiple parameters since spaces are used to separate parameters. So, in your case if MYDIR is
C:\Documents and Settings\Jim\My Documents
then your script would be attempting to perform a directory listing on several parameters:Try this in a command prompt:
Hmmm, three folders named "peas", "and", "carrots", (sorted alphabetically as is my preference for the dir command). To create a folder with spaces in the name, you must put quotes around the folder name:
To correct this problem in your case, add quotes around the file/folder path:
That eliminates the
system cannot find the file specified
even if the file does exist problem.Unfortunately, if the file/folder really does not exist, you will get that same error message. If you are insistent upon using the
DIR
command for this, then you will have to redirectstderr
stream. You have already redirectedstdout
using the>
redirect operator. To redirectstderr
to the same file, your command should look like this:Based on the output messages of your batch file, I am assuming you are only interested in determining if %MYDIR%\tmp exists, and you do not actually want the directory listing of %MYDIR%\tmp left in a file called test.txt in the current working directory. If you are simply checking if a file/folder exists, then using the
DIR
command is a pretty bad choice. If %MYDIR%\tmp is a folder, then the script would be wasting time retrieving the directory information for every file in that folder. If there are thousands of files in that folder, it could result in a noticeable delay. In addition, you have deposited a file named test.txt in the current working directory which you should delete before you script exits... you know... assuming you didn't really want that file in the first place.If you simply want to know if the folder or file exists there is a much better choice: The
if exist
command is perfectly suited for your needs. To find out more about theif
command, in a Windows Command Prompt window, type:if /?
Example:
At first glance the documentation implies that
if exist
works with files only, but rest assured it does work with folders as well as files.As you are reading through the
if
command's documentation, you may come across the phrase "if Command Extensions are enabled ..." On most systems Command Extensions are enabled by default, but I still like to make the second line of any scriptsetlocal EnableExtensions
. The first line is@echo off
, which is quickly change torem @echo off
until I have finished debugging.The
setlocal EnableExtensions
command does two things:If you want to impress your friends by writing sophisticated and useful scripts in Batch (a task that at one time was thought to be impossible), read carefully and grock the embedded documentation on each of these commands:
You will have to utilize every trick and nuance these commands have to offer to write really good scripts.
For example: The
set
command can be used for many tasks:Simple assignment:
set myDir=C:\Foo
Performing math:
set /a myCount=myCount + 1
Prompting for input:
set /p myColor=What is favorite color
Extracting substrings:
set myCentury=%myYear:~0,2%
Here is how I would have written your script
Good luck and happy hunting.