Question

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?

Was it helpful?

Solution

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

I suggest you to use

%~n0

OTHER TIPS

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, Linux 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top