Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When you run a batch script by dragging another file onto it, the location of the file being dragged onto the batch script is used as the path.

How can I reference a file that's in the same folder as the batch script?

share|improve this question
    
Is a batch script normally run as though it is in the folder that the file that is dropped on it is in? –  viking Jun 16 '11 at 21:29
    
That seems to be the way it behaves, yes. –  Acorn Jun 16 '11 at 21:48

1 Answer 1

up vote 1 down vote accepted

Assuming Windows cmd.exe: "%~dp0\otherfile"

Here %0 references the batch file, ~ causes any quotation marks to be removed from the value, dp means "disk letter and path".

See for /? for a full list of expansions. (They unfortunately only apply to numbered command-line arguments and for single-letter variables. To use such expansions with environment variables like %myfile%, you'll have to pass them to a subroutine with call :label.)

share|improve this answer
    
Fantastic, thanks for the explanation! –  Acorn Jun 17 '11 at 13:16

Your Answer

 
discard

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.