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


i using below script automate processes make work life easier. when running current version faults out , closes program right first if statement executes. did quite bit of research on own , code looks correct. program closed fast couldn't read reason why. ran output txt file. looks if program faults out syntax reason. unfortunately don't have file me , don't have exact error. can post tomorrow when in front of me.

::turns off unnecessary messages command prompt echo off  ::copies files on nas drive required setup echo transfering files 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 changing file pathway in 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 workgroup , computer name echo setting computer name... set /p pcname=please enter computer name:  wmic computersystem "name='%computername%'" rename "%pcname%"  echo setting workgroup... set /p wgname=please enter workgroup name: wmic computersystem name="%computername%" call joindomainorworkgroup name="%wgname%"  ::selecting pos software install set /p posname=please enter pos software install (a:aldelo m:mapos t:trpos):  if /i %posname% == "m" (    ::transfers required files nas drive install folder    echo transferring install files...    xcopy /y \\nas1\volume_1\"tech department"\"pos software"\mapos\* "%userprofile%\desktop\install_files"      ::installs mapos , groovv sdk 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 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 computer changes take effect ::shutdown.exe /r /t 00 

there 2 problems ifs

the first 1 related how parser handles commands. line

if %posname% == "m" 

is not comparing value inside variable against literal string. happening parser expands variable reference (%posname%) replacing reference value inside command , tries execute resulting command, without variable reference, value. so, expected values stored in posname variable, command executed , result

if %posname% == "m"  value                  parsed            result -------------------------------------------------------------- postname empty ->   if == "m"            syntax error  postname     ->   if == "a"          false postname m     ->   if m == "m"          false 

in first case command fails because there not value in left side of == operator. variable empty , nothing can placed in command executed.

the second case seems logical, third case not obvious. why false? because value in right side of == quoted literal while value in left side unquoted literal, both values not match.

you can solve problem quoting both sides

if "%posname%"=="m"   value                  parsed            result -------------------------------------------------------------- postname empty ->   if "" == "m"           false postname     ->   if "a" == "a"          false postname m     ->   if "m" == "m"          true 

(note: can unquote both sides, not recommended unless sure values on both sides , resulting command not generate problems)

the second problem in code parenthesis placement. batch syntax requires them placed:

  • if present, opening parenthesis in if clause must in same line contains if

  • if there else clause closing if parenthesis must in else line.

  • if there else opening parenthesis, must in else line

so, this

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

is not valid syntax. can see here samples of how place parenthesis.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -