How to get windows batch's parent folder
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