Batch file when executing from another folder - batch-file

I am trying to execute a batch file (run.bat) to execute another batch file (clear.bat) located in another folder.
Run.bat looks like this:
call "C:\Documents\BatchFiles\clear.bat"
The clear.bat executes an exe that clears a portion of a .bin file. This batch works when executed independently.
C:\Documents\BatchFiles\clearUtility.exe C:\Documents\BatchFiles\firmware.bin
But when I execute the run.bat batch file the firmware.bin file does not get cleared.
Please help me with this.
I thank you in advance!
I tried adding a few more lines to the clear.bat file to see some output on console.
`C:\Documents\BatchFiles\clearUtility.exe C:\Documents\BatchFiles\firmware.bin
echo DONE!
pause`
I can see the output on the screen when I execute run.bat, but the firmware.bin file still does not get cleared.

Most likely the clearUtility expects the binfile to be in the current working directory.
So change the clear.bat to first set the current directory before executing the clearutility.
#echo off
pushd %~dp0
clearUtility.exe
popd

Related

How to return to the directory from where I ran the batch script file?

My folder structure is like some thing below,
C:\WORK\Project\
Inside the above path there are multiple sub folders,
1. Src
2. bat
3. output
etc.
All my batch script file are present in C:\WORK\Project\bat\
So I will be going to the same directory and run my batch script file.
But as my source code and all present in side C:\WORK\Project\Src path so in my batch script file the path mentioned as C:\WORK\Project\
Problem :-
After finishing my batch command as the path mentioned C:\WORK\Project\ so it is going to the same folder in command prompt. But I want to be in C:\WORK\Project\bat from where I started my command.please let me know hoe to achieve this.
As there are many folders inside C:\WORK\Project\ which I need to take into consideration so I have to set my path in batch script file as C:\WORK\Project.
from the prompt, try
pushd /?
and its friend,
popd /?
Probably the easiest way is to include in the batch file, before any directory change
setlocal
This will make changes to the environment local to the current batch file. When the batch file finishes or when endlocal command is used, any change to the environment, including changes to the current active directory, will be discarded.

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

changing the path in the batch script dynamically on run time

I am working on a batch script file to copy and trigger a .exe file installation
So i have coded it as follows:
set path = "c:\path_to_install_exe\"
set installationfilepath=%cd%
(this one gives, d:\installation_file_path\commands)
the installation file is present in the above path and i want to copy from my current working directory
so i gave the it as
echo xcopy "%installationfilepath%\..\install.exe" "%path_to_install_exe%"
But this does not do the necessary operation. i get d:\installation_file_path\commands..\install.exe as an output.
Can anyone please help me on this, as i am new to batch file scripting.
You could always use pushd and popd to change the directory the batch file assumes.
Like how
pushd C:\Users\Arescet\Desktop\
echo Hello world! > Hi.txt
popd
Will create "Hi.txt" with 'Hello world!' no matter where you launch the bat.
For shorter commands, you're better off setting a different fileName, in the list of default variables 'path' is already a system variable, and it is unwise to change it, as 'MC ND' stated.
For just one single file, you're better off with the copy command as it lets you indicate the path type.

capture cmd output in a batch file

i need to capture the output of a batch file (maintenance.bat) that will run on a schedule to restart services and run some maintenance. i need to see the output of the bat file.
my bat file calls other bat files. here is an example
pushd d:\workingdirectory
call servicecontroller.bat services.stop
call rotatelogs.bat
call pushconfigupdates.bat
popd
i need to capture the output of all the commands ran.
Thanks
try with
myfile.bat > log.txt
For more info you can check here.

Running batch file depending on other files from batch file

I'm writing a batch file which is supposed (among other things) to call another batch file.
The second batch file is depending on files located in the same directory, so, when I try running it from the first batch file, it fails.
Example:
Batch file #1: (Located on C:)
#echo OFF
call C:\Tests\Tests.bat
Output: "Could Not Find C:\Tests\Tests.txt
Reason: Both Tests.bat and Tests.txt are located under: C:\Tests while I'm calling it from C:.
Any ideas?
Thanks a lot,
Idan.
You can use %-dp0 within a batch file to get the path of the currently executing batch file. You can use that path every time you need to access a file from that directory. For example:
call "%~dp0\Tests.bat"
for /f "usebackq ..." ... in ("%~dp0\someFile") do ...
Another option is to temporarily set your current directory to the same path at the top of a batch script using PUSHD. From that point on you don't have to worry about providing the path to the other commands. Just be sure to use POPD before the script exits. That way the script will not impact any script that may have called it.
pushd "%~dp0"
rem Do you work
popd

Resources