FOR /F "tokens=1 delims=" %%A in ('netstat -ano | findstr 7001') do SET myVar=%%A
echo %myVar%
|
指挥部说以前不认识findstr
,谁能帮帮我。
FOR /F "tokens=1 delims=" %%A in ('netstat -ano | findstr 7001') do SET myVar=%%A
echo %myVar%
|
指挥部说以前不认识findstr
,谁能帮帮我。
在 IN() 子句中时,必须对特殊字符(例如|
)进行转义或引用。请注意,不需要 TOKENS=1 选项 - 这是默认选项。
你可以使用
for /f "delims=" %%A in ('netstat -ano ^| findstr 7001') do set myVar=%%A
或者
for /f "delims=" %%A in ('"netstat -ano | findstr 7001"') do set myVar=%%A
@echo off
set portnumber=?
FOR /F "tokens=1,2,3,4,* delims= " %%a in ('netstat -ano') do call :handleline %%a %%b %%c %%d %%e
goto :eof
:handleLine
call :portcheck %1 %2 %2 %4 %5
rem you can have your check here
rem if %portnumber%==7001 do whatever
echo pid %4 uses port %portnumber%
goto :eof
:portcheck
set prot=%1
set ipandport=%2
set pid=%4
set pn1=%ipandport:~-1%
set pn2=%ipandport:~-2%
set pn3=%ipandport:~-3%
set pn4=%ipandport:~-4%
set pn5=%ipandport:~-5%
if !%pn2:~0,1%!==!:! set portnumber=%pn1% & goto :bailout
if !%pn3:~0,1%!==!:! set portnumber=%pn2% & goto :bailout
if !%pn4:~0,1%!==!:! set portnumber=%pn3% & goto :bailout
if !%pn5:~0,1%!==!:! set portnumber=%pn4% & goto :bailout
set portnumber=%pn5%
:bailout
goto :eof