2

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?

lance
  • 16,092
  • 19
  • 77
  • 136
  • [Windows cmd is **not** DOS](https://superuser.com/q/451432/241386). DOS has no ability like `%~n0` – phuclv Jun 20 '17 at 02:11

2 Answers2

3

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

I suggest you to use

%~n0
Community
  • 1
  • 1
Nicolas
  • 6,289
  • 4
  • 36
  • 51
  • I don't follow. That shows me the name of the batch file? How does that give me for what the question asks? – lance Nov 30 '10 at 14:10
  • 2
    Sorry for my bad explanation. %0 contains the path to your script. With %~n0 you extract the script name only. Here is a more explicit exemple : for /D %%i in ("..\Apple\Jones","..\Banana\Smith","..\Pear\Wilson") do echo %%~ni – Nicolas Nov 30 '10 at 14:25
  • 1
    Delightful. Thank you. Note: I used echo %%~ni%%~xi, as my last folder (the real data) is a directory whose name includes a period. – lance Nov 30 '10 at 15:03
  • `%%~ni%%~xi` can be written as `%%~nxi` – Stephan Jun 20 '17 at 05:35
0

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

Frank Einstein
  • 676
  • 8
  • 15