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?

link|improve this question
feedback

1 Answer

up vote 7 down vote accepted

Put the following at the top of your batch file:

CD /D %~dp0

Explanation:

  • 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.

link|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 – hvtuananh Feb 5 '10 at 13:33
feedback

Your Answer

 
or
required, but never shown

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