0

I have the following batch file

REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Dependencies\{2e085fd2-a3e4-4b39-8e10-6b8d35f55244} > nul
if %ERRORLEVEL% EQU 1 (

     start /wait "" "%~dp0vc_redist.x86.exe"
)

if exist "%~dp0files\data\GAME_EXE.exe" (

    start "" "%~dp0files\data\GAME_EXE.exe"

) else (

    SET scriptDir=%tmp%\tempGameDialog.vbs

    @echo Call msgbox("It looks like your virus scanner deleted the game exe file. Please deactivate it, restore the game exe file, then try again.", vbCritical, "Error with game"^) > %scriptDir%

    @cscript /nologo %scriptDir%
    @del %scriptDir%
)

When I run this file through command prompt, it works without problem and I see the dialog window in the else statement as I would expect. Whenever I run the batch file through windows explorer, the command prompt closes instantly without seeing any prompt.

If I add the following line to the first line of the batch file, then run through windows explorer, then all works as you would expect

@SET scriptDir=%tmp%\tempGameDialog.vbs

I've tried using the start command to run cscript etc, but it doesn't work without the above line at the start of the batch file.

Whats the cause of this weird behavior? Why do I need to do a variable assignment on the first line for any of my script to run? Or am I missing something completley obvious.

Thanks

user2229618
  • 105
  • 1
  • 11
  • 3
    Specifically, because %scriptDir% is initialized inside of parentheses, it doesn't exist when the script is first run because of how batch expands variables. – SomethingDark Aug 09 '16 at 10:47
  • Thanks. I didn't know that the variable wasn't being set, which was the cause of it seemingly not running, so I didn't know how to target my search as I researched the problem. The answer you have marked as duplicate has furthered my understanding of what went wrong. – user2229618 Aug 09 '16 at 11:10
  • Yeah, batch variables can be pretty counterintuitive when you start working with them, but you get the hang of it. – SomethingDark Aug 09 '16 at 11:11
  • 2
    oh, the variable *is* being set. The problem occures with it's *usage* (expansion) in the same code block. Maybe [this](http://stackoverflow.com/a/30284028/2152082) makes it more obvious. – Stephan Aug 09 '16 at 13:12

0 Answers0