Starting batch file command prompt from the directory location in which the batch file resides in

batch-file, windows

Solution

Start your batch file with:

pushd %~dp0

That will set the current directory to be the folder containing the batch file. Then in the batch file, make sure all your paths are relative to the current directory.

However, if your batch file changes to other directories in the course of its execution and you would still like to be able to refer to the batch's home folder's contents without knowing the exact path, use the same `%~dp0` as the path to the file(s) you want to use. For instance, a `FileA` from the same folder as the batch file would be addressed as

"%~dp0FileA"

Note the absence of a `\` before the `FileA`. This is because the `%~dp0` already includes a trailing `\` and so the entire thing will evaluate to a correct path all right. (Although if you do put another backslash, like `"%~dp0\FileA"`, it should work as well, because Windows usually disregards multiple consecutive backslashes when in the middle of a path.)

Problem

I am writing a batch file that uses some of the files within it's parent directory (lets say Folder1). ``` C:\User\Steve\Foder1\ ``` Now I want to make the whole Folder_1 relocatable so that I can copy paste the folder anywhere on my/someone else's computer and run batch script. ``` D:\User\Random_guy\Folder1\ ``` The question is how do I start batch file's command prompt to (D:\User\Random_guy\Folder1) it's parent directory without writing another batch script to do that.

Original source