I tried so many possibilities to achieve this but I keep on getting errors.
I dont know why I can't pass %CD%
and %PATH%
to FINDSTR
.
@echo off
findstr %cd% %path%
echo %errorlevel%
pause
The result is findstr
can't take value from %path%
because it is not a file so I tried to echo
it to file.
@echo off
echo %path% > path.txt
findstr %cd% path.txt
echo %errorlevel%
pause
For now findstr
could open path.txt
but couldn't get the string to compare. The %cd%
didn't appear to work so I tried to put it manually like this:
@echo off
echo %path% > path.txt
findstr c:\foo path.txt
echo %errorlevel%
pause
It works!
So how can I get the current directory value and pass the value to findstr
? or more plainly, how do I detect if the current directory exists within %PATH% variable.
for %%a in (echo "%path:;=" "%") do if /i "%cd%"=="%%a" echo "found it"
should do this for most situations but there are exceptions.
path
may contain relative paths (.
or ..
) which will not be detected.
path
may contain "some;directory"
which will not play nicely
and there is no requirement that the drivename appears in path
.
So - use with caution.
Run the following small example script which uses FIND
and FINDSTR
with conditionals and also an IF
/ELSE
:
@Echo Off
Echo(%PATH%|Find/I "%CD%;">Nul&&(Echo(Found)||Echo(Not Found
Timeout 2
Echo(%PATH%|FindStr/I "%CD%;">Nul&&(Echo(Found)||Echo(Not Found
Timeout 2
SetLocal EnableDelayedExpansion
If /I "!PATH:%CD%;=!"=="!PATH!" (Echo(Not Found) Else Echo(Found
EndLocal
Timeout -1
So that is three different attempts at the same task, how do they work for you?
There is not any universal, simple, direct way to check if the current folder is included in the path
variable using findstr
because for each referenced folder inside path
: it can be an absolute or relative reference, it can include or not an ending backslash, it can be or not quoted, it can include or not special characters, it can include (if quoted) semicolons, ...
In top of that, in order to use findstr
to do the check you will need to handle problems with the backslash characters as they are used as escape characters in regular expressions but also in literals when preceding a non alphanumeric character. Try
echo x:\_uno\ | findstr /L /c:"x:\_uno" && echo Yes || echo No
So, you will need to process each value inside the path
variable dealing with quoted semicolons, special characters, backslashes, ...
Fortunately this was solved by Jeb and dbenham in the 'Pretty print' windows %PATH% variable - how to split on ';' in CMD shell. Using their code to enumerate the elements it the path
variable, and the approach in the Magoo's answer in this question, we can write somenthing like
@echo off
setlocal enableextensions disabledelayedexpansion
rem Flag variable. Assume current folder is not present in path variable
set "present="
rem This code uses:
rem Q: Pretty print %path% https://stackoverflow.com/q/5471556
rem A: Jeb answer https://stackoverflow.com/a/5472168
rem A: dbenham enhancement https://stackoverflow.com/a/7940444
set "var=%path:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"
set "var=%var:;=^;^;%"
set var=%var:""="%
set "var=%var:"=""Q%"
set "var=%var:;;="S"S%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
setlocal EnableDelayedExpansion
set "var=!var:"Q=!"
rem Get a reference to current folder (%%c) and check against each
rem delimited value inside the processed variable
for %%c in (.) do for %%a in ("!var:"S"S=";"!") do (
if "!!"=="" endlocal
if %%a neq "" for %%b in ("%%~fa.") do (
if /i "%%~fb"=="%%~fc" set "present=1"
)
)
if defined present (
echo Current folder is INCLUDED in path variable
) else (
echo Current folder is NOT included in path variable
)
For each element in the path, resolve it to the full qualified path and check against the full qualified path of the current folder.