批处理脚本编程 - 如何让用户从文件夹中的文件列表中选择按编号的文件? [英] Batch Script Programming -- How to allow a user to select a file by number from a list of files in a folder?

查看:524
本文介绍了批处理脚本编程 - 如何让用户从文件夹中的文件列表中选择按编号的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这N个文件的文件夹。我试图找出如何做到以下几点:

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.

推荐答案

以下批处理脚本应该做你想要什么,解释如下所示:

The following batch script should do what you want, the explanation follows 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

这一切的脚本首先使用类似于一个数组来存储文件名。此数组填充在 -loop。循环体为在当前目录中找到的每个文件名执行一次。

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.

该数组实际上由一组变量的所有开始文件的,并附加一个编号(如文件1 文件2 该号码被存储在变量首页,并在每个循环迭代增量,但在循环体数量和相应的文件名也被打印出来。

The array actually consists of a set of variables all starting with file and with a number appended (like file1, file2. The number is stored in the variable index and is incremented in each loop iteration. In the loop body that number and the corresponding file name are also printed out

在接下来的部分 SET / P 命令要求用户输入一个数字,然后存储在变量。第二个 SET 命令和下面的如果用于检查输入的号码将通过检查提供有效的数组索引为 FILEX 变量。

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

最后, RESOLVE 子程序用于由形成的变量文件中的内容复制 +中输入的号码来命名变量 FILE_NAME 然后可以用于进一步的处理。

Finally the RESOLVE subroutine is used to copy the content of the variable formed by file + the entered number in selection to a variable named file_name that can then be used for further processing.

希望给一些提示。

这篇关于批处理脚本编程 - 如何让用户从文件夹中的文件列表中选择按编号的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆