23

I am on the windows console trying to find out whether a file/folder exists or not.

EXIST could be used in batch, but it is not available on the command-line:

C:\Users\WIN7PR~1>EXIST C:\Users
'EXIST' is not recognized as an internal or external command, operable program or batch file.
0

5 Answers 5

24

The solution when the resource is a file it is pretty straight-forward as indicated by others:

C:\> IF EXIST C:\CONFIG.SYS ECHO C:\CONFIG.SYS exists.

Unfortunately, the above does not work for directories. The EXIST function returns the same result for both missing and present folders. Fortunately, there is an obscure workaround:

C:\> IF NOT EXIST C:\FOLDER\NUL ECHO C:\FOLDER missing.
C:\FOLDER missing.
C:\> MD C:\FOLDER
C:\> IF EXIST C:\FOLDER\NUL ECHO C:\FOLDER exists.
C:\FOLDER exists.

It turns out that to support constructs like appending >NUL on command statements, there is a sort of virtual file named "NUL" in every directory. Checking for its existence is equivalent to a check for the directory's existence.

This behavior is documented in a Microsoft knowledge base article ( https://support.microsoft.com/en-us/kb/65994 ) and I have confirmed its behavior on FreeDOS 1.1 and in a Windows 7 command shell.

EXTRA: The KB article indicates this technique can also be used to see if a drive is present. In the case of checking for drive existence, however, caveats exist:

  • An Abort, Retry, Fail? error occurs if the drive is not formatted.

  • Using this technique to check for drive existence depends on device driver implementation and may not always work.

2
  • This does work on FreeDOS 1.1 and is documented at support.microsoft.com/en-us/kb/65994 . If you say it does not work on Windows 7, then apparently CMD is not DOS ;-), however, I disagree that it does not work as I just tried it on Windows 7.
    – kbulgrien
    Apr 12, 2016 at 12:50
  • Windows stopped being DOS around 1999-2000, when Microsoft finally dropped the Windows 9x line of products and went with the Windows NT line (NT 4, Windows 2000, Windows XP, Windows Vista, ...).
    – user
    Apr 12, 2016 at 13:03
13

You can use a simple

DIR C:\User
0
7

You can use type command, it will return the contents of a text file without opening it, and for a directory it will return: Access is denied.

If the file or directory is not available you get the message: The system cannot find the file specified.

So for example:

C:\>type c:\temp
Access is denied.

C:\>type c:\example.txt
Some example content in a text file

C:\>type c:\doesnotexist
The system cannot find the file specified.
1
  • +1, this is pretty handy for scripting like in Lua being able to check directory without needing 3rd party module.
    – greatwolf
    Feb 3, 2020 at 19:31
4

Just put if on the front :)

if exist C:\Users echo It exists!
2
  • 1
    Returns: C:\Users was unexpected at this time.
    – Vaccano
    Aug 25, 2014 at 20:27
  • 1
    The correct format is if exist <path> (true operation). E.g. if exist "C:\Path with spaces" (echo It exists!) ELSE (echo It doesn't exist!)
    – matt.baker
    Feb 16, 2023 at 14:13
0

You can use this code:

<pre>
:init
SETLOCAL enabledelayedexpansion
GOTO make_dir

:make_dir
ECHO .
ECHO Checking if exists directory %out_path% ...
CD %out_path%
IF !ERRORLEVEL! GTR 0 (
   ECHO Directory doesn't exist, creating...
   MD %out_path%
   GOTO make_dir
) ELSE (
   ECHO Directory already exists.
)
:GOTO back_it_up

:back_it_up
::Procedure that makes an backup
GOTO done

:done
ECHO Finished
SETLOCAL
EXIT /B
</pre>
1
  • My question is about command line. Would you mind giving an example command line? Also, preferably it should work on read-only filesystems too. Dec 2, 2020 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .