根据给定的字符串列表创建编号列表 [英] Create a numbered list based on a given list of strings

查看:64
本文介绍了根据给定的字符串列表创建编号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

windows cmd批处理

windows cmd batch

我有一个文本文件,如下所示:

I have a text file that looks like this:

Dog
Cat
Frog
Bat
Bird
Mouse

我想为每个字符串和每一行赋予一个数字.

I want to attribute a number to each string, each line.

这样就变成了

1 Dog
2 Cat
3 Frog
4 Bat
5 Bird
6 Mouse

然后我想请用户输入数字,然后将相应的字符串存储在变量中.

Then I want to ask the user to input a number, and then have the corresponding string stored in the variable.

因此,如果用户输入1,则该变量将设置为字符串Dog
因此,当用户输入1时,程序将存储/输出Dog

So if the user inputs 1, then the variable is set to the string Dog
So when the user inputs 1 the program stores/outputs Dog

set /p var1="number? ",因此var1成为字符串,而不是数字.

set /p var1="number? " so var1 becomes the string, not the number.

这完成了任务的第一部分,但是现在我需要第二部分,将字符串存储在变量中.

This does the first part of the task kinda, but now I need the second part, storing the strings in avariable.

@echo off
set TEXT_T="list.txt"

set /a c=0

setlocal ENABLEDELAYEDEXPANSION

FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
  set /a c=c+1

  echo !c! %%i 
)
endlocal
pause

感谢LotPings,以下是更新后的答案.

Here below is an updated answer, thanks to LotPings.

稍作调整即可按字符串查询文件夹

With a small tweak to ask for the folder by string

这提供了一种使用CMD中的Megatools的简便方法

This provides an easier way to use Megatools from CMD

https://megatools.megous.com/man/megals.html
https://github.com/megous/megatools

@echo off

:start:

megals /Root/

set /p var1="dir? " & megals /Root/%%var1%%

for /f "tokens=1,* delims=:" %%A in ('megals -n /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)

for /f "tokens=1,* delims=:" %%A in ('megals -n -e /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
)

set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%

set "trimmedlink="
for %%h in (%Link%) do if not defined trimmedlink set "trimmedlink=%%h"
Megadl %trimmedlink% && goto :start:

pause

编辑:必须将%Link%修剪为第一个单词,即仅链接

Edit: Had to trim %Link% to just the first word, i.e just the link

Megals -e /Root/misc的输出如下所示: 星号是文件的唯一链接ID

The output of Megals -e /Root/misc looks like this: The asterisk are the unique link ids for the files

                                                    /Root/misc
   https://mega.nz/#!********!********************* /Root/misc/File1
   https://mega.nz/#!********!********************* /Root/misc/File2
   https://mega.nz/#!********!********************* /Root/misc/File3

上面的批处理脚本看起来像:

With the batch script above it looks like:

1 File1
2 File2
3 File3
Select #:  <------input file number 

Edit2 号码列表是固定的

Edit3 文件名中的括号会使程序崩溃 即

Edit3 Parentheses in filenames crash the program i.e

1 File(1)

然后将所选文件号作为相应的链接传递给Magadl
Megadl Link

The chosen file number then gets passed to Magadl as the corresponding link
Megadl Link

批处理脚本允许您通过输入相应的文件号来下载链接,因此您不必在标准cmd窗口中键入长链接ID.

The batch script allows you to download the link by just entering the corresponding file number so you don't have to type out the long link id in a standard cmd window.

推荐答案

直接使用megals输出并避免延迟扩展(删除了!)
Findstr/n将进行编号.

To use megals output directly and avoid delayedexpansion (which removes the !)
Findstr /n will do the numbering.

@echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
  set Item[%%A]=%%B
  Echo %%A %%B
)
set /p choice=Select #:
Call Echo Choice:%%Item[%choice%]%%

使用具有双倍百分号的(伪)调用是一种实现延迟扩展而又不会出现!问题的老式方法.

Using a (pseudo-)call with doubled percent signs is an old fashioned method of realizing delayed expansion without the problem with the !.

在编程/脚本编制中,您需要调整技术以适应您的需求.

In programming/scripting you need to adapt techniques to fit your needs.

在不知道您的巨型工具的确切输出的情况下,
这可以完成工作:

Without knowing the exact output of your megatools,
this could do the job :

@echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
  set Folder[%%A]=%%B
  Echo %%A %%B
)
set /p choice=Select #:
Call Set Folder=%%Folder[%choice%]%%

for /f "tokens=1,* delims=:" %%A in ('megals -e %Folder% ^|findstr /n "." ') do (
  set Link[%%A]=%%B
  Echo %%A %%B
)
set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%

megadl %Link%

如compo建议,请编辑您的问题以包含所有必要的信息-不要强迫他人收集它来自不必要的答案和评论.

As compo advised, please edit your question to contain all necessary information - don't force others to gather it from unnecessary answer and comments.

这篇关于根据给定的字符串列表创建编号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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