Determine parent folder name without entire folder path?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Determine parent folder name without entire folder path?

#1 Post by djangofan » 04 Nov 2011 12:37

I'm trying to find the best way to determine parent folder name without entire folder path. (This needs to be a tip on the main dostips page btw) I came up with this method. Can it be improved at all?

Code: Select all

@echo off
:: script uses ',' and '#' as special chars
:: script wont work if any folder name has a special char in it
SET CDIR=%~p0
SET CDIR=%CDIR:~1,-1%
SET CDIR=%CDIR:\=,%
SET CDIR=%CDIR: =#%
FOR %%a IN (%CDIR%) DO SET "CNAME=%%a"
ECHO Current directory path: %CDIR%
SET CNAME=%CNAME:#= %
ECHO Current directory name: %CNAME%
pause

aGerman
Expert
Posts: 3887
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Determine parent folder name without entire folder path?

#2 Post by aGerman » 04 Nov 2011 13:21

djangofan wrote:(This needs to be a tip on the main dostips page btw)

Why do you think it's so important :?: :?

Code: Select all

@echo off
call :getFolderName "%~dp0" FolderName
echo "%FolderName%"
pause
goto :eof

:getFolderName
setlocal
set "name=%~1"
if "%name:~-1%"=="\" (
  endlocal
  call :getFolderName "%name:~,-1%" "%~2"
  goto :eof
)
endlocal
set "%~2=%~nx1"
goto :eof


BTW Certainly some special characters (like %) wont work. I'm currently too busy to test it...

Regards
aGerman

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Determine parent folder name without entire folder path?

#3 Post by !k » 04 Nov 2011 13:42

Code: Select all

for %%i in ("%~dp0.") do echo ParentFolderName = "%%~nxi"

djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Re: Determine parent folder name without entire folder path?

#4 Post by djangofan » 04 Nov 2011 14:26

!k wrote:

Code: Select all

for %%i in ("%~dp0.") do echo ParentFolderName = "%%~nxi"


I couldn't get it to work as you wrote but I got it to work with this:

Code: Select all

SET CDIR=%~dp0
SET _CDIR=%CDIR:~1,-1%
for %%i in ("%_CDIR%") do SET ParentFolderName=%%~nxi

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Determine parent folder name without entire folder path?

#5 Post by !k » 04 Nov 2011 14:46

it works fine on XP.
what`s your OS?

aGerman
Expert
Posts: 3887
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Determine parent folder name without entire folder path?

#6 Post by aGerman » 04 Nov 2011 16:30

!k, your loop works also on Win7.
djangofan, would you tell us the full path where it doesn't work. Perhaps we could figure out why it fails.

Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Determine parent folder name without entire folder path?

#7 Post by Ed Dyreen » 05 Nov 2011 00:09

'
I thought I found a golden egg, but unfortunately it doesn't work if the last path is missing a slash.
At this point the result is faulty as DOS assumes a path must have an ending slash.
This is a problem as paths don't have to have an ending slash to be a valid path :(

Code: Select all

@echo off
for %%r in (

   "C:\This\Works\This Works.TMP"
   "C:\This\Works\"
   "C:\This\Works"
   "C:\This\"
   "C:\"

) do    for %%r in ("%%~dpr.") do echo ParentFolderName = "%%~nxr"
pause
exit

ParentFolderName = "Works"
ParentFolderName = "Works"
ParentFolderName = "This" whoops !
ParentFolderName = "This"
ParentFolderName = ""
Druk op een toets om door te gaan. . .

And even 'This Works.TMP' could be a path, there really is no way of being sure.

dbenham
Expert
Posts: 2383
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Determine parent folder name without entire folder path?

#8 Post by dbenham » 05 Nov 2011 08:09

Ed Dyreen wrote:And even 'This Works.TMP' could be a path, there really is no way of being sure.

Try this:

Code: Select all

>nul 2>&1 dir %mypath%\ && (echo %mypath% is a directory path) || (>nul 2>&1 dir %mypath% && (echo %mypath% is a file path) || (echo %mypath% is an invalid path))

Ideally the contents of mypath should be quoted (or add the quotes to the code above and make sure mypath contents are not quoted)

Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Determine parent folder name without entire folder path?

#9 Post by Ed Dyreen » 05 Nov 2011 08:48

'
That's not what I mean, you assume the path exists, I'm just saying there is no way of determining the difference between a path or a file with a 100% certainty unless the user always ends the path with a slash which often is not the case.

Code: Select all

if $string == $Path ( md "!$string!" ) else >"!$string!" type nul
"C:\This\Works\This Works.TMP" -> Is it a path or a file ?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Determine parent folder name without entire folder path?

#10 Post by !k » 05 Nov 2011 09:07

Code: Select all

@echo off &setlocal enabledelayedexpansion
for %%r in (

   "C:\This\Works\This Works.TMP"
   "C:\This\Works\"
   "C:\This\Works"
   "C:\This\"
   "C:\"

) do (pushd %%r 2>nul &&(for %%r in ("!cd!") do echo ParentFolderName = "%%~nxr" &popd) ||for %%r in ("%%~dpr.") do echo ParentFolderName = "%%~nxr")
pause




no way of determining the difference between a path or a file with a 100% certainty unless the user always ends the path with a slash
Yes. So I always ends the path with a slash, even if it does not necessarily

jeb
Expert
Posts: 967
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Determine parent folder name without entire folder path?

#11 Post by jeb » 07 Nov 2011 03:03

One more problem ... :)

If the path contains trailing spaces the %~nx expansion fails always,
as it removes the spaces from the name.

jeb

dbenham
Expert
Posts: 2383
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Determine parent folder name without entire folder path?

#12 Post by dbenham » 07 Nov 2011 10:58

jeb wrote:One more problem ... :)

If the path contains trailing spaces the %~nx expansion fails always,
as it removes the spaces from the name.

How is that a problem :?:
I'm not able to force Windows to create a file or directory containing trailing spaces in the name, so the behavior you cite seems proper and not a problem to me :?

Dave Benham

djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Re: Determine parent folder name without entire folder path?

#13 Post by djangofan » 07 Nov 2011 12:55

Thanks guys. This was really helpful in understanding the issue:

Code: Select all

@echo off &setlocal enabledelayedexpansion
ECHO This directory is: %CD%
for %%r in (
   "%CD%"
   "C:\This1\Works1\This.Works1\"
   "C:\This2\Works2\This_Works2\"
   "C:\This3\Works3\This Works3\"
   "C:\This4\Works4\This Works4"
   "C:\This5\Works5"
   "C:\This6\"
   "C:\"
) do (pushd %%r 2>nul &&(for %%r in ("!cd!") do echo ParentFolderName = "%%~nxr" &popd) ||for %%r in ("%%~dpr.") do echo ParentFolderName = "%%~nxr")
pause


It explains why I had to do this to trim the trailing space, in order for it to work:

Code: Select all

SET CDIR=%~dp0
SET _CDIR=%CDIR:~1,-1%

jeb
Expert
Posts: 967
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Determine parent folder name without entire folder path?

#14 Post by jeb » 07 Nov 2011 15:20

dbenham wrote:jeb wrote:
One more problem ... :)

If the path contains trailing spaces the %~nx expansion fails always,
as it removes the spaces from the name.

How is that a problem :?:
I'm not able to force Windows to create a file or directory containing trailing spaces in the name, so the behavior you cite seems proper and not a problem to me :?


I'm suprised :shock:
I supposed a space is legal at any position, but you are right with normal dos/windows tools it seems not possible to
create a file or directory with trailing spaces, leading spaces are allowed.

jeb

dbenham
Expert
Posts: 2383
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Determine parent folder name without entire folder path?

#15 Post by dbenham » 07 Nov 2011 16:19

Another surprise:

Trailing dots (.) are also trimmed from file and directory names :!:

Dave Benham

Post Reply