0

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
TGutmann87
  • 19
  • 6
  • Please provide the output of the script in order to find out where the error comes from. – Daniel C. Aug 15 '17 at 00:16
  • "_The program closed so fast I couldn't read a reason why_" Then open a command-prompt window and run it from there. This also allows you to add debugging `echo` statements and temporarily remove/comment-out the `echo off` command. In general, you have inconsistent double-quoting. Specifically, the `if not exist` at the top of the file will create the wrong thing if your `%userprofile%` contains spaces. – TripeHound Aug 15 '17 at 13:20

2 Answers2

1

There are two problems with your ifs

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 the if

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

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

So, this

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

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

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • When i got into work this morning I went ahead and made those changes and it seemed to have worked. While I was aware of the double quotes(to an extent). I was not aware of the second part of the solution you gave. It seems like many code samples online I was looking at online were incorrect. Thanks for the help! – TGutmann87 Aug 15 '17 at 14:09
0

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.

RGuggisberg
  • 4,630
  • 2
  • 18
  • 27
  • Originally that is how I had the if statements formatted. I had gone through the steps of putting that global variable in double quotes, but the results were no different. Based on the documentation I found through windows everything looks correct. Global variables would only need quotes if they contain spaces or other such characters – TGutmann87 Aug 15 '17 at 13:56