Idanis
Idanis

Reputation: 1988

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.

Upvotes: 0

Views: 159

Answers (1)

dbenham
dbenham

Reputation: 130889

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

Upvotes: 1

Related Questions