为什么我的批处理脚本在if语句匹配时同时运行if和else语句?

时间:2013-05-14 22:43:47

标签: windows if-statement batch-file

即使if匹配(并且也被处理),批处理脚本也会处理else语句。这是为什么?

set getprocesslistlocal=wmic process get name,processid
echo Type the name of the remote machine to view processes of (or type local for local machine), and press Enter.
set /P remotemachine=
if %remotemachine%==local (
%getprocesslistlocal%
) else (
echo Type the user name to access %remotemachine% with, then press Enter.
set /P remoteuser=
echo Type the password for %remoteuser% on %remotemachine%, then press Enter. (Will be displayed in plaintext)
set /P remotepassword=
set getprocesslistremote=wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process get name,processid
%getprocesslistremote%
)
echo End of list.
pause
echo Type the process id and hit Enter.
set /P killid=
if %remotemachine%==local (
wmic process where processid="%killid%" call terminate
) else (
wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process where processid="%killid%" call terminate
)
echo Process id %killid% terminated. Press Enter to exit.
pause

2 个答案:

答案 0 :(得分:2)

这可能与您试图回应:(将以纯文本显示)

您可以使用^(和^)

转义字符

答案 1 :(得分:0)

我会像这样重写它(留下脚本的后半部分让你完成):

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set /P machine=Type the name of the remote machine to view processes of [local]: 
if "%machine%"=="" (
  set machine=local
  set "getprocesslist=wmic.exe"
) else (
  set "getprocesslist=wmic.exe /node %machine%"
)


echo %getprocesslist%

set /P user=Type the user name to access %machine% with: 
if not "%user%"=="" SET "getprocesslist=%getprocesslist% /user:%user%
set /P password=Type the password for %remoteuser% on %machine% [displayed in plaintext]: 
if not "%password%"=="" SET "getprocesslist=%getprocesslist% /password:%pass%"

SET "getprocesslist=%getprocesslist% process get name,processid"

ECHO cmd.exe /c %getprocesslist%
cmd.exe /c %getprocesslist%

pause