Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've created a few batch files that moves files around relative to the folder the batch file is in.

I've added the folder to my Taskbar using Toolbars => New Toolbar..., however now when I run the batch files, the starting position is My Documents (U:), not the actual location of the batch files. Is there any way I can get it to use the actual path?

share|improve this question
up vote 8 down vote accepted

Put the following at the top of your batch file:

CD /D %~dp0


  • The /D option tells CD to change current drive as well as current directory for a drive.
  • %0 is the name of the batch file
  • The ~ says we want to use some modifiers
  • d expands to drive letter.
  • p expands to a path.

So %~dp0 is the directory in which the batch file resides.

share|improve this answer
The CD works perfectly, but it doesn't change the drive designation, so the script still tries to run on U:\ – ck. Feb 5 '10 at 11:38
Have added %~d0 to change drive following your explanation. – ck. Feb 5 '10 at 11:42
@ck - Forgot about drives, sorry. It's probably easiest to use CD /D. Have edited the answer to reflect this. – Dave Webb Feb 5 '10 at 11:55
we can use PUSHD instead of CD /D – tuananh Feb 5 '10 at 13:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.