Extracting last Folder Name from complete path

  • Thread starter Thread starter andreas kässens
  • Start date Start date
A

andreas kässens

Hi,

I am trying to extract the last Folder from a complete path. (e.g.
C:\temp\ExtendedDB > ExtendedDB).
My idea is, to define the "\" as a Delimiter and chose the last token. But I
haven't found a decent way, yet.
Any Ideas?

Greets

Andreas
 
I am trying to extract the last Folder from a complete path. (e.g.
C:\temp\ExtendedDB > ExtendedDB).
My idea is, to define the "\" as a Delimiter and chose the last token. But I
haven't found a decent way, yet.
Any Ideas?

Using 4NT/4DOS, I do this frequently thus:
ECHO %@WORD["\",-0,C:\temp\ExtendedDB]

4NT is a commercial product, 4DOS is free
(<http://jpsoft.com/download.htm>). @Word is documented at
<http://jpsoft.com/help/f_word.htm>.
 
Using 4NT/4DOS, I do this frequently thus:
ECHO %@WORD["\",-0,C:\temp\ExtendedDB]

4NT is a commercial product, 4DOS is free

Thanks a lot for your answer! But I have to use the standard Windows xp
commands, which do not have such function. So I tried the following batch:

@echo off
set pfad=D:\Arbeit\PROJEKTE\ATARI 2
for /f "delims=\ tokens=4" %%a in ("@echo %pfad%") do @echo %%a

Unfortunately there seems to be no way to address the last token in general
("tokens=-1" does not work). The only way I can think of, is another for
loop, which counts the "\" in the variing path and replace it with the
tokens value.

This seems to be far too much effort for such a simple task, doesn't it?

Andreas
 
The command processor provides file name parsing directly in the FOR ...

@echo off
set pfad=D:\Arbeit\PROJEKTE\ATARI 2
for %%a in ("%PFAD%") do echo %%~na

Type FOR/? at a command prompt for more info and other parsing options.

Tom Lavedas
===========

andreas kässens said:
Using 4NT/4DOS, I do this frequently thus:
ECHO %@WORD["\",-0,C:\temp\ExtendedDB]

4NT is a commercial product, 4DOS is free

Thanks a lot for your answer! But I have to use the standard Windows xp
commands, which do not have such function. So I tried the following batch:

@echo off
set pfad=D:\Arbeit\PROJEKTE\ATARI 2
for /f "delims=\ tokens=4" %%a in ("@echo %pfad%") do @echo %%a

Unfortunately there seems to be no way to address the last token in general
("tokens=-1" does not work). The only way I can think of, is another for
loop, which counts the "\" in the variing path and replace it with the
tokens value.

This seems to be far too much effort for such a simple task, doesn't it?

Andreas
 
Using 4NT/4DOS, I do this frequently thus:
ECHO %@WORD["\",-0,C:\temp\ExtendedDB]

4NT is a commercial product, 4DOS is free

Thanks a lot for your answer! But I have to use the standard Windows xp
commands, which do not have such function. So I tried the following batch:

@echo off
set pfad=D:\Arbeit\PROJEKTE\ATARI 2
for /f "delims=\ tokens=4" %%a in ("@echo %pfad%") do @echo %%a

Unfortunately there seems to be no way to address the last token in general
("tokens=-1" does not work). The only way I can think of, is another for
loop, which counts the "\" in the variing path and replace it with the
tokens value.

This seems to be far too much effort for such a simple task, doesn't it?

Andreas

set pfad=D:\Arbeit\PROJEKTE\ATARI 2
set work1=%pfad%
:loop
for /f "Tokens=1* Delims=\" %%a in ('@echo %work1%') do (
set work2=%%a
set work3=%%b
)
if "%Work3%" NEQ "" set work1=%work3%&goto loop
@echo %work2%


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
andreas kässens schrieb:
@echo off
set pfad=D:\Arbeit\PROJEKTE\ATARI 2
for /f "delims=\ tokens=4" %%a in ("@echo %pfad%") do @echo %%a

Unfortunately there seems to be no way to address the last token in general
("tokens=-1" does not work). The only way I can think of, is another for
loop, which counts the "\" in the variing path and replace it with the
tokens value.

This seems to be far too much effort for such a simple task, doesn't it?

Hi Andreas,
I see Tom Lavedas was faster with a similar solution.

Call arguments and for variables can return parts with the tilde
commands: ~d drive, ~p path , ~n name, ~x extension. They work equally
if the last element is a folder name. See call /? and for /?.

Some code to ashure it is a folder and not terminated with a back slash:

@echo off&setlocal
set Folder=%~1
if not defined Folder echo Please pass folder argument&exit /B 1
echo Folder %Folder%
:: Remove trailing back slash
if "%Folder:~-1%" EQU "\" set Folder=%Folder:~0,-1%
:: Check if folder and if it exists
call :IsFolder "%Folder%" ||(echo not a folder or absent & exit /b 1)
for /f "delims=" %%A in ("%Folder%") do set LFolder=%%~nxA
echo Last Folder is %LFolder%
goto :eof
:IsFolder %1
:: returns errorlevel 1 if folder2chk doesn't exist
:: returns errorlevel 2 if folder2chk isn't a foldert
setlocal
set "FldAttr=%~a1"
if NOT defined FldAttr exit /B 1
if [%FldAttr:~0,1%] NEQ [d] exit /B 2
exit /B 0

HTH
 
Wow!!

Many thanks for all the superb replies!!

I did indeed read about the %~nI option but did't realize that it works for
folders as well.

Greets

Andreas
 
I guess one more alternative can't hurt -

set BEFORE=%CD%
pushd ..
set AFTER=%CD%
popd
call set DIFF=%%BEFORE:%AFTER%=%%
 
andreas said:
Wow!!

Many thanks for all the superb replies!!

I did indeed read about the %~nI option but did't realize that it works for
folders as well.
You're welcome.

I'd always use %~nxA. There are folder names containing dots being
interpreted as name and extension.
 
andreas kässens said:
Wow!!

Many thanks for all the superb replies!!

I did indeed read about the %~nI option but did't realize that it works for
folders as well.

That is the kind of thing you have to condition yourself to look for, as the
available documentation can sometimes be aimed more closely at the most
obvious uses. In short, since a folder is a type of file, one should always
assume that what works for a file *might* work for a folder.

/Al
 
Back
Top