检查结果是否包含在命令答案中

时间:2014-07-02 11:22:33

标签: batch-file

我正在尝试仅在命令答案不包含时运行命令:X-UA-Compatible

所以我试过这个:

set APPCMD_EXEC=c:\Windows\System32\inetsrv\appcmd.exe

for /f "delims=" %%a in ('%APPCMD_EXEC% list config "Default Web site" /section:httpProtocol /text:*') do @set VALUE_1=%%a

IF %VALUE_1% EQU [] Echo List Empty
IF %VALUE_1% NEQ [] Echo %VALUE_1%


REM %APPCMD_EXEC% set config "Default Web site" /section:system.webserver/httpProtocol /+"customHeaders.[name='X-UA-Compatible',value='IE=EmulateIE9']"

我得到的答案是

[redirectHeaders]

但如果我只运行命令,我就会得到所有这些:

CONFIG
 CONFIG.SECTION:"system.webServer/h
 path:"MACHINE/WEBROOT/APPHOST/Defa
 overrideMode:"Inherit"
 locked:"false"
[system.webServer/httpProtocol]
allowKeepAlive:"true"
[customHeaders]
  [add]
    name:"X-UA-Compatible"
    value:"IE=EmulateIE9"
[redirectHeaders]

那我怎么能这样做才能找到X-UA-Compatible是否存在于我的命令答案中?

2 个答案:

答案 0 :(得分:2)

试试这样:

@echo off

c:\Windows\System32\inetsrv\appcmd.exe list config "Default Web site" /section:httpProtocol /text:* | find "X-UA-Compatible" && goto:found || echo not found
exit /b

:found
echo Here the command to execute

答案 1 :(得分:0)

如果找不到字词value_1,则只会设置X-UA-Compatible。 for循环中的find过滤器有助于此。

set APPCMD_EXEC=c:\Windows\System32\inetsrv\appcmd.exe

set "value_1="
for /f "delims=" %%a in ('%APPCMD_EXEC% list config "Default Web site" /section:httpProtocol /text:* ^| find /v "X-UA-Compatible" ') do @set "VALUE_1=%%a"

if not defined VALUE_1 Echo List Empty
if defined VALUE_1 Echo %VALUE_1%


REM %APPCMD_EXEC% set config "Default Web site" /section:system.webserver/httpProtocol /+"customHeaders.[name='X-UA-Compatible',value='IE=EmulateIE9']"

因此,只有在找不到该术语时,才能执行此操作以执行示例中的命令:

set APPCMD_EXEC=c:\Windows\System32\inetsrv\appcmd.exe

for /f "delims=" %%a in ('%APPCMD_EXEC% list config "Default Web site" /section:httpProtocol /text:* ^| find /v "X-UA-Compatible" ') do %APPCMD_EXEC% set config "Default Web site" /section:system.webserver/httpProtocol /+"customHeaders.[name='X-UA-Compatible',value='IE=EmulateIE9']"