Calling a batch file from another batch file in different directory - resources not found - windows

I'm working with installshield and have a group of batch files that I want to run as part of the install process. Instead of executing each batch file from installshield I want to create one batch file that executes all of the batch files.
The issue I have is that the calling batch file sits two directories up from the others. When the batch file tries to call the others they fail to run because they can not find the resources that they need. It seems that when they are executed from the batch file two directories up they are for some reason using the relative path of the calling batch file. Is my assumption correct?
One of the batch files that I am calling is a batch file to star an h2 database the call looks like this:
call h2\bin\h2.bat
If I go to the /h2/bin directory in a command prompt the h2.bat runs fine but once I run it from the calling batch file this is the error that I get.
Error: Could not find or load main class org.h2.tools.Console
How do I call one batch file from another without using the calling batch files path?

Explanation
It seems that when they are executed from the batch file two
directories up they are for some reason using the relative path of the
calling batch file. Is my assumption correct?
Yes your assumption is correct. Calling a batch file will not change the current working directory. The main batch file will be found because you are providing the correct relative path, but all the other relative paths will be seen from the perspective of your current working directory, not from the directory that contains the main batch file.
%~dp0 is your friend, it yields the drive letter and path to the batch file containing that character sequence. Use it as a basis for relative paths and your batch files will work no matter who calls them from where.
Example:
Fictitious h2.bat that won't work:
#echo off
h2.exe start
Working h2.bat:
#echo off
"%~dp0\h2.exe" start
See What does %~dp0 mean, and how does it work? for more explanations on %~dp0

Try setting the directory:
cd ht\bin\
call h2.bat
cd %HOMEPATH%
REM just reset to where ever you were before.
If that doesn't work, try using the C:// prefix in your path. That might/might not work.
Good Luck!

Suppose current .bat file is running in C drive and you want to run .bat file placed in D: directory then in first .bat write.
D:
cd "D:/folder/folder2/"
call batFile.bat

It might be because you don't have permission. M facing the same problem and i found the solution like this -
Right click on your task than properties.
In properties click on General tab and then click on 'User Group or User' and select appropriate user.
Or create a another bat file to call your bat file and schedule that file. you can create the bat file like this -
open Notepad and give your original bat file path and then call bat file with name like -
D:
cd "E:/ABC/FirstJob/main/"
call main_run.bat
Now save this file with .bat extension.

if your bat file is correct, try cmd command as below and hit enter(tried in windows 10):
"\h2.bat"
e.g: "C:\Users..\bin\h2.bat"

I tried :
pushd h2\bin\
call h2.bat
=> It 's okay.

Related

How to set a go to a directory and execute a command in a batch file?

During the deployment of the windows application that I make, I have to run a few commands in the CommandPrompt to update the database. I was thinking of making a batch file to automate this process so that the user can update the database just by double-clicking the batch file. Following is the screenshot for the same.
I want my batch file to go to the current directory where the .bat file is located.
Then i want to execute the DBUpdater exe file against the config file.
I am new to batch programming so anything informative will help a lot. Thank you.
Create a batch file named DBUpdater.bat in any directory of your choice with the below script:
#ECHO OFF
setlocal
cd /d %~dp0
DBUpdater.v15.2.exe OfficeManagement.win.exe.config
This assumes that the OfficeManagement.win.exe.config file exists in the same directory as batch file and that DBUpdater.v15.2.exe is recognized as a global command.

Batch file flashing command prompt in endless loop after being executed

Background
I want to use ROBOCOPY to backup folders. To learn this, I created a test source folder, containing other subfolders and dummy files.
F:\RoboCopy\RoboCopy_Files
I am able to ROBOCOPY the source folder from the Command line and PowerShell (with using Windows 10).
ROBOCOPY "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR
It does exactly what I want.
Now I put the command into batch file Robocopy.cmd.
Problem symptoms
However, when I put the same command into Robocopy.cmd file, in the root F:\RoboCopy folder and run it, I get only flashing cmd window with my command repeated on ever increasing number of lines.
How can I put the command into a CMD file (e.g. Robocopy.cmd) for later use/share/schedule? How to prevent command prompt from flashing in endless loop without running the command?
Note: I feel this is more about learning how to put cmd scripts into files, than how to use ROBOCOPY.
Cause: File and Command have the same name
I wanted to use the ROBOCOPY command inside the ROBOCOPY file. This is a mistake.
The file ends up calling itself.
Solution 1: Rename batch file
One solution is to rename the batch file to be different from the command(s) in the file.
Solution 2: Use more explicit command call
Another solution could be to use ROBOCOPY.exe or explicitly specify the full path to the exe like C:...\robocopy.exe. This would prevent the "confusion" in calling the command vs calling the batch file itself.
Solution 1 x 2
The best solution (thx Mofi again) is to combine the 1 x 2 together. Use unique batch file name AND specify the full path to command (exe) inside the batch file.
Useful related commands: To determine the full path to command (exe), see the WHERE command (e.g. Where Robocopy.exe - This should be somewhere in windows folder.), system variables (e.g. SS64), or the command SET.
The full version in my case would be to run for example BackupRobocopyFiles.cmd with line:
%SystemRoot%\System32\robocopy.exe "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR /R:2
Note: This would work only if the cmd file is in the root folder F:\RoboCopy. If I would like to run the cmd from different folder (or task sheduler), I would specify the full path to SOURCE_FOLDER and DESTINATION_FOLDER parameters of ROBOCOPY command.
Contributions
The answer was found based on comments by: "MC ND", "eryksun". "Mofi" did point out, that the original Q was not helpful. Thanks to all.

issues in exe files from bat files

ok...Im a new member here and I can express how jolly I am...back to subject
I made a bat file, lets call it 1.bat and I used iexpress to make it an exe file, lets call it 1.exe.
So, in the batch file I added the command line to add a vbs file ( call it 1.vbs ) which is also included in the exe package (1.bat and 1.vbs are in 1.exe) but it installs 1.bat, so in the command I typed:
copy "1.vbs" "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
If I run the bat file it copies the vbs file to start up, but if I make the 1.exe file in iexpress by including 1.bat and 1.vbs and when I click the 1.exe file, it doesnt work, it doesn't copy the vbs file to startup, it says something about too many parameters.
I'm sorry if my post is too long or my question have been posted before, but I couldn't found any thread solves my issues, if there is, i'd be glad to be enlightened.
Thanks.
Never use "Batch to EXE" converter, they often do not work as expected. Simply use the batch script.
Just a note, this file actually only acts as a wrapper for your script, and the script itself actually gets executed in a temp folder created on execution (and deleted afterwards), so make sure you don't use any relative paths.
source
When running a batch file in IExpress, you need to call it like:
cmd /c 1.bat
If you don’t, variables like %username%, long file names, etc, will likely not work.
I suspect you are using short file names. Put in your bat dir then pause and you'll see it's 1~1.vbs or similar.

Windows PATH variable

My program needs .bat file to run because the .bat file is changing some language settings, so .bat file looks like:
set lang=en
start ap.exe
It is working great but when I made installer for my app and pointed .bat file as main file, it creates a shortcut on the desktop to that .bat file. So far everything is great but when I launch that shortcut it cannot open app.exe because it can't find it on desktop.
So my question is: How can I get path to folder of .bat file so I could set proper start command? Something like:
set lang=en
S=getpath();
start S/app.exe
It is just pseudocode but I think you get point.
You can write %~dp0 to get the directory containing the batch file.
Therefore, you can write
"%~dp0app.exe"

How to move output file in dos

I would like to create a batch file which will move the output file of a custom command "mdmg C:\source i5". I must execute this command from the C:\home directory where the mdmg.cmd resides.
This command converts the any file in the source dir and creates an output files in the C:\home folder.
However I want to move the output files to another folder autometically, lets say C:\test.
can it be done in a batch script?
Thanks in advance.
bla.bat
move c:\home\* c:\test
What is the problem ? DOS has a move command. Or you could simulate that with a copy and delete if move is unavailable for some reason.
You could save yourself the trouble of the batch using CMD redirects. Just paste the following behind the mdmg.cmd command.
> "C:\source i5\output.txt"
Basically the CMD interpreter will execute the command(s) in mdmg.cmd, and then redirect the output of the commands to print in output.txt. This way you don't have to call another batch. Another cool thing about doing it this way is that if the output file does not exist at the specified path, cmd.exe will make it for you.

Resources