Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

windows - Create a numbered list based on a given list of strings

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.

So that it becomes

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.

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? " 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

Here below is an updated answer, thanks to LotPings.

With a small tweak to ask for the folder by string

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

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

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 number listing is fixed

Edit3 Parentheses in filenames crash the program i.e

1 File(1)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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%

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...