Digbyswift
Digbyswift

Reputation: 10400

How can I get the path of a batch file as a variable?

How can I get the path of a batch file as a variable?

So for example, I found I can use %cd% to get the directory of the batch fie that is currently executing, but if I call a second batch file from the first, %cd% in the second file will point to the directory of the first file.

Thanks.

Upvotes: 0

Views: 120

Answers (1)

Joey
Joey

Reputation: 354366

You're being mistaken here. As the documentation (help set) clearly states,

%CD% - expands to the current directory string.

So it's not the directory of the batch file, it's the current working directory, same as $pwd in PowerShell. It might be the directory of the currently-executing batch file, but only if your current directory is the same.

What you want is %~dp0 instead, which evalutes to the drive and path of the invocation, i.e. your batch file.

Upvotes: 1

Related Questions