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.
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
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
This kills the first part of the link because it removes everything in between and including exclamations. !dasasdasd!
all megalinks start with #!something!
To use megals output directly and avoid delayedexpansion (which removes the
!
)Findstr /n will do the numbering.
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 :
As compo advised, please edit your question to contain all necessary information - don't force others to gather it from unnecessary answer and comments.
This didn't work
I need output of Call Echo %%Item[%choice%]%% passed to Megadl
Just use an array:
For further details, see this answer.