70

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.

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
Ryan Barber
  • 715
  • 1
  • 6
  • 7
  • It belongs to you to describe your issue more properly. I can't take your point. – Endoro Apr 27 '13 at 18:49
  • 1
    Nevermind on my last question(may not be visible because I deleted it. I figured it out. I had to add an "/s" to the line – Ryan Barber Apr 27 '13 at 20:53

4 Answers4

86

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
ElektroStudios
  • 17,150
  • 31
  • 162
  • 376
  • 30
    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. – christutty Dec 09 '15 at 23:39
  • 2
    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. – Sorensen Jan 24 '18 at 15:31
62

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
Henning
  • 766
  • 6
  • 7
  • 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! – Travis Laborde Aug 20 '17 at 13:20
46

You can also do

 Pushd "%~dp0"

Which also takes running from a unc path into consideration.

Matt Williamson
  • 6,687
  • 1
  • 20
  • 33
8

Try in yourbatch

set "batchisin=%~dp0"

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

Magoo
  • 68,705
  • 7
  • 55
  • 76
  • 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. – Ryan Barber Apr 27 '13 at 18:21