0

I made a menu in batch that display all the .txt file in folder and assign each file a number. Based on this tutorial: Batch Script Programming -- How to allow a user to select a file by number from a list of files in a folder? - It works very well, but I want to make it possible to choose more then 1 file. Example, the user can choose for example 1,2,4 and in a new variable I will get all the files names of the first file that was listed, the second, and the fourth.

In addition the number of .txt file is getting changed so I don't know how many .txt files will be in the folder and I want to let the user choose as many as file he wants- 1 file or more. I need the files in 1 variable because I want after the user choose to use a 3rd part app that need all files names in 1 line so I want to write something like:

"Do this command with %all_files_from_batch_menu%"

I hope it was clear... Thanks!

Here is an example Lets say I have in the folder 5 files a.txt b.txt c.txt d.txt e.txt - I want the menu to show them and assign numbers to each file and if the user will choose 1,2,4 it will write him you choose a.txt b.txt e.txt

John Kens
  • 1,615
  • 2
  • 10
  • 28
Kami Kami
  • 3
  • 2
  • 2
    Show us the code you want us to help you with, _you've told us that you have some based on code in a previous post_. We expect you to post the code you've created which attempts, _but fails_, to perform the tasks you've laid out in your question. – Compo Dec 30 '18 at 22:10
  • Please provide your attempts at the code, I will be happy to help you work it out. Provide the errors you're having with the script. – John Kens Dec 30 '18 at 22:28
  • Possible duplicate of [Multiple choices menu on batch file?](https://stackoverflow.com/questions/14529246/multiple-choices-menu-on-batch-file) – Compo Dec 30 '18 at 22:50
  • Take the [tour], read [Ask] and [MCVE]. – jwdonahue Dec 31 '18 at 00:27

1 Answers1

1

You have not shown a single line of code. You just requested code, any code... This means to me that I can write any code I want. So here it is:

@echo off
setlocal EnableDelayedExpansion

rem Create array / show the list
cls
set "n=0"
for %%a in (*.txt) do (
   set /A n+=1
   set "file!n!=%%a"
   echo !n!- %%a
)

set /P "files=Enter desired files: "

rem Extract the files
set "result=!file%files:,=! !file%!"

echo %result%

Output example:

1- a.txt
2- b.txt
3- c.txt
4- d.txt
5- e.txt
Enter desired files: 1,2,4
a.txt b.txt d.txt

Note: the file number 4 is d.txt, not e.txt as you indicated in the question...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks Aacini its what I nedded and its working great! I need another little help, is it possible to add a text before all the files name in result? So the output will be the full path of the file? Its also ok for me to set a variable with the path but how i can make it to echo me the result as c:\myfolder\a.txt c:\myfolder\b.txt and so on.. or maybe even just like this %mypathvar%\a.txt %mypathvar%\b.txt ? – Kami Kami Jan 01 '19 at 21:39
  • If this answer was helpful to you, you should accept it and upvote it... – Aacini Jan 03 '19 at 15:54
  • I accepted it and voted. Can you pleasehelp with the second thing? I want to save the path of each txt file (they are all in the same folder) and then when printing the choosen files it will write the path variable before each txt file that i choosed – Kami Kami Jan 04 '19 at 07:10
  • Try: `set "result=!mypathvar!\!file%files:,=! !mypathvar!\!file%!"` – Aacini Jan 04 '19 at 11:41