1

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!

Alegom
  • 105
  • 1
  • 2
  • 10
  • 3
    Try adding `setlocal` then `pushd "%~dp0"` just below `@echo off` and see whether that makes a difference. – rojo Jan 03 '17 at 14:50
  • 3
    `"%~dp0setup.exe"` instead of `"%cd%"\setup.exe` and `"%~dp0setup-x64.exe"` instead of `"%cd%"\setup-x64.exe`? – JosefZ Jan 03 '17 at 14:51
  • @rojo and @JosefZ, both your solutions work! I discovered that the `%~dp0` means directing to the script location. I prefer to use the `pushd "%~dp0"` option, seems more elegant. However, do I really need `setlocal`? I didn't understand what it might be used for, from what I searched. – Alegom Jan 03 '17 at 15:22
  • 2
    The `%~dp0` trick should fix the problem, but for debugging similar issues in the future, remove or comment-out the `@echo off` and put `pause` before all exit points: the command window should stay open you would be able to see the commands that were run (in this case showing the `%cd%` wasn't what you thought it was). – TripeHound Jan 03 '17 at 15:23
  • 2
    @Alegom You probably don't _need_ `selocal` in this case, since the thing that called the batch file _probably_ won't care that it changed the current directory. However, it's probably "good practice" in general so that calling a batch file doesn't leave you in a different directory (unless the purpose of the batch file is to change the directory). – TripeHound Jan 03 '17 at 15:26

1 Answers1

2

%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.

rojo
  • 24,000
  • 5
  • 55
  • 101
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54