Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 848
jonsinfinity Avatar asked May 23 '13 15:05

jonsinfinity


People also ask

How do I run a batch file from any location?

bat from anywhere on the command line. Show activity on this post. Create a folder called Batches (lets say in your C drive). Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.

What is %% f in batch file?

To use for in a batch file, use the following syntax: for {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>] To display the contents of all the files in the current directory that have the extension .doc or .txt by using the replaceable variable %f, type: for %f in (*.doc *.txt) do type %f.


2 Answers

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

like image 79
Brandlingo Avatar answered Oct 09 '22 11:10

Brandlingo


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!

like image 25
hornzach Avatar answered Oct 09 '22 10:10

hornzach