posted by jwerner on Wednesday, December 5, 2012 9:45 AM

Over time, many programs may add directories to your PATH environment variable.
The following small batch file checks each entry in the PATH variable and displays if the directory exists or not.

The following code is based on this StackOverflow code.

Put the following code into a text file, e.g. C:\validatepath.bat:

@echo off
setlocal DisableDelayedExpansion
set "var=%PATH%"

set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"

set "var=%var:;=^;^;%"
rem ** This is the key line, the missing quote is intention
set var=%var:""="%
set "var=%var:"=""%"

set "var=%var:;;="";""%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
set "var=%var:"=""%"
set "var=%var:"";""=";"%"
set "var=%var:"""="%"

setlocal EnableDelayedExpansion
for %%a in ("!var!") do (
    endlocal
    call :testdir "%%~a"
    setlocal EnableDelayedExpansion
)
goto :eof

:testdir
if exist %1 echo OK:  %1
if not exist %1 echo NOK: %1

When run, it should output something like:

 OK: C:\Users\abcde
NOK: C:\this\is\no\dir

Bonus

To just show the directories which don't exist, run this:

validatepath | findstr /b /r /c:"^ *NOK: *.*"

Comments

No comments


Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.

Add comment

Change Log

Created OnDec 5, 2012 9:45:23 AM CET
Created ByJoachim Werner
Updated OnDec 5, 2012 10:11:53 AM CET
Updated ByJoachim Werner