In tip 0494, I described how to parse a batch parameter.

When you use the %~n syntax, the CMD.EXE processor actually enumerates the string after the last \.

If variable VAR contains a folder path, you can extract the last sub-folder using:

for %%s in ("%VAR%") do set Last=%%~ns

Example

If variable VAR contains C:\Folder 1\Folder 2\Folder 3, then using:

for %%s in ("%VAR%") do set Last=%%~ns
@echo %Last%

in a batch script would echo:

Folder 3