Create a numbered list based on a given list of st

2019-09-19 18:04发布

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.

4条回答
We Are One
2楼-- · 2019-09-19 18:43

What about making the output of a command into a list, instead of a text file into a list?

so the line with var1 here would be turned into a numbered list (the files listed in that directory), and Var2 is the number the users enters to create the returned string that gets passed to a command. i.e
https://megatools.megous.com/man/megals.html

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
(megals -e /Root/) <--------list all folders in root
(set /p var1="dir? " && megals -e /Root/!var1!) <-- select folder + list files
(set /p var2="Megalink? " && Megadl !var2!) <--- enter the file name for download
ENDLOCAL
pause

I want to be able to enter a number for the download, instead of the long file name. So this way the user can enter the number that corresponds to the link that they want to download. So if they enter 1 then the program does Megadl Dog

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

megals -e /Root/

set /p var1="dir? "

megals -e /Root/!var1! > rlist.txt

set TEXT_T="rlist.txt"

set /a c=0

FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i 
set string[!c!]=%%i
)

set /P number=Enter number:
Megadl !string[%number%]!
Endlocal
pause

This kills the first part of the link because it removes everything in between and including exclamations. !dasasdasd!
all megalinks start with #!something!

查看更多
地球回转人心会变
3楼-- · 2019-09-19 18:47

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.

查看更多
放我归山
4楼-- · 2019-09-19 18:48

This didn't work

I need output of Call Echo %%Item[%choice%]%% passed to Megadl

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

for /F "tokens=*" (`%%Item[%choice%]%%`) do (
set "var1=%%A"
Megadl !Var1!
)
pause
查看更多
三岁会撩人
5楼-- · 2019-09-19 18:50

Just use an array:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set TEXT_T="list.txt"

set /a c=0

FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
  set /a c=c+1
  echo !c! %%i 
  set string[!c!]=%%i
)

set /P number=Enter number:
echo !string[%number%]!

pause

For further details, see this answer.

查看更多
登录 后发表回答