命令不在.BAT if语句中执行

时间:2016-03-07 10:04:03

标签: windows batch-file

我有一个批处理文件,它将检查CPU架构(32或64位),并相应地执行命令。它将找到特定的文件夹并执行某些.exe文件。我的问题是,如果我在if语句中有一个语句,例如。 'echo some text',它会显示正常,这意味着对架构的检查很好。但是只要我的If语句中有多个命令,批处理文件就会立即退出。到目前为止我的代码:

@echo off

SET "ARCH=x64" 
IF NOT EXIST "%SystemRoot%\SysWOW64\cmd.exe" ( 
IF NOT DEFINED PROCESSOR_ARCHITEW6432 SET "ARCH=x86" 
)
IF "%ARCH%"=="x64" (
    cd \
    cd Program Files (x86)\MySQL\MySQL Installer for Windows
    start /wait MySQLInstallerConsole community install server;5.6.26;x64:*:type=config;openfirewall=true;generallog=true;binlog=true;serverid=3306;enable_tcpip=true;port=3306;rootpasswd=somepass -silent

    cd \
    cd Program Files (x86)\Location
    start /wait DatabaseConfig
  ) ELSE (
    cd \
    cd Program Files\MySQL\MySQL Installer for Windows
    start /wait MySQLInstallerConsole community install server;5.6.26;x86:*:type=config;openfirewall=true;generallog=true;binlog=true;serverid=3306;enable_tcpip=true;port=3306;rootpasswd=somepass -silent

    cd \
    cd Program Files\Location
    start /wait DatabaseConfig
)

pause

如果我在下面的if语句之外运行命令,那么它可以正常工作。

cd \
cd Program Files (x86)\MySQL\MySQL Installer for Windows
start /wait MySQLInstallerConsole community install server;5.6.26;x64:*:type=config;openfirewall=true;generallog=true;binlog=true;serverid=3306;enable_tcpip=true;port=3306;rootpasswd=somepass -silent

cd \
cd Program Files (x86)\Location
start /wait DatabaseConfig

1 个答案:

答案 0 :(得分:2)

这是因为Program Files (x86) path中的括号。关闭括号被视为IF命令的一部分。试试这个(路径是双引号):

@echo off

SET "ARCH=x64" 
IF NOT EXIST "%SystemRoot%\SysWOW64\cmd.exe" ( 
IF NOT DEFINED PROCESSOR_ARCHITEW6432 SET "ARCH=x86" 
)
IF "%ARCH%"=="x64" (
    cd \

    cd "Program Files (x86)\MySQL\MySQL Installer for Windows"
    start /wait MySQLInstallerConsole community install server;5.6.26;x64:*:type=config;openfirewall=true;generallog=true;binlog=true;serverid=3306;enable_tcpip=true;port=3306;rootpasswd=somepass -silent

    cd \
    cd "Program Files (x86)\Location"
    start /wait DatabaseConfig
  ) ELSE (
    cd \
    cd "Program Files\MySQL\MySQL Installer for Windows"
    start /wait MySQLInstallerConsole community install server;5.6.26;x86:*:type=config;openfirewall=true;generallog=true;binlog=true;serverid=3306;enable_tcpip=true;port=3306;rootpasswd=somepass -silent

    cd \
    cd "Program Files\Location"
    start /wait DatabaseConfig
)

pause