0

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!

cmd
  • 563
  • 3
  • 10
  • 26
  • I could do this in "pure batch" but for some reasons I will not spend one little minute in this project. Furthermore, use Google and you will find complete "batch-tree" solutions already in the net. – Endoro May 14 '13 at 06:07

2 Answers2

2

Run this batch file - it allows you to pick a folder and then returns the folder path in a variable.

@if (@CodeSection == @Batch) @then

@echo off
echo Select a folder:
pause

for /F "delims=" %%a in ('CScript //nologo //E:JScript "%~F0"') do (
   set Destination_Folder=%%a
)
echo "%Destination_Folder%"
pause>nul
:EXIT
exit

@end

// Creates a dialog box that enables the user to select a folder and display it.
var title = "Select a folder", rootFolder = 0;
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, title, 0, rootFolder);
WScript.Stdout.WriteLine(folder ? folder.self.path : "");
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Thank you this does work but is there a way to emulate the entire process in a batch. @foxidrive – cmd May 14 '13 at 04:40
  • There is no general method - you could have 200 folders in the list and then have to cater for multiple pages. Without specific details of the task a working solution can't be created simply. – foxidrive May 14 '13 at 15:28
2

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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • How can I now have the command line stay open and run `cd %selected%`? So, I don't want to enter the `+` at the end because I want to run a different command afterwards. – Spurious May 29 '17 at 06:19