Package Script Programming. How to allow a user to select a file by number from a list of files in a folder?

I have a folder with N files. I am trying to figure out how to do the following:

Display a list of files with numbers next to them to select:

01 - FileA.pdf 02 - FileB.pdf 03 - FileC.pdf ... 

Then indicate to the user which file he wants to use by entering the corresponding number. I do not know where to start from this.

+4
source share
1 answer

The following batch of script should do what you want, explanation below:

 @ECHO OFF SET index=1 SETLOCAL ENABLEDELAYEDEXPANSION FOR %%f IN (*.*) DO ( SET file!index!=%%f ECHO !index! - %%f SET /A index=!index!+1 ) SETLOCAL DISABLEDELAYEDEXPANSION SET /P selection="select file by number:" SET file%selection% >nul 2>&1 IF ERRORLEVEL 1 ( ECHO invalid number selected EXIT /B 1 ) CALL :RESOLVE %%file%selection%%% ECHO selected file name: %file_name% GOTO :EOF :RESOLVE SET file_name=%1 GOTO :EOF 

First of all, this script uses something like an array to store file names. This array is populated in FOR -loop. The loop body is executed once for each file name found in the current directory.

An array actually consists of a set of variables, starting with file and with an added number (for example, file1 , file2 ). The number is stored in the variable index and increases in each iteration of the loop. In the loop body, this number and the corresponding file name are also printed

In the next part, the SET /P command asks the user to enter a number, which is then stored in the selection variable. The second SET command and the next IF are used to check whether the entered number will give a valid array index by checking for the fileX variable.

Finally, the RESOLVE subroutine is used to copy the contents of the variable formed by file +, the number entered in selection into a variable called file_name , which can then be used for further processing.

Hope that gives some clues.

+10
source

Source: https://habr.com/ru/post/1391667/


All Articles