PC Review


Reply
Thread Tools Rate Thread

capturing last node of the current directory string

 
 
TJT
Guest
Posts: n/a
 
      17th Mar 2005
I have a bat file in which I need to know the last node of a directory
string. I know that I am not properly describing it so let me give an
example...

Assuming that I am executing a bat file from the directory path
C:\TestPrograms\Version6\Release2

I would like to be able to obtain the last node ("Release2"). I've tried
everything but can't seem to get this to work.

I tried something like the following
SET FullPath=%CD%
cd..
SET PartPath=%CD%

At this point
FullPath=C:\TestPrograms\Version6\Release2
and
PartPath=C:\TestPrograms\Version6

I tried executing the following to essentially change
"C:\TestPrograms\Version6" to empty string but it didn't work as I had hoped
SET LastNode = %FullPath:%PartPath=%

Any help would be greatly appreciated.



 
Reply With Quote
 
 
 
 
Phil Robyn
Guest
Posts: n/a
 
      17th Mar 2005
TJT wrote:
> I have a bat file in which I need to know the last node of a directory
> string. I know that I am not properly describing it so let me give an
> example...
>
> Assuming that I am executing a bat file from the directory path
> C:\TestPrograms\Version6\Release2
>
> I would like to be able to obtain the last node ("Release2"). I've tried
> everything but can't seem to get this to work.
>
> I tried something like the following
> SET FullPath=%CD%
> cd..
> SET PartPath=%CD%
>
> At this point
> FullPath=C:\TestPrograms\Version6\Release2
> and
> PartPath=C:\TestPrograms\Version6
>
> I tried executing the following to essentially change
> "C:\TestPrograms\Version6" to empty string but it didn't work as I had hoped
> SET LastNode = %FullPath:%PartPath=%
>
> Any help would be greatly appreciated.
>
>
>


If you have Win2000 or WinXP, you can use delayed expansion:

set LastNode=!FullPath:%PartPath%=!

If you have NT4.0:

call set LastNode=%%FullPath:%PartPath%=%%

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l
 
Reply With Quote
 
TJT
Guest
Posts: n/a
 
      17th Mar 2005
Phil - Thanks for the reply.

I tried executing the following test batch....
@echo off
SET FullPath=C:\TestPrograms\Version6\Release2
SET PartPath=C:\TestPrograms\Version6

echo FullPath=%FullPath%
echo PartPath=%PartPath%

set LastNode=!FullPath:%PartPath%=!
echo LastNode=%LastNode%


I was hoping to get LastNode=Version6 but I am not getting it. Here's the
output from the batch...
FullPath=C:\TestPrograms\Version6\Release2
PartPath=C:\TestPrograms\Version6
LastNode=!FullPath:C:\TestPrograms\Version6=!

Thanks,
Tom
"Phil Robyn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> TJT wrote:
> > I have a bat file in which I need to know the last node of a directory
> > string. I know that I am not properly describing it so let me give an
> > example...
> >
> > Assuming that I am executing a bat file from the directory path
> > C:\TestPrograms\Version6\Release2
> >
> > I would like to be able to obtain the last node ("Release2"). I've

tried
> > everything but can't seem to get this to work.
> >
> > I tried something like the following
> > SET FullPath=%CD%
> > cd..
> > SET PartPath=%CD%
> >
> > At this point
> > FullPath=C:\TestPrograms\Version6\Release2
> > and
> > PartPath=C:\TestPrograms\Version6
> >
> > I tried executing the following to essentially change
> > "C:\TestPrograms\Version6" to empty string but it didn't work as I had

hoped
> > SET LastNode = %FullPath:%PartPath=%
> >
> > Any help would be greatly appreciated.
> >
> >
> >

>
> If you have Win2000 or WinXP, you can use delayed expansion:
>
> set LastNode=!FullPath:%PartPath%=!
>
> If you have NT4.0:
>
> call set LastNode=%%FullPath:%PartPath%=%%
>
> --
> Phil Robyn
> Univ. of California, Berkeley
>
> u n z i p m y a d d r e s s t o s e n d e - m a i l



 
Reply With Quote
 
Phil Robyn
Guest
Posts: n/a
 
      17th Mar 2005
TJT wrote:

> Phil - Thanks for the reply.
>
> I tried executing the following test batch....
> @echo off
> SET FullPath=C:\TestPrograms\Version6\Release2
> SET PartPath=C:\TestPrograms\Version6
>
> echo FullPath=%FullPath%
> echo PartPath=%PartPath%
>
> set LastNode=!FullPath:%PartPath%=!
> echo LastNode=%LastNode%
>
>
> I was hoping to get LastNode=Version6 but I am not getting it. Here's the
> output from the batch...


The last node is 'Release2'. How could you hope to get 'LastNode=Version6'?

> FullPath=C:\TestPrograms\Version6\Release2
> PartPath=C:\TestPrograms\Version6
> LastNode=!FullPath:C:\TestPrograms\Version6=!
>
> Thanks,
> Tom


For Win2000 or WinXP:

C:\cmd>demo\zzzz
FullPath=C:\TestPrograms\Version6\Release2
PartPath=C:\TestPrograms\Version6
LastNode=\Release2

C:\cmd>rlist demo\zzzz.cmd
=====begin C:\cmd\demo\zzzz.cmd ====================
01. @echo off
02. setlocal ENABLEDELAYEDEXPANSION
03. SET FullPath=C:\TestPrograms\Version6\Release2
04. SET PartPath=C:\TestPrograms\Version6
05.
06. echo FullPath=%FullPath%
07. echo PartPath=%PartPath%
08.
09. set LastNode=!FullPath:%PartPath%=!
10. echo LastNode=%LastNode%
=====end C:\cmd\demo\zzzz.cmd ====================

For WinNT4.0:

C:\cmd>demo\zzzz40
FullPath=C:\TestPrograms\Version6\Release2
PartPath=C:\TestPrograms\Version6
LastNode=\Release2

C:\cmd>rlist demo\zzzz40.cmd
=====begin C:\cmd\demo\zzzz40.cmd ====================
01. @echo off
02. setlocal
03. SET FullPath=C:\TestPrograms\Version6\Release2
04. SET PartPath=C:\TestPrograms\Version6
05.
06. echo FullPath=%FullPath%
07. echo PartPath=%PartPath%
08.
09. call set LastNode=%%FullPath:%PartPath%=%%
10. echo LastNode=%LastNode%
=====end C:\cmd\demo\zzzz40.cmd ====================

> "Phil Robyn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>TJT wrote:
>>
>>>I have a bat file in which I need to know the last node of a directory
>>>string. I know that I am not properly describing it so let me give an
>>>example...
>>>
>>>Assuming that I am executing a bat file from the directory path
>>> C:\TestPrograms\Version6\Release2
>>>
>>>I would like to be able to obtain the last node ("Release2"). I've

>
> tried
>
>>>everything but can't seem to get this to work.
>>>
>>>I tried something like the following
>>>SET FullPath=%CD%
>>>cd..
>>>SET PartPath=%CD%
>>>
>>>At this point
>>> FullPath=C:\TestPrograms\Version6\Release2
>>>and
>>> PartPath=C:\TestPrograms\Version6
>>>
>>>I tried executing the following to essentially change
>>>"C:\TestPrograms\Version6" to empty string but it didn't work as I had

>
> hoped
>
>>>SET LastNode = %FullPath:%PartPath=%
>>>
>>>Any help would be greatly appreciated.
>>>
>>>
>>>

>>
>>If you have Win2000 or WinXP, you can use delayed expansion:
>>
>> set LastNode=!FullPath:%PartPath%=!
>>
>>If you have NT4.0:
>>
>> call set LastNode=%%FullPath:%PartPath%=%%
>>
>>--
>>Phil Robyn
>>Univ. of California, Berkeley
>>
>>u n z i p m y a d d r e s s t o s e n d e - m a i l

>
>
>



--
Phil Robyn [MS MVP]
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l
 
Reply With Quote
 
Jerold Schulman
Guest
Posts: n/a
 
      17th Mar 2005
On Thu, 17 Mar 2005 13:45:47 -0500, "TJT" <(E-Mail Removed)> wrote:

>I have a bat file in which I need to know the last node of a directory
>string. I know that I am not properly describing it so let me give an
>example...
>
>Assuming that I am executing a bat file from the directory path
> C:\TestPrograms\Version6\Release2
>
>I would like to be able to obtain the last node ("Release2"). I've tried
>everything but can't seem to get this to work.
>
>I tried something like the following
>SET FullPath=%CD%
>cd..
>SET PartPath=%CD%
>
>At this point
> FullPath=C:\TestPrograms\Version6\Release2
>and
> PartPath=C:\TestPrograms\Version6
>
>I tried executing the following to essentially change
>"C:\TestPrograms\Version6" to empty string but it didn't work as I had hoped
>SET LastNode = %FullPath:%PartPath=%
>
>Any help would be greatly appreciated.
>
>


@echo off
setlocal
call :lastnode "C:\TestPrograms\Version6\Release2"
@echo Last node is %lstn%
@echo Parent path is %prnt%
endlocal
goto :EOF
:lastnode
set lstn=%~n1
set prnt=%~dp1

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
Reply With Quote
 
Phil Robyn
Guest
Posts: n/a
 
      17th Mar 2005
TJT wrote:
> Phil - Thanks for the reply.
>
> I tried executing the following test batch....
> @echo off
> SET FullPath=C:\TestPrograms\Version6\Release2
> SET PartPath=C:\TestPrograms\Version6
>
> echo FullPath=%FullPath%
> echo PartPath=%PartPath%
>
> set LastNode=!FullPath:%PartPath%=!
> echo LastNode=%LastNode%
>
>
> I was hoping to get LastNode=Version6 but I am not getting it. Here's the
> output from the batch...
> FullPath=C:\TestPrograms\Version6\Release2
> PartPath=C:\TestPrograms\Version6
> LastNode=!FullPath:C:\TestPrograms\Version6=!
>
> Thanks,
> Tom


'Version6' is the *next*-to-last node.

C:\cmd>demo\NexttoLastNode
Next to last node is Version6

C:\cmd>rlist demo\NexttoLastNode.cmd
=====begin C:\cmd\demo\NexttoLastNode.cmd ====================
01. @echo off
02. setlocal
03. SET FullPath=C:\TestPrograms\Version6\Release2
04. set first=
05. set rest=
06. :loop
07. for /f "tokens=1* delims=\" %%a in (
08. 'echo %FullPath%'
09. ) do set first=%%a&set FullPath=%%b
10. if defined FullPath (
11. set next_to_last=%first%
12. goto :loop
13. )
14. echo/Next to last node is %next_to_last%
=====end C:\cmd\demo\NexttoLastNode.cmd ====================

> "Phil Robyn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>TJT wrote:
>>
>>>I have a bat file in which I need to know the last node of a directory
>>>string. I know that I am not properly describing it so let me give an
>>>example...
>>>
>>>Assuming that I am executing a bat file from the directory path
>>> C:\TestPrograms\Version6\Release2
>>>
>>>I would like to be able to obtain the last node ("Release2"). I've

>
> tried
>
>>>everything but can't seem to get this to work.
>>>
>>>I tried something like the following
>>>SET FullPath=%CD%
>>>cd..
>>>SET PartPath=%CD%
>>>
>>>At this point
>>> FullPath=C:\TestPrograms\Version6\Release2
>>>and
>>> PartPath=C:\TestPrograms\Version6
>>>
>>>I tried executing the following to essentially change
>>>"C:\TestPrograms\Version6" to empty string but it didn't work as I had

>
> hoped
>
>>>SET LastNode = %FullPath:%PartPath=%
>>>
>>>Any help would be greatly appreciated.
>>>
>>>
>>>

>>
>>If you have Win2000 or WinXP, you can use delayed expansion:
>>
>> set LastNode=!FullPath:%PartPath%=!
>>
>>If you have NT4.0:
>>
>> call set LastNode=%%FullPath:%PartPath%=%%
>>
>>--
>>Phil Robyn
>>Univ. of California, Berkeley
>>
>>u n z i p m y a d d r e s s t o s e n d e - m a i l

>
>
>



--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l
 
Reply With Quote
 
TJT
Guest
Posts: n/a
 
      17th Mar 2005
I'm sorry Phil - I cut and pasted the wrong value. My fingers are a few
steps ahead of my brain. Sorry for the confusion.

I am not looking for Version6 (next-to-last node), I am actually looking for
Release2.

Thanks,
Tom
"Phil Robyn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> TJT wrote:
> > Phil - Thanks for the reply.
> >
> > I tried executing the following test batch....
> > @echo off
> > SET FullPath=C:\TestPrograms\Version6\Release2
> > SET PartPath=C:\TestPrograms\Version6
> >
> > echo FullPath=%FullPath%
> > echo PartPath=%PartPath%
> >
> > set LastNode=!FullPath:%PartPath%=!
> > echo LastNode=%LastNode%
> >
> >
> > I was hoping to get LastNode=Version6 but I am not getting it. Here's

the
> > output from the batch...
> > FullPath=C:\TestPrograms\Version6\Release2
> > PartPath=C:\TestPrograms\Version6
> > LastNode=!FullPath:C:\TestPrograms\Version6=!
> >
> > Thanks,
> > Tom

>
> 'Version6' is the *next*-to-last node.
>
> C:\cmd>demo\NexttoLastNode
> Next to last node is Version6
>
> C:\cmd>rlist demo\NexttoLastNode.cmd
> =====begin C:\cmd\demo\NexttoLastNode.cmd ====================
> 01. @echo off
> 02. setlocal
> 03. SET FullPath=C:\TestPrograms\Version6\Release2
> 04. set first=
> 05. set rest=
> 06. :loop
> 07. for /f "tokens=1* delims=\" %%a in (
> 08. 'echo %FullPath%'
> 09. ) do set first=%%a&set FullPath=%%b
> 10. if defined FullPath (
> 11. set next_to_last=%first%
> 12. goto :loop
> 13. )
> 14. echo/Next to last node is %next_to_last%
> =====end C:\cmd\demo\NexttoLastNode.cmd ====================
>
> > "Phil Robyn" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> >>TJT wrote:
> >>
> >>>I have a bat file in which I need to know the last node of a directory
> >>>string. I know that I am not properly describing it so let me give an
> >>>example...
> >>>
> >>>Assuming that I am executing a bat file from the directory path
> >>> C:\TestPrograms\Version6\Release2
> >>>
> >>>I would like to be able to obtain the last node ("Release2"). I've

> >
> > tried
> >
> >>>everything but can't seem to get this to work.
> >>>
> >>>I tried something like the following
> >>>SET FullPath=%CD%
> >>>cd..
> >>>SET PartPath=%CD%
> >>>
> >>>At this point
> >>> FullPath=C:\TestPrograms\Version6\Release2
> >>>and
> >>> PartPath=C:\TestPrograms\Version6
> >>>
> >>>I tried executing the following to essentially change
> >>>"C:\TestPrograms\Version6" to empty string but it didn't work as I had

> >
> > hoped
> >
> >>>SET LastNode = %FullPath:%PartPath=%
> >>>
> >>>Any help would be greatly appreciated.
> >>>
> >>>
> >>>
> >>
> >>If you have Win2000 or WinXP, you can use delayed expansion:
> >>
> >> set LastNode=!FullPath:%PartPath%=!
> >>
> >>If you have NT4.0:
> >>
> >> call set LastNode=%%FullPath:%PartPath%=%%
> >>
> >>--
> >>Phil Robyn
> >>Univ. of California, Berkeley
> >>
> >>u n z i p m y a d d r e s s t o s e n d e - m a i l

> >
> >
> >

>
>
> --
> Phil Robyn
> Univ. of California, Berkeley
>
> u n z i p m y a d d r e s s t o s e n d e - m a i l



 
Reply With Quote
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      18th Mar 2005

"Jerold Schulman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 17 Mar 2005 13:45:47 -0500, "TJT" <(E-Mail Removed)> wrote:
>
> >I have a bat file in which I need to know the last node of a directory
> >string. I know that I am not properly describing it so let me give an
> >example...
> >
> >Assuming that I am executing a bat file from the directory path
> > C:\TestPrograms\Version6\Release2
> >
> >I would like to be able to obtain the last node ("Release2"). I've tried
> >everything but can't seem to get this to work.
> >
> >I tried something like the following
> >SET FullPath=%CD%
> >cd..
> >SET PartPath=%CD%
> >
> >At this point
> > FullPath=C:\TestPrograms\Version6\Release2
> >and
> > PartPath=C:\TestPrograms\Version6
> >
> >I tried executing the following to essentially change
> >"C:\TestPrograms\Version6" to empty string but it didn't work as I had

hoped
> >SET LastNode = %FullPath:%PartPath=%
> >
> >Any help would be greatly appreciated.
> >
> >

>
> @echo off
> setlocal
> call :lastnode "C:\TestPrograms\Version6\Release2"
> @echo Last node is %lstn%
> @echo Parent path is %prnt%
> endlocal
> goto :EOF
> :lastnode
> set lstn=%~n1
> set prnt=%~dp1


Finally, the straight-forward answer. Considering that folder names may have
extensions, however, shouldn't that second last line actually be:

set lstn=%~nx1

Also lost in the verbiage might have been how to actually get the path
needed:

> @echo off
> setlocal

echo/for folder containing batch file:
call:lastnode "%~dp0"
> @echo Last node is %lstn%
> @echo Parent path is %prnt%

echo/for current directory:
call:lastnode "%cd%"
> @echo Last node is %lstn%
> @echo Parent path is %prnt%
> endlocal
> goto :EOF
> :lastnode

set lstn=%~nx1
> set prnt=%~dp1



/Al


 
Reply With Quote
 
Jerold Schulman
Guest
Posts: n/a
 
      18th Mar 2005
On Thu, 17 Mar 2005 22:08:44 -0700, "Al Dunbar [MS-MVP]" <alan-no-drub-(E-Mail Removed)> wrote:

>
>"Jerold Schulman" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> On Thu, 17 Mar 2005 13:45:47 -0500, "TJT" <(E-Mail Removed)> wrote:
>>
>> >I have a bat file in which I need to know the last node of a directory
>> >string. I know that I am not properly describing it so let me give an
>> >example...
>> >
>> >Assuming that I am executing a bat file from the directory path
>> > C:\TestPrograms\Version6\Release2
>> >
>> >I would like to be able to obtain the last node ("Release2"). I've tried
>> >everything but can't seem to get this to work.
>> >
>> >I tried something like the following
>> >SET FullPath=%CD%
>> >cd..
>> >SET PartPath=%CD%
>> >
>> >At this point
>> > FullPath=C:\TestPrograms\Version6\Release2
>> >and
>> > PartPath=C:\TestPrograms\Version6
>> >
>> >I tried executing the following to essentially change
>> >"C:\TestPrograms\Version6" to empty string but it didn't work as I had

>hoped
>> >SET LastNode = %FullPath:%PartPath=%
>> >
>> >Any help would be greatly appreciated.
>> >
>> >

>>
>> @echo off
>> setlocal
>> call :lastnode "C:\TestPrograms\Version6\Release2"
>> @echo Last node is %lstn%
>> @echo Parent path is %prnt%
>> endlocal
>> goto :EOF
>> :lastnode
>> set lstn=%~n1
>> set prnt=%~dp1

>
>Finally, the straight-forward answer. Considering that folder names may have
>extensions, however, shouldn't that second last line actually be:
>
> set lstn=%~nx1
>
>Also lost in the verbiage might have been how to actually get the path
>needed:
>
>> @echo off
>> setlocal

> echo/for folder containing batch file:
> call:lastnode "%~dp0"
>> @echo Last node is %lstn%
>> @echo Parent path is %prnt%

> echo/for current directory:
> call:lastnode "%cd%"
>> @echo Last node is %lstn%
>> @echo Parent path is %prnt%
>> endlocal
>> goto :EOF
>> :lastnode

> set lstn=%~nx1
>> set prnt=%~dp1

>
>
>/Al
>


Yes, in the general case set lstn=%~nx1 is correct.



Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find node in TreeView by using string(the same as Node.Text) ? jiing Microsoft ASP .NET 0 27th Apr 2007 03:34 AM
Menu Control - current node AG Microsoft ASP .NET 10 26th Sep 2006 12:46 PM
Add treeview node "manually" as string with PathSeparator as divider in the string Mikael Jansson Microsoft Dot NET Framework Forms 2 1st Aug 2005 09:15 AM
Current Directory String Brad Markisohn Microsoft C# .NET 6 17th Sep 2004 11:01 PM
How to get file's current directory path to a string in VBA? wenkej@nj.sc.mcel.mot.com Microsoft Excel Programming 1 16th Oct 2003 07:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:08 PM.