开发者

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 extra开发者_Go百科ct the last name values ("Jones", "Smith", "Wilson") from each value. What one technique can I use that will always give me these substring values?


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

I suggest you to use

%~n0


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜