批处理文件 - findstr 命令使用多个字符串进行搜索失败



此批处理文件使用逗号分隔符 (IP_List.txt( 循环浏览计算机 IP 地址和密码列表。然后运行命令以远程连接到每台计算机。我有另一个 IP 列表,这些 IP 是例外,需要跳过,所以我有一个变量 (set str=( 列出了这些 IP,我正在使用 findstr 搜索它们:

@ECHO OFF
setlocal EnableDelayedExpansion
Pushd "%~dp0"
Set Log=LogFile.log
set str=172.16.66.1 172.16.66.2 172.16.66.3 172.16.66.4
FOR /F "tokens=1,2 delims=," %%i in (IP_List.txt) do call :process %%i %%j
(ECHO+ & ECHO --- END OF REPORT ---) >> %Log%
GOTO :EOF
:process
set IP=%1
set PW=%2
@ECHO %IP% | findstr "%str%" >nul
IF NOT ERRORLEVEL 1 (ECHO+ & ECHO %IP% & ECHO This IP address was skipped) >> %Log% & GOTO :EOF
@ECHO Processing %IP% >> %Log%
---- (remainder of batch file here) ----

findstr存在一个问题,因为例外列表中有 172.16.66.2,findstr 错误地在我的整体列表中找到了与以下 IP 的匹配项:

172.16.66.224、172.16.66.225、172.16.66.226 等。更改代码并将 IP 例外列表放在文本文件 (IP_Exceptions.txt( 中后,我能够克服 findstr 问题,并且这个修改后的批处理文件可以正常工作:

@ECHO OFF
setlocal EnableDelayedExpansion
Pushd "%~dp0"
Set Log=LogFile.log
FOR /F "tokens=1,2 delims=," %%i in (IP_List.txt) do call :process %%i %%j
(ECHO+ & ECHO --- END OF REPORT ---) >> %Log%
GOTO :EOF
:process
set IP=%1
set PW=%2
::~~ Add a right bracket to the end of the IP address variable
SET IPstr=%IP%]%x%
set match=N
::~~ Loop through a list of IP address exceptions
::~~ (A left bracket has been added to the beginning of
::~~ each IP on the list in order to use it as a delimiter)
::~~ Add a right bracket to the end of the variable "%%j"
::~~ in order to look for a match with the findstr command
for /F "delims=[" %%i in (IP_Exceptions.txt) do (
for /F "tokens=1" %%j in ("%%i") do (
ECHO %%j] | findstr "%IPstr%" >nul
IF not errorlevel 1 set match=Y
)
)
IF %match% == Y (ECHO+ & ECHO %IP% & ECHO Skipped this machine) >> %Log% & GOTO :EOF
@ECHO Processing %IP% >> %Log%
:: ---- (remainder of batch file here) ----

不过,让 findstr 部分在没有错误匹配的情况下正常工作的细节(向变量添加括号、嵌套用于带有分隔符的循环等(有点笨拙。如能就提高该科的效率提出任何建议,将不胜感激。

修改后的批处理文件可以工作,但它很笨拙:

@ECHO OFF
setlocal EnableDelayedExpansion
Pushd "%~dp0"
Set Log=LogFile.log
FOR /F "tokens=1,2 delims=," %%i in (IP_List.txt) do call :process %%i %%j
(ECHO+ & ECHO --- END OF REPORT ---) >> %Log%
GOTO :EOF
:process
set IP=%1
set PW=%2
::~~ Add a right bracket to the end of the IP address variable
SET IPstr=%IP%]%x%
set match=N
::~~ Loop through a list of IP address exceptions
::~~ (A left bracket has been added to the beginning of
::~~ each IP on the list in order to use it as a delimiter)
::~~ Add a right bracket to the end of the variable "%%j"
::~~ in order to look for a match with the findstr command
for /F "delims=[" %%i in (IP_Exceptions.txt) do (
for /F "tokens=1" %%j in ("%%i") do (
ECHO %%j] | findstr "%IPstr%" >nul
IF not errorlevel 1 set match=Y
)
)
IF %match% == Y (ECHO+ & ECHO %IP% & ECHO Skipped this machine) >> %Log% & GOTO :EOF
@ECHO Processing %IP% >> %Log%
:: ---- (remainder of batch file here) ----

您的子例程可能如下所示:

...
:process
echo/ %str% |findstr /LC:" %1 " >nul && (
echo %1 skipped
goto :eof
)
echo processing %1 with %2
:: ---- (remainder of batch file here) ----

请注意,我颠倒了"搜索"逻辑(以便更容易正确处理空格(
而不是echo <sub>|findstr <whole>,我切换到echo <whole> |findstr <sub>

注意:echo/ %str% |findstr /LC:" %1 "中的空格是关键的(它们也被比较以避免误报(。

/LC:告诉findstr搜索文字(将点视为点而不是通配符(并包含空格。

最新更新