如果批处理文件中的语句不执行(可能是语法问题?)

If statement in batch file doesn't execute(Possible syntax issue?)

我正在使用下面的脚本来帮助自动化一些流程,这些流程可以让我的工作生活更轻松。当 运行 这个当前版本出现故障并在第一个 if 语句执行时立即关闭程序。我自己做了很多研究,代码看起来是正确的。该程序关闭得如此之快,以至于我无法理解原因。所以我把运行全部输出成一个txt文件。看起来程序由于语法原因而出错。不幸的是,我没有这个文件,也没有确切的错误。我可以 post 明天当它在我面前时。

::Turns off unnecessary messages from Command Prompt
echo off

::Copies files over from the NAS drive that are required for setup
echo Transfering files from NAS1...
if not exist "%userprofile%\Desktop\Install_Files" mkdir %userprofile%\Desktop\Install_Files
xcopy /Y \nas1\Volume_1\"Tech Department"\"General Windows  POS Preperation"\* "%userprofile%\Desktop\Install_Files"
echo File Transfer Complete

::Start installation of Foxit Reader
echo Installing Foxit Reader...
start /w %userprofile%\Desktop\Install_Files\"FoxitReader831_current version".exe
echo Installations Complete

::Changes background by changing the file pathway in the registry value
echo Setting Background...
REG ADD "HKCU\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %userprofile%\Desktop\Install_Files\NewTMS1024x768.jpg /f

::Changes the Workgroup and Computer Name
echo Setting Computer Name...
SET /P PCNAME=Please enter computer name: 
wmic computersystem where "Name='%computername%'" rename "%PCNAME%"

echo Setting Workgroup...
SET /P WGNAME=Please enter workgroup name:
Wmic computersystem where name="%computername%" call joindomainorworkgroup name="%WGNAME%"

::Selecting which POS Software to install
SET /P POSNAME=Please enter POS Software to install (a:Aldelo m:MAPOS t:TRPOS):

if /i %POSNAME% == "m"
(
   ::Transfers required files from NAS drive to Install Folder
   echo Transferring install files...
   xcopy /Y \nas1\Volume_1\"Tech Department"\"POS Software"\MAPOS\* "%userprofile%\Desktop\Install_Files"

    ::Installs MAPOS and Groovv SDK for card processing
    echo Installing GroovvSDK...
    start /w %userprofile%\Desktop\Install_Files\GroovvSDK_Client_Setup_v3.9.6

    echo Installing MAPOS...
    start /w %userprofile%\Desktop\Install_Files\mapos_install
)

if /i %POSNAME% == "t"
(
    ::Transfers required install file for TRPOS
    echo Transferring install files...
    xcopy /Y \nas1\Volume_1\"Tech Department"\"POS Software"\TRPOS\TRPOS_install.exe "%userprofile%\Desktop\Install_Files"

    ::Installs TRPOS
    start /w %userprofile%\Desktop\Install_Files\TRPOS_install.exe
)

if  /i %POSNAME% == "a"
(
)
else
(
    echo No POS Software selected or improper input
)

::Force restarts the computer so changes will take effect
::shutdown.exe /r /t 00

首先...IF 语句有问题。您需要引用 == 的两边并删除空格。更改此格式

if /i %POSNAME% == "m"

至此

if /i "%POSNAME%"=="m"

试试看,post 结果。

您的 ifs

有两个问题

第一个与解析器如何处理命令有关。行

if %POSNAME% == "m"

没有将变量内的值与文字字符串进行比较。发生的事情是解析器扩展变量引用 (%POSNAME%),用命令中的值替换引用,然后尝试执行生成的命令,没有任何变量引用,只有值。因此,对于存储在 POSNAME 变量中的预期值,执行的命令和结果将为

if %POSNAME% == "m"

value                  parsed as            result
--------------------------------------------------------------
POSTNAME is empty ->   if == "m"            syntax error 
POSTNAME is a     ->   if a == "a"          false
POSTNAME is m     ->   if m == "m"          false

在第一种情况下,命令失败,因为 == 运算符的左侧没有任何值。变量为空,要执行的命令中不能放任何东西。

第二种情况看似合乎逻辑,但有时第三种情况就没那么明显了。为什么是假的?因为 == 右侧的值是带引号的文字,而左侧的值是不带引号的文字,所以两个值不匹配。

你可以简单地引用双方来解决这个问题

if "%POSNAME%"=="m" 

value                  parsed as            result
--------------------------------------------------------------
POSTNAME is empty ->   if "" == "m"           false
POSTNAME is a     ->   if "a" == "a"          false
POSTNAME is m     ->   if "m" == "m"          true

(注意:你也可以取消两边的引号,但不建议这样做,除非你完全确定两边的值是什么,并且结果命令不会产生问题)

您代码中的第二个问题是圆括号的放置。批处理语法要求正确放置它们:

  • 如果存在,if 子句中的左括号必须位于包含 if

  • 的同一行
  • 如果有 else 子句,则右括号 if 必须在 else 行中。

  • 如果有else左括号,它必须在else

所以,这个

if "%POSNAME%"=="m" 
(
    .....
)

不是有效语法。您可以查看 here 个如何放置括号的示例。