Solved How to find sub-dir names for 'n' level down from CurrentDir

October 15, 2014 at 15:33:48
Specs: Windows 7
Current directory/PATH = ... \main-dir\sub1\sub2\...\subN

Need to find the path from 'main-dir' to 'subN'. 'main-dir' will become a root directory (for DOSbox runs). So then the current dir. changes to \sub1\sub2\...\subN . Need this PATH variable.

I have several Apps that run in DOSbox and each will have a different PATH in DOSbox.

At present One App works okay for 2 levels max. with following code:

 for %%* in (.) do set CurrDirName=%%~n*
 echo  current dir = %CurrDirName%
 if /I %CurrDirName%==%1 goto gotit

 for %%* in (..) do set BaseDirName=%%~n*
 echo base dir = %BaseDirName%
 if /I %BaseDirName%==%1 goto gotit

 set CurrDirName=%BaseDirName%\%CurrDirName%
 for %%* in (..\..\) do set BaseDirName=%%~n*
 echo base dir = %BaseDirName%
 if /I %BaseDirName%==%1 goto gotit

2. What does '%%~n*' in script mean? Tried this in a more general way and it failed. It doesn't start in my 'subN' directory ... it starts in my 'base-dir'. hmmm?

Thanks, Phil


See More: How to find sub-dir names for n level down from CurrentDir

Report •

✔ Best Answer
October 23, 2014 at 20:28:51
See resp#1, which was SUMMARILY REJECTED for no known reason. I'm not gonna try and fix that other guys code. We think different. Here's a slightly modified re-iteration of #1 to handle spaces-in-filenames:

@echo off & setlocal
::target root-dir.name is commandline
set base=%*
set string=%cd%
call :xx "%string%"
echo FINAL RESULT:"%base%\%qq%"
goto :eof

:xx
echo ONE:%1]
for /f "tokens=1* delims=\" %%a in (%1) do (
set qq=%%b
if /i "%%a" neq "%base%" (call :xx "%%b") else (goto :eof)
)

::===== end. this worked in my tests

message edited by nbrane



#1
October 15, 2014 at 18:15:20
I made this to search the path for a target base-name:

@echo off & setlocal
::target root-dir.name is in var 'base', in this case it's just "test"
set base=test
:: assuming a "back-trace" from the current working directory
set string=%cd%
call :xx "%string%"
echo %base%/%qq%
goto :eof

:xx
for /f "tokens=1* delims=/" %%a in (%1) do (
set qq=%%b
if /i "%%a" neq "%base%" (call :xx "%%b") else (goto :eof)
)
::------ end script. A nagging feeling this script was way overly complicated...

Not sure if this is what you were aiming at.
%%~n
The tilde (~) means "strip quotes off the string". The n means give the name only and not the drive, path or extension. (See: For /?) The asterisk is unusual. Normally it's just a alphabet, like %%a, or %%~a, or %%~na


Report •

#2
October 17, 2014 at 16:16:52
This just gave me the base (which I already gave it). I'm trying to get the path from where I am (... \BASE\sub1\sub2\...\subN) in the tree Down to the Base (\sub1\sub2\...\subN).

Thanks!


Report •

#3
October 19, 2014 at 19:09:17
Oh! I think... But I was confused as to where 'N' camre from, so I assumed you wanted to go backwards till you hit a specific directory name. Otherwise, you have to supply 'N' for this to work. It just traces back 'N' levels:
:: ======= begin test-script
@echo off & setlocal enabledelayedexpansion
set N=
set /p N=levels to descend:
if not defined N set n=3
set k=.
for /L %%z in (1 1 %N%) do (
for %%a in (!k!) do set z=%%~na\!z!
set k=!k!\..
)
echo %z%
::====== end

message edited by nbrane


Report •

Related Solutions

#4
October 21, 2014 at 09:04:46
Found following code to solve my problem:

 set mask=.
:loop
 for %%* in (%mask%) do set CurrDirName=%%~n*
	if /I %CurrDirName%X==%1X goto gotit
	if %UserDirName%X == X (
		set UserDirName=%CurrDirName%
		set mask=..
	) else (
		set UserDirName=%CurrDirName%\%UserDirName%
		set mask=..\%mask%
	)
	goto Loop

:gotit
 echo UserDir = %UserDirName%
 set od-User= %UserDirName%

Thanks for your ideas ... Phil


Report •

#5
October 23, 2014 at 06:48:52
Above 'solution' not working in all cases. One directory tested had a period in its name and that got truncated; 'match.hz' came back as 'match'. Tried changing below from a '%%~n' to a '%%~nx' and that aborted.
 for %%* in (%mask%) do set CurrDirName=%%~n*
... changed above to the below                            |
 for %%* in (%mask%) do set CurrDirName=%%~nx*

Need to be able to handle paths with or without a period. Any ideas?

Report •

#6
October 23, 2014 at 20:28:51
✔ Best Answer
See resp#1, which was SUMMARILY REJECTED for no known reason. I'm not gonna try and fix that other guys code. We think different. Here's a slightly modified re-iteration of #1 to handle spaces-in-filenames:

@echo off & setlocal
::target root-dir.name is commandline
set base=%*
set string=%cd%
call :xx "%string%"
echo FINAL RESULT:"%base%\%qq%"
goto :eof

:xx
echo ONE:%1]
for /f "tokens=1* delims=\" %%a in (%1) do (
set qq=%%b
if /i "%%a" neq "%base%" (call :xx "%%b") else (goto :eof)
)

::===== end. this worked in my tests

message edited by nbrane


Report •

#7
October 24, 2014 at 09:26:28
Got it working!!!

Thanks, Phil

message edited by OptimalDesigns


Report •

Ask Question