I've written a complex function for the same purpose, so maybe I can help... This is a very simplified version (it is unable to handle paths at the drive root and first level).
The behaviour of a trailing backslash is actually a useful thing to exploit. You can determine if a path is a folder, a file, or nonexistent. In this case we're just stripping trailing backslashes though, to format folder paths.
Get parent folder name:
Code: Select all
set "check=C:\My\Folder\Path\"
if "%check:~-1%"=="\" set "check=%check:~,-1%"
for %%a in ("%check%") do set "check=%%~dpa"
for %%a in ("%check:~,-1%") do echo Parent folder: "%%~nxa"
django, you probably know, but your top most dir function returns the drive letter for fully qualified paths.