问题
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
回答1:
There are two problems with your if
s
The first one is related to how the parser handles the commands. The line
if %POSNAME% == "m"
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.
You can solve this problem simply quoting both sides
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 present, the opening parenthesis in the
if
clause must be in the same line that contains theif
If there is an
else
clause then the closingif
parenthesis must be in theelse
line.If there is an
else
opening parenthesis, it must be in theelse
line
So, this
if "%POSNAME%"=="m"
(
.....
)
is not a valid syntax. You can see here samples of how to place the parenthesis.
回答2:
For starters... there is a problem with the IF statements. You need to quote both sides of the == and remove spaces. Change this format
if /i %POSNAME% == "m"
to this
if /i "%POSNAME%"=="m"
Try that and post results.
来源:https://stackoverflow.com/questions/45684429/if-statement-in-batch-file-doesnt-executepossible-syntax-issue