capturing last node of the current directory string


T

TJT

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.
 
Ad

Advertisements

P

Phil Robyn

TJT said:
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%=%%
 
T

TJT

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
 
P

Phil Robyn

TJT said:
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 ====================
 
J

Jerold Schulman

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
 
P

Phil Robyn

TJT said:
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 ====================
 
Ad

Advertisements

T

TJT

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
 
A

Al Dunbar [MS-MVP]

Jerold Schulman said:
@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
 
Ad

Advertisements

J

Jerold Schulman

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/for folder containing batch file:
call:lastnode "%~dp0"
echo/for current directory:
call:lastnode "%cd%"


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



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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top