批处理脚本编程--如何允许用户从文件夹中的文件列表中按编号选择文件?
我有一个文件夹,里面有N个文件。我试图弄清楚如何执行以下操作:
显示文件列表,旁边带有数字以供选择:
01 - FileA.pdf
02 - FileB.pdf
03 - FileC.pdf
...
然后,让用户通过输入相应的数字来选择他想要使用的文件。我不知道从哪里开始。
I have a folder with N files in it. I'm trying to figure out how to do the following:
Display a list of the files with numbers next to them for selection:
01 - FileA.pdf
02 - FileB.pdf
03 - FileC.pdf
...
Then, have the user select which file he wants to use by typing in the corresponding number. I have no idea where to even begin with this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下批处理脚本应该执行您想要的操作,解释如下:
首先,该脚本使用类似数组的内容来存储文件名。该数组填充在
FOR
循环中。对于当前目录中找到的每个文件名,循环体都会执行一次。该数组实际上由一组变量组成,所有变量均以
file
开头,并附加一个数字(如file1
、file2
)。该数字存储在变量index
并在每次循环迭代中递增。在循环体中,还会打印出该数字和相应的文件名。在下一部分中,
SET /P
命令会询问。用户输入一个数字,然后将其存储在变量中selection
第二个SET
命令和后面的IF
用于通过检查SET
命令来检查输入的数字是否给出有效的数组索引。 。最后使用
RESOLVE
子例程复制file
+selection 到名为
file_name
的变量,然后可以用于进一步处理。希望能给一些提示。
The following batch script should do what you want, the explanation follows below:
First of all this script uses something like an array to store the file names. This array is filled in the
FOR
-loop. The loop body is executed once for each file name found in the current directory.The array actually consists of a set of variables all starting with
file
and with a number appended (likefile1
,file2
. The number is stored in the variableindex
and is incremented in each loop iteration. In the loop body that number and the corresponding file name are also printed outIn the next part the
SET /P
command asks the user to enter a number which is then stored in the variableselection
. The secondSET
command and the followingIF
are used to check if the entered number will give a valid array index by checking for afileX
variable.Finally the
RESOLVE
subroutine is used to copy the content of the variable formed byfile
+ the entered number inselection
to a variable namedfile_name
that can then be used for further processing.Hope that gives some hints.