Batch script does not run in temporary folder after decompressing SFX WinRAR file - batch-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!

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

Related

How to run .EXE from .BAT inside the folder where .EXE is locating

To recreate my problem you need to understand that I have next files in 2 folders.
K:\Script.bat
K:\Project\PortChanger.exe
K:\Project\settings.xml
I want to launch PortChanger.exe using Script.bat that contains next line:
start "K:\Project\PortChanger.exe"
This script is actually executing Program.exe, but my program throws me exception since PortChanger.exe can't find Settings.xml.
How can I launch PortChanger.exe from "K:\Project\", not from "K:\"? Now it seems like .BAT taking .EXE code and just running it where .BAT is locating.
To make it even more clear:
You could use Start with its /D option:
Start "" /D "K:\Project" "K:\Project\PortChanger.exe"
Open a Command Prompt window and enter start /? to read its usage information.
I would suggest you rather use pushd and popd
#echo off
pushd "K:\Project"
start "" PortChanger.exe
popd
pushd will change to the directory, launch the executable from it, then popd will return to the previous directory stored.

How to get the current path of my batch file? [duplicate]

I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.

Batch file Start program

M'kay, so I've written a few batch files before, so I'm not completely new to them, but this is stumping me. What I'm trying to do is run a .exe file from a batch file. Here's the Batch script:
#echo off
:start
setlocal EnableDelayedExpansion
cd "C:\Users\Zac\Dropbox\SoundCloud"
set n=0
for %%f in (*.html*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
move "!file[%rand%]!" C:\Users\Zac\Temp
start "~dp0Link_Open.exe"
echo %time%
timeout 70 > NUL
echo %time%
goto start
So from my understand, this moves a random .html file from one directory to another, this works, I've used it a lot, the only issue is the "Start" command, I don't use this very often. the "Link_Open.exe" is in the same folder as my .bat, but I've tried running it with the full directory written in, I've tried quotations, no quotations, brackets, no brackets, START, start, Start, Call, CALL, call, and none of them work, I'm always getting the same error "Link_Open.exe cannot be found, have you written it correctly"
The only reason I can possibly think of that would be why it wouldn't work, is that the .exe was written in AutoIT and then compiled...but that shouldn't effect it should it?
Running the batch file will result in a random file being moved, and then an error coming up, and then repeating.
What am I doing wrong?
Ps: Running the Link_Open.exe does what it's supposed to do, so there's no errors there, the only issue I'm having is opening it with .bat.
I'm still very new to Autoit, but if someone could show me a script to move a random .html file with Autoit, I could just combine the two scripts together couldn't I?
Provided that your bat file and exe file are in the same directory as you said, try changing:
START Link_Open.exe
to:
start "%~dp0Link_Open.exe"
%~dp0 expands to the current batch script's path
start equivalent to START, it doesn't matter
enclosed in quotes in case your path has spaces like C:\My Documents, thus keeps the whole path as one argument, instead of being misinterpreted as being multiple arguments separated by space
Error Explanation
Your error: Link_Open.exe cannot be found... is precisely that, batch can't find Link_Open.exe. Normally it would, because when you just give a program without full absolute path, it checks the current working directory. This defaults to where the batch script is. But notice you actually changed the current working directory with this line:
cd "C:\Users\Zac\Dropbox\SoundCloud"
Thus, unless you happen to have Link_Open.exe in that SoundCloud directory, or unless you had any code to cd back to the batch script's directory containing the exe, or unless you happen to have Link_Open.exe in %PATH%, then there is no reason batch would automatically know to check the original directory that the script was run from.
Thus we try to refer to Link_Open.exe in a way that batch will know how to find it. One way is if we did absolute path C:\path\to\Link_Open.exe however to keep things portable, I recommend %~dp0 which expands to whatever path of the current batch script, and thus batch is able to find Link_open.exe again.
You change directory to C:\Users\Zac\Dropbox\SoundCloud and then try to start Link_Open.exe which is left in script directory.
So next step you use ~dp0 variable to get folder from script executed, but there is missed leading %: you must write %~dp0
Even though you'll fix it, maybe Link_Open.exe also depend on current working directory? start allows to set up CWD: start "window header" /D "%~dp0" "%~dp0Link_Open.exe"
To debug: add echo before start and add pause next line. start command will be printed with all variables expanded. You'll see if the something is wrong - e. g. you may copy and test via Start -> Run.

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

iexpress extract files then run script referring extracted file

I was trying to deploy my project with IExpress. I have the following scenario. I want to extract some files to a location preferably c:\program files\. Then after it copies all the files i want it to run a .cmd file (which is a script). the script is also added in the project itself and it would refer to a file which is copied by IExpress. Now how can access the path on which the file was extracted. So that i can access it in my script.
If the script is in the project itself, thus being extracted in the same directory when you send your files to, it should start in the same directory.
Test it easy, make a cmd like this:
cmdsetup.cmd:
#echo Source path: %~dp0 >> %temp%\%~n0.log
Put this in your package and when it's done, go check the %temp% directory, locate the cmdsetup.log file and look in it. This should be the path where your files are.
If so, go from there. If I got this wrong, come back and comment, also amend your question to make it clearer.
I hope this helps.
P.s.: Voted for the question as I don't see why the negative vote was given.
The answer is use this format:
start /wait .\hello.cmd
I did this with the following two files. One key thing is that the file I was calling had to be in 8.3 format. In otherwords it failed to find hello.cmd the first time because i called it hello.world.cmd.
First file (start.cmd)
#echo off
cls
echo this is start.cmd
pause
dir
pause
echo going to hello world
start /wait .\hello.cmd
echo back in start.cmd
pause
Second file (hello.cmd)
#echo off
echo HELLO WORLD!
pause
exit
Directions
Use IEXPRESS to create a package that contains both files above. Have it launch START.CMD.

Resources