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

时间:2017-08-14 23:22:55

标签: windows batch-file batch-processing

我正在使用以下脚本来帮助自动化一些使我的工作生活更轻松的过程。当运行当前版本时,它会在第一个if语句执行时立即关闭并关闭程序。我自己做了很多研究,代码看起来是正确的。程序关闭得如此之快,我无法理解原因。所以我将所有输出都运行到了一个txt文件中。看起来程序因语法原因而出错。很遗憾,我没有提供该文件,也没有确切的错误。我明天可以在它面前张贴它。

::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

2 个答案:

答案 0 :(得分:1)

if

有两个问题

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

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样本,了解如何放置括号。

答案 1 :(得分:0)

对于初学者来说...... IF语句存在问题。您需要引用==的两边并删除空格。更改此格式

Coq < Require Import Arith.
[Loading ML file z_syntax_plugin.cmxs ... done]
[Loading ML file quote_plugin.cmxs ... done]
[Loading ML file newring_plugin.cmxs ... done]
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked

Coq < Check 5/10.
5 / 10
     : nat

到这个

if /i %POSNAME% == "m"

试试并发布结果。