Test if Directory exists in Batch file (.cmd)

My previous post on testing network drives led me to further research the topic, and I came to quite surprising (at least for me) results: the result if a check with IF EXIST depend on whether

  • the drive is a local drive or a mapped network drive or a UNC path
  • the path contains spaces or not
  • the path is quoted or not
  • cmd runs in administrator mode or user mode

I wrote a small batch file that contains a couple of assignments of the form

set dir=c:\temp
set dir=c:\temp\with spaces
etc.

and executed these tests on each value

if exist %dir% echo exists
if exist %dir%\nul echo exists
if exist %dir%\. echo exists
if exist "%dir%" echo exists
if exist "%dir%\nul" echo exists
if exist "%dir%\." echo exists

These are the results

directory %dir% %dir%\nul %dir\. “%dir%” “%dir%\nul” “%dir%\.”
local x x x x x
local (spaces) x x
mapped (non-admin) x x x x x
mapped (non-admin, spaces) x x
UNC x x x x x x
UNC (spaces) x x x

Comments:

Testing directory path containing spaces can only be performed using the quoted notation.

Mapped network drives can only be access in non-administrator mode (see these threads).

The only reliable way to test for directory existence is therefore to use the quoted “%dir%\.” notation.

To check whether cmd runs in administrator mode or not, use an admin statement such as ‘at’:

at >nul 2>nul
if errorlevel 1 echo you are not in administrator mode

10 thoughts on “Test if Directory exists in Batch file (.cmd)

  1. This article has been very helpful in debugging a few problems I had with testing for directories…. thank you so much!!!

  2. Pingback: Fifth Anniversary « devioblog

  3. Pingback: New Year 2014 | devioblog

  4. It is obvious that BATCH programming is reaching it’s end of life. MS seems not be supporting this anymore and batch programming stays behind all the new features being introduced in newer OS versions, becoming incompatible with it. Sure, basic things can be easily achieved using BATCH, but more sophisticated scripts that worked best lately in Windows XP are becoming worse and worse manageable and even slight inconsistency in different language OS/regional settings makes any script dependent on it to fail. e.g. DATE result, PING result, now files/dirs not being recognized by EXIST command …. it is the end.
    Time to move on to another programming language. PowerShell for Windows environment…. may be.
    Or AutoIt…

  5. Unfortunately, dir “\folder\.” will not work if the “folder” is actually a junction (vs a real directory). Example: c:\documents and settings\” on a Win 7 machine.

  6. Your solution works better than the classic

    if exist “%~1\nul” echo FOO

    At least in Win8.1 yours handles correctly file names with spaces.

    Maybe I wil have to change all my MSdos.bat files… Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.