Skip navigation

JSI Tip 9056. How can I test if a folder is in my PATH?


Using the command to return your %PATH%, one line at a time, I have scripted IsInPath.bat to test if a folder is in your %PATH%.

The syntax for using IsInPath.bat is:

call IsInPath FolderPath Answer

Where FolderPath is the folder path that you wish to check and Answer is a call directed environment variable that will contain a Y if the FolderPath is in the %PATH%, or an N if the FolderPath is NOT in the %PATH%.

NOTE: You can use Pathman.exe to add the FolderPath to the %PATH%.

IsInPath.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: IsInPath Folder Answer&goto :EOF
setlocal ENABLEDELAYEDEXPANSION
set folder=%1##
set folder=%folder:"=%
set folder=%folder:;##=##%
set folder=%folder:\##=%
set folder=%folder:##=%
set ans=N
for %%f in ("%PATH:;=" "%") do (
 for /f "Tokens=*" %%a in ('@echo %%f^|Find /I 
%Folder%\
') do ( set ans=Y ) if "!ans!" EQU "N" for /f "Tokens=*" %%a in ('@echo %%f^|Find /I
%Folder%
') do ( set ans=Y ) ) endlocal&set %2=%ans%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish