views:

2321

answers:

3

How can you get the directory of the script that was run and use it within the .cmd file?

+4  A: 

Raymond Chen has a few ideas:

http://blogs.msdn.com/oldnewthing/archive/2005/01/28/362565.aspx

Also the comments in the article are well worth scanning.

Update: For the benefit of ketorin and others who didn't read the comments in the article:

http://blogs.msdn.com/oldnewthing/archive/2005/01/28/362565.aspx#362741

This covers the use of %~dp0.

Kev
This does not answer the question. %CD% gives the current directory, while what was asked was the directory of the script.
ketorin
**@ketorin:** This **DOES** answer the question. %CD% will change if the script itself change directory throughout it's usage (pushd/cd/..) but **%~dp0** will **NOT** change and always point to where the script is located.
Jay
+6  A: 

This is equivalent to the path of the script:

%~dp0

Here's a webpage with more resources on this sort of thing: http://www.ss64.com/ntsyntax/parameters.html

Landon
A: 
for /F "eol= delims=~" %%d in ('CD') do set curdir=%%d

pushd %curdir%

Source

Alex. S.