batch-filewinrarsfxtemporary-directory

Batch script does not run in temporary folder after decompressing SFX WinRAR file


I have a program with a separate setup for 32 and 64 bits. My goal is to create a single executable that can run the appropriate setup. So I created a folder and placed the two setups inside, then wrote the following script:

@echo off

if %PROCESSOR_ARCHITECTURE%==AMD64 goto :x64
if %PROCESSOR_ARCHITEW6432%==AMD64 goto :x64

:x86
    "%cd%"\setup.exe
    exit

:x64
    "%cd%"\setup-x64.exe
    exit

Afterwards, I created the SFX file with this folder in WinRAR, pointing to the BAT file. But when I run it, a command line window pops up and shuts down instantly and nothing happens. I go to the temporary folder and double click the BAT file and the setup starts. The same happens in the original folder. What is happening and how can I fix it? Thanks!


Solution

  • %cd% reffers to the directory of the call of the batch-file.

    For example a batch-file is in %USERPROFILE%\Desktop\Folder\bat.bat:

    echo %cd%
    pause
    

    and you start it for example from the command-line like this:

    C:\>%USERPROFILE%\Desktop\Folder\bat.bat

    it should echo C:\ as that is where you called it from.

    Two ways from the comments to solve the problem:

    Push the directory of the batch-file using pushd "%~dp0" -> will result in a change of the variable value of %cd%

    or

    do not use "%cd%" but "%~dp0"

    Both ways use the fact that the zeroth argument of a batch-file is its path.

    You can prevent the command-line window from closing if you are debugging the file from the command-prompt itself if possible. With that you should have seen an error that state something like ...\setup.exe not found. After that nothing had to be done from the batch-file so it closed.