Test if Network Directory exists in Batch file (.cmd)

The default way to check whether a directory exists in a Windows batch file (.cmd) is

if not exist "%directory%\nul" (
   echo %directory% does not exist

However, as this MS KB explains, the check for the NUL file does not work with directories on network drives mapped to a drive letter.

A working solution I found is to process the ERRORLEVEL value a DIR command sets

dir %directory% >nul 2>nul
if errorlevel 1 (
   echo %directory does not exist


dir %directory% >nul 2>nul
if not errorlevel 1 (
    echo %directory exists

Also note that mapped network drives are not available in administrator mode, as is discussed in these threads.

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

  1. Pingback: Test if Directory exists in Batch file (.cmd) « devioblog

  2. Pingback: Fifth Anniversary « devioblog

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 )

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.