Search code examples
filebatch-filecmddirectorywindows-xp-sp3

Choosing folders with numbers in batch


ok so from the title you might have an idea as to what im trying to accomplish.

What I want to do is [ tree ] and show a list of folders ( hence the tree command ) and then allow me to select a folder by using numbers, the catch is that I need to be able to do so without already knowing the folder's name's

Ex. [ THIS IS WHAT I DONT WANT IT TO DO ]

cd C:\windows
tree
set input=
set /p input=Choose:
if %input%== Folder 1 goto :B
if %input%== Folder 2 goto :C
etc.

So i need it to be able to tree, set each folder as a variable and then allow me to choose that as a number some how?

Please help!


Solution

  • Try this Batch file, I think it is an adequate solution for this problem:

    @echo off
    
    echo Select a folder. Terminate folder number with + to open it.
    echo/
    call :SelectFolder selectedFolder
    echo/
    echo Selected folder: %selectedFolder%
    goto :EOF
    
    
    :SelectFolder returnVar
    setlocal EnableDelayedExpansion
    :nextFolder
       echo/
       echo %cd%
       set i=0
       set folder[0]=..
       echo     0- ..
       for /D %%d in (*) do (
          set /A i+=1
          set folder[!i!]=%%d
          echo     !i!- %%d
       )
       :getOption
       set option=0+
       set openFolder=1
       set /P "option=Enter the desired folder: "
       if "%option:~-1%" equ "+" (
          set option=%option:~0,-1%
       ) else (
          set openFolder=
       )
       if %option% gtr %i% goto getOption
       cd "!folder[%option%]!"
    if defined openFolder goto nextFolder
    endlocal & set %1=%cd%
    exit /B