这个.bat文件的if参数语法有什么问题?

时间:2013-09-13 15:22:42

标签: batch-file

这就是我所拥有的:

@ECHO OFF

IF [%1]==[](
  SET /P server=What url would you like to proxy to? :
  SET /P location=What is the path to your local files? :
)

IF [%1]==[ggp](
  SET server=10.10.10.10
  SET location=c:/Users/Brook/Desktop/hello
)


start chrome.exe localhost:8080/login

call:startServer

:startServer

  node httpdp.js --server %server% --srcpath %location%

  %ifErr% (
    call:startServer
  )

GOTO:EOF

当if条件被取出时,文件运行正常,所以我的语法有问题,但我不知道。

我真的想把第一个if条件放在else或默认块中,检查参数是否匹配各种字符串,如果有人知道怎么做......

谢谢!

2 个答案:

答案 0 :(得分:1)

这改进了一些语法,使其更加健壮,并停止调用两次子程序。未知命令也可以解决。

@ECHO OFF

IF "%~1"=="" (
  SET /P server=What url would you like to proxy to? :
  SET /P location=What is the path to your local files? :
)

IF /i "%~1"=="ggp" (
  SET server=10.10.10.10
  SET location=c:\Users\Brook\Desktop\hello
)


start "" chrome.exe localhost:8080/login

call:startServer
goto :EOF

:startServer

  node httpdp.js --server %server% --srcpath %location%

REM unknown command  %ifErr% (  call:startServer )

GOTO:EOF

答案 1 :(得分:0)

这样可行,但可以改进。

@ECHO OFF

IF [%1]==[ggp] GOTO ggp

GOTO default

:ggp

SET server=10.10.10.10
SET location=c:/Users/Brook/Desktop/hello
GOTO always

:default

SET /P server=What url would you like to proxy to? :
SET /P location=What is the path to your local files? :
GOTO always

:always

start chrome.exe localhost:8080/login

call:startServer

:startServer

  node httpdp.js --server %server% --srcpath %location%

  %ifErr% (
    call:startServer
  )

GOTO:EOF