0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
Nichie
  • 1
  • 3
  • What are you trying to achieve? Do you want to find out if the current directory is part of your path? – J.Baoby Oct 14 '17 at 09:17
  • @J.Baoby yeah, like that. i already edit my question. – Nichie Oct 14 '17 at 09:22
  • Simple: `if "!path:%cd%=!" neq "%path%" echo Current directory IS in PATH`, but don't forget to include `setlocal EnableDelayedExpansion` line before... – Aacini Oct 14 '17 at 13:02
  • That code with added case insensitivity is in my answer @Aacini but apparently `its not work only echo not found.` – Compo Oct 14 '17 at 14:06
  • 1
    You have gotten help from some if the best when it comes to batch-files. If none of their code is working then it comes down to a PEBKAC error. – Squashman Oct 14 '17 at 15:19
  • @Compo: Ops! I didn't saw it... **`:(`** The case insensivity is not needed in this case. – Aacini Oct 14 '17 at 15:26

3 Answers3

0
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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

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?

Compo
  • 36,585
  • 5
  • 27
  • 39
0

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.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • thx for reply. i make discuss to move to real desktop programming. i see so many disadvantage batch file as i through this script. – Nichie Oct 15 '17 at 16:16
  • @Nichie, batch files are very useful, and for a lot of tasks they are the simplest approach, but sometimes they are a wrench we use to hammer a nail, it can solve the problem but it is not the right tool. But in this case, for the problem in your question, you will have to deal with the same indicated problems whatever the language you decide to use. – MC ND Oct 15 '17 at 18:28
  • the purpose my script is i want to act like antivirus that find something in list that i don't want and kill it. or block it. but the more i dig in batch. i see the disadvantage of batch files its not standalone app. it need cmd.exe to execute. and it can't mask or hide itself in process. – Nichie Oct 16 '17 at 16:18