Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Last Part of Path

15 views
Skip to first unread message

Joachim Hofmann

unread,
Jun 30, 2008, 6:46:12 AM6/30/08
to
Hello,

how can I get the last part of a given Path?

c:\dir1\dir2\dir3\

should result in

dir3\

Thank You

Joachim

t.m.tr...@gmail.com

unread,
Jun 30, 2008, 7:04:58 AM6/30/08
to

set p=c:\dir1\dir2\dir3\
if %p:~-1%==\ set p=%p:~0,-1%
for %%G in (%p%) do set p=%%~nxG\
echo %p%

Cheers,

Tomek

foxidrive

unread,
Jun 30, 2008, 7:41:28 AM6/30/08
to

@echo off
set p=c:\dir 1\dir 2\dir 3\
for %%G in ("%p%.") do set p=%%~nxG
echo %p%
pause

t.m.tr...@gmail.com

unread,
Jun 30, 2008, 8:19:21 AM6/30/08
to
On 30 Jun, 12:41, foxidrive <got...@woohoo.invalid> wrote:
> for %%G in ("%p%.") do set p=%%~nxG

Nice trick Foxidrive. I've never seen that one before.

Cheers,

Tomek

Timo Salmi

unread,
Jun 30, 2008, 1:33:43 PM6/30/08
to
Joachim Hofmann <spei...@freenet.de> wrote:
> how can I get the last part of a given Path?
> c:\dir1\dir2\dir3\
> should result in
> dir3\

80} How can I extract the last part of a path such as C:\ABC\DEF\GHI\?
http://www.netikka.net/tsneti/info/tscmd080.htm

144} How do I parse the items from a path like C:\a\b\c\d ?
http://www.netikka.net/tsneti/info/tscmd144.htm

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

foxidrive

unread,
Jun 30, 2008, 3:42:47 PM6/30/08
to
On Mon, 30 Jun 2008 20:33:43 +0300, Timo Salmi <t...@uwasa.fi> wrote:

>Joachim Hofmann <spei...@freenet.de> wrote:
>> how can I get the last part of a given Path?
>> c:\dir1\dir2\dir3\
>> should result in
>> dir3\
>
> 80} How can I extract the last part of a path such as C:\ABC\DEF\GHI\?
> http://www.netikka.net/tsneti/info/tscmd080.htm
>
> 144} How do I parse the items from a path like C:\a\b\c\d ?
> http://www.netikka.net/tsneti/info/tscmd144.htm

Try this Timo, it seems to handle these cases just fine:

@echo off
call :next "C:\ABC\\DEF XYZ\.GHI\" A
call :next "C:\ABC\\DEF XYZ\.GHI\\" B
call :next "C:\ABC\DEF XYZ\.GHI\" C
call :next "C:\ABC\DEF XYZ\.GHI" D
call :next "C:\ABC\DEF XYZ\GHI\" E
call :next "C:\ABC\DEF XYZ\GHI" F
call :next "d:\folder 1\folder 2\folder 3\" G
call :next "d:\folder 1\folder 2\folder 3.abc\" H
call :next "d:\folder 1\folder 2\folder 3.def" I

goto :eof
:next
set "p=%~1"


for %%G in ("%p%.") do set p=%%~nxG

echo %2 %p%
pause

Timo Salmi

unread,
Jul 2, 2008, 12:39:06 PM7/2/08
to
foxidrive <got...@woohoo.invalid> wrote:
>> 80} How can I extract the last part of a path such as C:\ABC\DEF\GHI\?
>> http://www.netikka.net/tsneti/info/tscmd080.htm
>> 144} How do I parse the items from a path like C:\a\b\c\d ?
>> http://www.netikka.net/tsneti/info/tscmd144.htm

> Try this Timo, it seems to handle these cases just fine:

Thanks. Added a link in the FAQ to your posting.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland

Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

0 new messages