In case anyone looks this thread up in the future, I came up with another way of doing this. It took me some long, hard thought to come up with something that is ultimately so simple. It helps to know all of your built-in variables. @ECHO OFF CD /D "%~dp1" FOR %%G IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF '"%CD%"'=='"%%G:\"' SET LD=%%G IF NOT '"%LD%"'=='""' GOTO :NEXT CALL :LASTDIR "%CD%" :LASTDIR SET LD=%~nx1 :NEXT ECHO %LD% PAUSE The first line assumes that you're relying on a %1 variable (which represents either a file or a directory) that has been passed from a previous routine. The quotes from line four are not part of the LD variable when it is created in the :LASTDIR section. The %~nx1 ensures that any directories named like folder_name.stuff.more.stuff are not truncated after the word name. This works for any directory, even ones that end up just root drive letters like C: (that is what line two does). If you make this into a .bat file, you can test it by running the batch file in any directory, even when you have no predefined %1 variable. When there is no %1, the LD variable will always return the last directory in the path that the batch file is running from. I honestly still can't wrap my head around the syntax of most of the solutions posted above since I'm no command line guru, so I came up with this one because I understand it 100%. Thanks to Delprat for making me think about this again.