2012-09-14 114 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