How do I find the current directory of a batch file, and then use it for the path?

176,559

Solution 1

There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:

.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt

And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:

@Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit

Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:

@Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit

Solution 2

ElektroStudios answer is a bit misleading.

"when you launch a bat file the working dir is the dir where it was launched" This is true if the user clicks on the batch file in the explorer.

However, if the script is called from another script using the CALL command, the current working directory does not change.

Thus, inside your script, it is better to use %~dp0subfolder\file1.txt

Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory. Thus, if you need the directory name without a trailing backslash, you could use something like

call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF

:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF

Solution 3

You can also do

 Pushd "%~dp0"

Which also takes running from a unc path into consideration.

Solution 4

Try in yourbatch

set "batchisin=%~dp0"

which should set the variable to your batch's location.

Share:
176,559
Ryan Barber
Author by

Ryan Barber

Updated on July 05, 2022

Comments

  • Ryan Barber
    Ryan Barber almost 2 years

    I have a batch file that I intend to distribute to our customers to run a software task.

    We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.

    Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.

    So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.

    But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.

  • Ryan Barber
    Ryan Barber about 11 years
    thank you for all your help. But none of these worked. My main batch links to other batches under the same master folder, but inside a sub-folder. When I type the number at the menu in the main batch, it is unable to lauch the BATs in the sub-folders.
  • christutty
    christutty over 8 years
    This is wrong. Launching a bat file does nothing to change the working directory. This is only guaranteed to work if you open a command window, change the current directory to that of the batch script and then start it by typing it's name. An alternative that won't work is entering "CD C:\Temp" and then "C:\Scripts\MyScript.bat". That will launch MyScript,bat but the working directory WON'T be the launch directory. In a similar way launching batch scripts from Windows explorer doesn't automatically change the working directory.
  • Travis Laborde
    Travis Laborde over 6 years
    I also have always been under the impression that the "working directory" is wherever the .bat file is when you run it. I found out today that just isn't true. On a new PC, I found that simply having a "dir" command in a bat file would not show the directory of where the bat file lived, but some other directory. Using the pushd statement here first cleared up that problem. Thanks!
  • Marco Rinaldi
    Marco Rinaldi over 6 years
    This worked for me when the bat is launched as administrator
  • Sorensen
    Sorensen over 6 years
    Fun fact: if you write a batch script to delete a bunch of build DLLs, then run it from explorer, it will run in the current directory as the file. But if you can't delete the build DLLs because "access is denied" and you try to run the batch script from explorer as an administrator, it will run in C:\Windows\System32. And then delete the DLLs.