Here is a solution, did not code solution for file being in root.
Variable f is what I believe you are asking for.
foo.cmd
@echo off setlocal ::: p parent ::: g grandparent ::: a grandparent absolute ::: f folder of this batch file set p=%~dp0 set g=%~dp0\.. FOR /F "delims=" %%t IN ("%g%") DO SET "a=%%~ft" call set f=%%p:%a%=%% call set f=%%f:\=%% echo p is: %p% echo g is: %g% echo a is: %a% echo f is: %f% endlocal
Test Cases
L:\test>foo.cmd p is: L:\test\ g is: L:\test\\.. a is: L:\ f is: test L:\test>md subdir L:\test>cd subdir L:\test\subdir>..\foo.cmd p is: L:\test\ g is: L:\test\\.. a is: L:\ f is: test L:\test\subdir>copy ..\foo.cmd 1 file(s) copied. L:\test\subdir>foo.cmd p is: L:\test\subdir\ g is: L:\test\subdir\\.. a is: L:\test f is: subdir L:\test\subdir>cd.. L:\test>cd.. L:\>copy test\foo.cmd 1 file(s) copied. L:\>foo.cmd p is: L:\ g is: L:\\.. a is: L:\ f is: \=
%0for %%I in ("%~0\..") do @echo %%~nxI