2012-09-14 67 views
0

运行此批处理脚本时,结果始终相同。我相信我在语法中缺少一些东西。我错过了什么?Netstat批处理程序

@echo off 
netstat -an | find /C "LIST" > tmpFile 
set test=<tmpFile 
del tmpFile 
set max=6 
IF !%test! GTR !max! echo Greater than X 
IF !%test! LEQ !max! echo Less than X 
PAUSE 
:EOF 

回答

1
@echo off 
setlocal EnableDelayedExpansion 

netstat -an | find /C "LIST" > tmpFile 
set /P test=<tmpFile 
del tmpFile 
set max=20 
IF !test! GTR !max! echo Greater than X 
IF !test! LEQ !max! echo Less than X 
PAUSE 
:EOF 

您需要:

  1. 在可变test
  2. 删除%set命令添加关键/P可变test
  3. 添加setlocal EnableDelayedExpansion。又见Batch File: Fails to Echo Variable Inside Loop