Page 1 of 2
Determine parent folder name without entire folder path?
Posted: 04 Nov 2011 12:37
by djangofan
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
Re: Determine parent folder name without entire folder path?
Posted: 04 Nov 2011 13:21
by aGerman
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
Re: Determine parent folder name without entire folder path?
Posted: 04 Nov 2011 13:42
by !k
Code: Select all
for %%i in ("%~dp0.") do echo ParentFolderName = "%%~nxi"
Re: Determine parent folder name without entire folder path?
Posted: 04 Nov 2011 14:26
by djangofan
!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
Re: Determine parent folder name without entire folder path?
Posted: 04 Nov 2011 14:46
by !k
it works fine on XP.
what`s your OS?
Re: Determine parent folder name without entire folder path?
Posted: 04 Nov 2011 16:30
by aGerman
!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
Re: Determine parent folder name without entire folder path?
Posted: 05 Nov 2011 00:09
by Ed Dyreen
'
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.
Re: Determine parent folder name without entire folder path?
Posted: 05 Nov 2011 08:09
by dbenham
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
Re: Determine parent folder name without entire folder path?
Posted: 05 Nov 2011 08:48
by Ed Dyreen
'
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 ?
Re: Determine parent folder name without entire folder path?
Posted: 05 Nov 2011 09:07
by !k
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
Re: Determine parent folder name without entire folder path?
Posted: 07 Nov 2011 03:03
by jeb
One more problem ...
If the path contains trailing spaces the %~nx expansion fails always,
as it removes the spaces from the name.
jeb
Re: Determine parent folder name without entire folder path?
Posted: 07 Nov 2011 10:58
by dbenham
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
Re: Determine parent folder name without entire folder path?
Posted: 07 Nov 2011 12:55
by djangofan
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%
Re: Determine parent folder name without entire folder path?
Posted: 07 Nov 2011 15:20
by jeb
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
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
Re: Determine parent folder name without entire folder path?
Posted: 07 Nov 2011 16:19
by dbenham
Another surprise:
Trailing dots (.) are also trimmed from file and directory names
Dave Benham