Dynamic Multi Level Menu

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
LouChar
Posts: 2
Joined: 02 Sep 2014 12:46

Dynamic Multi Level Menu

#1 Post by LouChar » 02 Sep 2014 13:13

Hopefully someone out there has an idea how to create a dynamic multi level menu.

I have a Folder structure approx. 3 levels deep. My current solution has been to hardcode these menu's, however my users add new install folders almost everyday, I'd prefer a solution which cuts me out of the loop. A dynamic menu seems ideal.

Our folder structures looks something like this:

Folder1
Folder1a
Folder1b
Folder1c
Folder1c1
Folder2
Folder2a
Folder2b
Folder2b1

and so on....

Essentially these folders are software install folders, I need to allow the users to navigate (via a DOS menu) to, say, 'Folder2b1'. Importantly the entire path with be included in the final command line, for instance:
%Folder2%\%Folder2b%\%Folder2b1%

I found an example script but I can't workout how to extend it further to include additional levels.

Sample script:
@echo off

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

set fname=%~n0
rem echo %fname%

cls
set count=1
for /D %%f in (Folder*) do (
SET Folder=%%f
echo.
echo !count! - %%f
set mis!count!=%%f
set /A count=!count!+1

)

set /A count=%count%-1
echo.
set /P input=Please select or 0 (zero) to exit [1-%count%]:



Any advice is appreciated.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Dynamic Multi Level Menu

#2 Post by Aacini » 02 Sep 2014 18:17

I am afraid your question is confusing. Do you want a multi level menu in order to execute options, or do you want to browse a directory tree in order to select a folder? Also, I don't know what "to navigate (via a DOS menu)" is.

Anyway, the code below may give you a starting point for your request:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Select a file browsing a directory tree
rem Antonio Perez Ayala

set pageSize=30

rem Load current directory contents
:ProcessThisDir
for /F "delims==" %%a in ('set name[ 2^>NUL') do set "%%a="
set numNames=0
for /D %%a in (*) do (
   set /A numNames+=1
   set "name[!numNames!]=<DIR>   %%a"
)
for %%a in (*.*) do (
   set /A numNames+=1
   set "name[!numNames!]=        %%a"
)

rem Show directory contents, one page at a time
set start=1
:ShowPage
if %start% equ 1 (
   set "less="
) else (
   set "less=-=Previous page, "
)
set /A end=start+pageSize-1
if %end% gtr %numNames% (
   set end=%numNames%
   set "more="
) else (
   set "more=+=Next page, "
)
cls
echo Directory: %CD%
echo/
for /L %%i in (%start%,1,%end%) do echo     %%i- !name[%%i]!
echo/
:GetOption
set "option="
set /P "option=Enter desired item (%less%%more%Nothing=..): "
if not defined option (
   cd ..
   goto ProcessThisDir
) else if "%option%" equ "-" (
   set /A start-=pageSize
   if !start! lss 1 set start=1
   goto ShowPage
) else if "%option%" equ "+" (
   if defined more set /A start+=pageSize
   goto ShowPage
) else if not defined name[%option%] (
   goto GetOption
) else if "!name[%option%]:~0,5!" equ "<DIR>" (
   cd "!name[%option%]:~8!"
   goto ProcessThisDir
)

rem Return selected file
cls
for %%a in ("!name[%option%]:~8!") do set "result=%%~Fa"
echo Result="%result%"


Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Dynamic Multi Level Menu

#3 Post by foxidrive » 02 Sep 2014 21:52

That's neat Antonio. It works well and is easy to navigate.

A couple of comments here -

1) The display truncated the top of the list in one folder:
I opened the folder BACKUP from the root which showed:
14 directories
Then up to file 30 - with a second page up to file 40

The issue is that the display at the top of the screen started with
3- <DIR> AllAct
and didn't show the ones before that.

2) It may be useful to set a larger cmd window using Mode, as fairly long filenames wrap to the next line.
Or it could be useful to truncate the displayed filename to the width of the screen.

foxi

LouChar
Posts: 2
Joined: 02 Sep 2014 12:46

Re: Dynamic Multi Level Menu

#4 Post by LouChar » 03 Sep 2014 09:57

Sorry for any confusion, but it seems you've answered my cry for help perfectly.

I'm very grateful for the help.

Post Reply