Skip navigation

JSI Tip 3579. How can I tell if a file is located in my PATH, or other set of folders?

In tip 0494, we learned how to parse a batch parameter.

To determine whether a file is located within your PATH, use this script snippet:

set test=%~$PATH:1
if "%test%" EQU "" goto not_in_path

NOTE: %~$PATH:1 searches the folders in your PATH environment variable and returns a fully qualified name for the first occurance found. If not found, or the environment variable does not exist, the expression expands to a null string.

To determine whether batch parameter 2 exists in a list of folder, parameter 1, use:

set list=%1
REM Remove the "s
set list=%list:"=%
set test=%~$list:2
if "%test%" EQU "" goto not_in_path

NOTE: %~$list:2 searches the folders in your list environment variable and returns a fully qualified name for the first occurrence found. If not found, or the environment variable does not exist, the expression expands to a null string.

Example:

call testscript "c:\Documents and Setting\%username%\My Documents;c:;D:\folder" boot.ini

will set test to c:\boot.ini.




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