lance
lance

Reputation: 16342

DOS batch: get last folder from relative path

I have the following values in a DOS batch file (for example...):

..\Apple\Jones  
..\Banana\Smith  
..\Pear\Wilson  

I need to extract the last name values ("Jones", "Smith", "Wilson") from each value. What one technique can I use that will always give me these substring values?

Upvotes: 2

Views: 7384

Answers (2)

Frank Einstein
Frank Einstein

Reputation: 701

I already wrote a function for that. You give it any path and it returns you only it's filename or pathname. Works for any path: Url, Windows path, Unix path, etc...

Copy this function at the end of your batch script: (Instructions below)

rem ===========================================================================

:Name_From_Path
SetLocal

set _TMP_FOLDERNAME=%1
for %%g in ("%_TMP_FOLDERNAME%") do set _TMP_FOLDERNAME=%%~nxg

EndLocal & set _Name_From_Path=%_TMP_FOLDERNAME%
goto :EOF

rem ===========================================================================

Usage:

CALL :Name_Of_Path ..\Apple\Jones
ECHO %_Name_From_Path%

Result: Jones

Upvotes: -1

Nicolas
Nicolas

Reputation: 6484

According to this topic : What is the best way to do a substring in a batch file?

I suggest you to use

%~n0

Upvotes: 3

Related Questions