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 (
    call :testdir "%%~a"
    setlocal EnableDelayedExpansion
goto :eof

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


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

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


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