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 t
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%
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