如果批处理文件中的语句不执行(可能出现语法问题?) [英] If statement in batch file doesn't execute(Possible syntax issue?)

查看:293
本文介绍了如果批处理文件中的语句不执行(可能出现语法问题?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the below script to help automate some processes that would make my work life easier. When running this current version it faults out and closes the program right as soon as the first if statement executes. Did quite a bit of research on my own and the code looks to be correct. The program closed so fast I couldn't read a reason why. So I ran all the output into a txt file. It looks as if the program faults out for a syntax reason. I unfortunately don't have the file with me and don't have the exact error. I can post it tomorrow when it is in front of me.

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

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

The first one is related to how the parser handles the commands. The line

if %POSNAME% == "m"

不会将变量内的值与文字字符串进行比较.发生的情况是,解析器扩展了变量引用(%POSNAME%),将引用替换为命令内部的值,然后尝试执行结果命令,而没有任何变量引用,仅执行该值.因此,对于存储在POSNAME变量中的期望值,执行的命令和结果将为

is not comparing the value inside the variable against a literal string. What is happening is that the parser expands the variable reference (%POSNAME%) replacing the reference with the value inside the command and then tries to execute the resulting command, without any variable reference, only the value. So, for expected values stored in POSNAME variable, the command executed and the result will be

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

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

In the first case the command fails because there is not any value in the left side of the == operator. The variable is empty and nothing can be placed in the command to be executed.

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

The second case seems logical, but sometimes the third case is not so obvious. Why false? Because the value in the right side of the == is a quoted literal while the value in the left side is an unquoted literal, so both values do not match.

您可以简单地引用双方

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

(注意:您也可以取消双方的引用,但是除非您完全确定双方的值是什么,并且所产生的命令不会产生问题,否则不建议这样做)

(note: you can also unquote both sides, but it is not recommended unless you are completely sure what the values on both sides are and that the resulting command will not generate problems)

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

The second problem in your code is parenthesis placement. The batch syntax requires them to be properly placed:

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

  • If present, the opening parenthesis in the if clause must be in the same line that contains the if

如果有else子句,则右圆括号if必须在else行中.

If there is an else clause then the closing if parenthesis must be in the else line.

如果有else开头括号,则必须在else行中

If there is an else opening parenthesis, it must be in the else line

所以

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

不是有效的语法.您可以在此处查看如何放置括号的示例.

is not a valid syntax. You can see here samples of how to place the parenthesis.

这篇关于如果批处理文件中的语句不执行(可能出现语法问题?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆