I tried to run grep.exe
(Unix version compiled for Windows) from a batch script and got this error:
0 [main] grep 7760 C:\grep.exe: *** fatal error - add_item ("\??", "/", ...) failed, errno 22
Stack trace:
Frame Function Args
000FFFFABB6 0018007215E (00180267E4A, 00180218E59, 00600010000, 000FFFF8B30)
000FFFFABB6 00180046E52 (000FFFF9B98, 000FFFFABB6, 00000000000, 00000000000)
000FFFFABB6 00180046E92 (000FFFF9BB0, 00000000016, 00600010000, 000003F3F5C)
000FFFFABB6 001800DEEFD (000FFFFCBB0, 000FFFFCE00, 001800CF158, 00000000000)
000FFFFCC00 00180128545 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFCCC0 001800474B5 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000 001800460AC (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0 00180046144 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace
This above error message is shown after running the following line in the batch script:
for /f "usebackq delims=" %%a in (`reg query "hklm\system\currentcontrolset\control\session manager\environment" /v Path ^| %~dp0grep -i directory_being_searched_for `) do set pathExists=true
This line of code might look complicated at first, but the grep
command near the end is what's being executed eventually, which fails at some point.
What seems to be causing this failure?
If there's no easy solution, I don't mind workarounds.
%~dp0grep
should be enclosed in double quotes in case of path to grep.exe
(= batch file path) contains a space or another syntax critical character from this list &()[]{}^=;!'+,`~
output by command interpreter itself on running in a command prompt window cmd /?
on last page.
And the file extension .exe
should be also appended to avoid the need to search for an executable by Windows command interpreter in batch file directory.
But the main problem causing the fatal error is most likely the string replaced in question by
directory_being_searched_for
?
or *
or .
?grep is ported from Unix to Windows. Therefore grep interprets a backslash as an escape character like C/C++/C#/JavaScript and many other programming and scripting languages and not as directory separator like Windows.
I don't have installed grep (which version downloaded from where?) and therefore can't reproduce this issue and find out the reason for the fatal error.
But perhaps the solution is using grep option -F
to let grep interpret the search string pattern as fixed string instead of regular expression.
for /F "usebackq delims=" %%a in (`%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| "%~dp0grep.exe" -i -F "directory_being_searched_for"`) do set "pathExists=true"
But there are easier solutions as Windows has the console applications find and findstr for simple fixed string searches and simple regular expression searches.
Example with find:
for /F "usebackq delims=" %%a in (`%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| "%SystemRoot%\System32\find.exe" "directory_being_searched_for"`) do echo set "pathExists=true"
Operator |
redirects the output of reg to handle STDOUT to the input handle STDIN of find. |
must be escaped here with ^
to avoid being interpreted for command for which would result in an exit of batch processing because of a syntax error. Please see Microsoft article about Using command redirection operators for more details.
But this simple command line does not verify if the complete directory path is really included in environment variable PATH as defined in Windows registry. A better verification would be for example:
@echo off
setlocal EnableDelayedExpansion
set "PathExists=false"
for /F "tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path') do (
if /I "%%A"=="Path" (
set "SystemPath=;%%C;"
set "SystemPath=!SystemPath:"=!"
if not "!SystemPath:;directory_being_searched_for;=!" == "!SystemPath!" set "PathExists=true"
)
)
echo Path exists: %PathExists%
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
find /?
for /?
if /?
reg /?
reg query /?
set /?
setlocal /?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments