44

I have a bat file on windows that execute a procdump operation. The issue with the batch file is that I need to cd to the batch file directory first before executing the job, or else the script won't work.

How to change to the current batch file directory?

I tried the following code in my procdump.bat:

cd "%~dp"
procdump -h devenv.exe mydump.txt

But it failed, the error message is:

The following usage of the path operator in batch-parameter substitution is invalid: %~dp"

For valid formats type CALL /? or FOR /?

Edit: The answer provided is working, but there is only one catch: if my current directory is different than the batch file directory, then I would get a "The system cannot find the path specified". Anyone has any ideas?

Graviton
  • 2,865
  • 12
  • 42
  • 64

3 Answers3

72

Ok, I think I found here what you mean with %~dp.

I think what you really want to do is this:

cd /D "%~dp0"

(!) But note that this will still not give you the right behaviour when you're trying to execute your batch while the current directory is on another drive as cd doesn't change the active drive.

Edit: Apparently (thanks @Yoopergeek) you can add the /D parameter to the cd command to let it also change the active drive.

StackzOfZtuff
  • 1,842
  • 13
  • 21
fretje
  • 1,644
  • 1
  • 14
  • 15
  • For more information on these batch parameters, see `help call` at a `cmd` prompt. – Dennis Williamson Dec 18 '09 at 15:00
  • By the way, you can combine those parameters like this: `%~dp0` – Dennis Williamson Dec 18 '09 at 15:02
  • Your answer is helpful; but if my current directory is at a different directory that the batch file, then the above command will fail; even `cd /D` or `cd "%~dp0" /D` can't help. – Graviton Dec 18 '09 at 15:58
  • @Ngu Soon Hui: That your current dir is at a different dir than the batch file was obvious. But why would that command fail? I've tested it here and it works. – fretje Dec 18 '09 at 16:01
  • I don't know, very weird. – Graviton Dec 19 '09 at 07:17
  • 1
    I've found the solution by using `cd /D "%~dp0"`. Thanks. – Graviton Jan 06 '10 at 03:22
  • FYI: Another alternative to `cd /D` is `pushd`... you can then use `popd` to return to the previous directory / you can nest calls such as this if you want to work up or down the stack. – JohnLBevan Apr 26 '20 at 07:07
  • The quotes are unnecessary. CD doesn't treat spaces as delimiters. Straight from CD /?: "CHDIR command does not treat spaces as delimiters, so it is possible to CD into a subdirectory name that contains a space without surrounding the name with quotes." – Jamie Jul 09 '21 at 17:37
11

I'd leave a comment to fretje's answer, but evidently I can't???


Anyway, regarding the note:
But note that this will still not give you the right behaviour when you're trying to execute your batch while the current directory is on another drive as cd doesn't change the active drive.


Use the /D switch in your CD command, and CD will change the active drive.

Yoopergeek
  • 246
  • 1
  • 4
7

You can do pushd "%~dp0" to go to the directory of a batch file -- even if it's on another drive. Additionally, that allows you to popd to go back to where you came from.

idbrii
  • 173
  • 1
  • 5