22

I'm writing a batch file, I need to get the parent folder of this bat file. Is it possibile? NB I mean the parent folder of the batch file not of the current directory of the prompt calling that batch.

Thanks

Tobia
  • 9,165
  • 28
  • 114
  • 219

4 Answers4

47

The parent folder of a Batch is in the Variable %~dp0 located. Example:

@echo off&setlocal
for %%i in ("%~dp0.") do set "folder=%%~fi"
echo %folder%
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • 2
    Doesn't work, maybe I forget to say that it is a windows's batch – Tobia May 18 '13 at 12:01
  • 2
    Yes, I know. My answer is pure batch. Put it with notepad in a text file, name it eg. `mybatch.bat` and start it from the command line. If you start it from the explorer, put `pause` at the end. – Endoro May 18 '13 at 12:06
  • Doesnt work test.bat, executed from command line. It's in folder ```c:\test123\test.bat``` with the code copied exactly as above. when I run it, i get output ```c:\``` – Northstrider Jun 15 '15 at 17:00
  • 4
    Yes, that's OK. The folder of `c:\test123\test.bat` is `test123` and the parent folder is in your case "c:\" – Endoro Jun 16 '15 at 00:33
  • 4
    You need to remove the two dots in ("~dp0..") because they will get one more directory upwards. And the question is how do I find the batch's parent folder and not the partent's folder parent's folder... – Dominik Jun 14 '18 at 11:34
8

If you want the folder where the batch file is located, you can assign

SET folder=%~dp0

as mentioned in another answer.

If you want the folder above the location of the batch file, you can assign

SET folder=%~dp0..\

However, this last variable could be inadequate if you plan to show the path to the user. In that case, perphaps it is preferable

FOR %%A IN ("%~dp0.") DO SET folder=%%~dpA

As an example, if you have the following script in C:\Users\Public

@ECHO OFF
ECHO %~dp0
ECHO %~dp0..\
FOR %%A IN ("%~dp0.") DO ECHO %%~dpA

the output will be

C:\Users\Public\
C:\Users\Public\..\
C:\Users\
ASdeL
  • 211
  • 2
  • 6
6

Endoro's answer doesn't work for me either but this does. This is what I use myself to do this.

V3

One of these should do the right thing

for %%I in ("%~dp0.") do for %%J in ("%%~dpI.") do set ParentFolderName=%%~nxJ
echo %ParentFolderName%

for %%I in ("%~dp0.") do for %%J in ("%%~dpI.") do set ParentFolderName=%%~dpnxJ
echo %ParentFolderName%

V2:

for %%I in ("%~dp0\.") do set ParentFolderName=%%~nxI
echo %ParentFolderName%

V1:

This one gets the parent directory of the current working directory

for %%I in (..) do set ParentFolderName=%%~nI%%~xI
echo %ParentFolderName%

Reference: For | Microsoft Docs

cpcolella
  • 247
  • 4
  • 10
  • 2
    that's the parent folder of the current working folder, not the parent folder of the batch file (they can be the same, but that's not guaranteed). (btw: `%%~nI%%~xI` can be shortened to `%%~nxI`) – Stephan Feb 03 '20 at 20:27
  • But now it's nearly the original answer, withthe difference that you only get the name of the folder without any prepending informations. For `C:\Projects\User1\batchFiles\myFile.bat` your solution extracts only `batchFiles` – jeb Feb 04 '20 at 05:48
  • (there is apparently some confusion of what the "parent folder" is - for my understanding, `batchfiles` is the "folder of the batch file" and its parent is `User1`, but OP seems to have a different definition) – Stephan Feb 04 '20 at 08:40
  • @cpcoletta: `%~dp0` already includes the trailing backslash, so `%~dp0\.` makes no sense. – Stephan Feb 04 '20 at 08:42
  • @Stephan gotcha. One more try here. – cpcolella Feb 04 '20 at 19:21
  • 1
    This works even if the name of a one of the folders in the path has whitespace. Some of the other solutions I found failed in that respect. Man, I *really* hate Windows. – Jeff Holt Jul 02 '20 at 05:44
  • @Jeff Holt Thanks. Windows is the worst. Glad to be in good company. – cpcolella Jul 03 '20 at 07:13
0

So this is a bad answer, but I use it more times than I'll admit these days:

powershell -command "(get-item '%~dp0').name" > %temp%\temp_var.txt
set /p parent_folder= < %temp%\temp_var.txt
echo %parent_folder%
pause

Since PowerShell is nearly everywhere these days when it comes to Windows, I abuse it as much as I can.

However, it's bad because it does rely on PowerShell existing within your environment path, and it relies on no other script modifying your temporary storage file between your script writing to it and reading it.