Link to home
Start Free TrialLog in
Avatar of Ryan Bennett
Ryan BennettFlag for United States of America

asked on

Batch file to get folder structure into a dynamic menu structure...

Hello,

what I would like is a batch file that will get the folder structure of the drive into a menu structure so that you can select the folder you want to go into, and once you select a folder from the menu, the subfolders of that folder would then be listed in a menu as well, and so on and so forth as deep into the folder structure as you want to drill down to. And in each menu would have a "backup current folder level" and a "list all contents of current folder" and if possible include a "go to previous folder" option.

 Basically I would like to run the batch file and be given a menu of the folders only of the c drive, then be given an option to either backup the current folder, list the files of the current folder in case I wanna to only backup one file from that folder, or select a subfolder and then be given the same options with the addition of the go to previous folder option for subfolders.

 I've tried and tried to write something like this but I'm not a strong batch script writer and just can't get anywhere with it.

 Thank you very much in advance for your help,
Ryan
Avatar of it_saige
it_saige
Flag of United States of America image

You would probably be better off writing something in powershell.  That being said, is this purely for learning purposes because Windows already has an Explorer.

-saige-
if you run
 dir /s /ad /b x:\ > AllDirsOnDrive.txt

Open in new window


You should get a list of all directories (and only directories) on the drive output to a text file AllDirsOnDrive.txt (no need for batch file).

Where x is the drive letter you want the list for.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ryan Bennett

ASKER

I guess I don't explain myself very well sometimes, I'm sorry.

 I have a backup script that I use to backup the user profiles on a windows machine which lists the user profiles into a menu to choose from. Many times I find that I've miss a folder or a file here and there that is outside the user's profile and have to go back to the machine and manually grab the missed data. So I just wanted to see if I could add an option for that into the script that I have that would allow me to grab the folders or files that I want to select.

 I realize that Windows has an Explorer, but after me trying to figure out how to do it on my own and failing I really wanted to see if it was possible and so I thought I'd reach out for some help. Isn't that what this site is all about? It use to be.
Don't get me wrong.  This site still is very much about helping people but sometimes without more context (which you have provided in your follow-up) it's hard to gauge whether someone is trying to re-invent the wheel or solve a problem.  In any case, as I had stated, Powershell would probably be a better alternative.  You have full .NET support which open up a whole new world of potential solutions.

-saige-
Another thing to keep in mind is that the question asked isn't always the right one.  And the answers sometimes provided - especially by experienced professionals - may be completely different from what you expected because we have an understanding of the technologies and best practices in areas you may not.  

I read your question to indicate you wanted to create a menu out of a directory structure.  I assumed you were going to pull in a text file with a list of folders and parse them in some program you wanted to use - hence my suggestion.

But sometimes, while you may have asked for a batch file, it may not be the best solution and others who know this may provide alternate, what they perceive as better solutions.  When you participate in the dialog (as you are doing), we may discover that the "better" solutions we propose aren't feasible for one reason or another.  But until we know that, we may not focus on exactly what you asked for.  COMMUNICATION is key.
Thanks Bill I like that option looks like that might be a good alternative to what I am trying to accomplish, I'll play with it and see what I can come up with.

saige and Lee, I appreciate what you  guys are saying and will try to be as thorough as I can and keep an open mind to your suggestions. And I apologize for whipping out the pouty pants card so fast....

What I have been trying in my script is to create an array by first passing the root of the system drive to the label that builds the array:
set folder=%1
set options=
set count=0
set "choice_options="
for /F "delims=" %%A in ('dir /a:d /b %folder%') do (
    set /a count+=1
    set "options[!count!]=%%A"
    set choice_options=!choice_options!!count!
)

Open in new window


and then spitting the menu out...
for /L %%A in (1,1,!count!) do echo  [%%A]. !options[%%A]!.

Open in new window


I get the first level of folders fine, but if I try to re-use the array from another function or label to drill down lower I get an empty menu or garbage.

maybe it is a pipe dream and I need to try something else, I'm open to powershell. I've been trying to get into it a little but fall back into what little I know of batch scripting so I haven't much experience with it at all.
Thanks Bill, this will get me what I am needing.
Avatar of Bill Prew
Bill Prew

Welcome, glad that was helpful.


»bp