批处理脚本编程--如何允许用户从文件夹中的文件列表中按编号选择文件?

发布于 2024-12-27 14:03:09 字数 186 浏览 3 评论 0原文

我有一个文件夹,里面有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2025-01-03 14:03:09

以下批处理脚本应该执行您想要的操作,解释如下:

@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

首先,该脚本使用类似数组的内容来存储文件名。该数组填充在 FOR 循环中。对于当前目录中找到的每个文件名,循环体都会执行一次。

该数组实际上由一组变量组成,所有变量均以 file 开头,并附加一个数字(如 file1file2)。该数字存储在变量 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:

@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 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 (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

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.

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.

Hope that gives some hints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文