-1

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.

Jim Jamil
  • 55
  • 8
  • Can you please [edit your question](https://stackoverflow.com/posts/49564553/edit) to include the code you have written or tried, _(this isn't a free code to order service)_. – Compo Mar 29 '18 at 20:40
  • Also please read [this guide](https://stackoverflow.com/help/mcve) on how to properly ask questions. – MarkovskI Mar 29 '18 at 20:46
  • Definitely take the [tour], read [Ask], and [MCVE]. – jwdonahue Mar 29 '18 at 21:16
  • Run `choice /?`, also search this site for existing answers to your menu problem. – jwdonahue Mar 29 '18 at 21:18
  • I added the code so far, I think my question is fairly clear now. – Jim Jamil Mar 29 '18 at 21:19
  • @Compo, look like the same concept as one of your previous [answers](https://stackoverflow.com/a/49482583/1417694) – Squashman Mar 29 '18 at 23:09
  • 1
    What about this to do the numbering: `find /N /V "" "list.txt"` or `findstr /N /R "^" "list.txt"`? – aschipfl Mar 30 '18 at 08:21
  • @jwdonahue, I'm afraid `choice` might be difficult to handle in case there are more than 9 list entries... – aschipfl Mar 30 '18 at 08:24
  • Would have to use the alphabet if you wanted to use choice. – Squashman Mar 30 '18 at 13:11
  • Are you now wanting to list the number and the filename, or just the filename? _Why ask somebody to type in a string when they could simply enter a number?_. So the end user will be given a listing of directories, and will type in their choice which will invoke the link. Do you want to list the filename as it appears or the filename only, _without the initial parent_? – Compo Mar 30 '18 at 19:50
  • The folders don't need to be numbered, they are entered as strings. Only the files in the folders need to be numbered. When the files are listed by `Megals`, /Root/dir is shown at the top of the file list, so it gets assigned the number 1 by the list, but number 1 should really be assigned to the first file instead. – Jim Jamil Mar 30 '18 at 20:43
  • The current code above outputs `Number Link /Path/Filename` Although `Number /Path/Filename Link` would be more readable. That might be tricky to do, and not totally necessary. – Jim Jamil Mar 30 '18 at 20:53
  • Would it not make more sense to run `MegaLs -n /Root/Dir` for the File selection then run `MegaLs -e /Root/Dir/File` to retrieve the Link after that? – Compo Mar 31 '18 at 20:03
  • `-n` doesn't return the file links, just the filenames. `%Link%` needs to be assigned the links numbered. – Jim Jamil Apr 01 '18 at 01:58
  • @JimJamil, I know, what it does, I read the pages you linked to! `-n` will return the file names only, which is what you need for your menu. Then you run `-e` to retrieve only the respective link using `/Root/typeddirectory/chosenfilename`. – Compo Apr 02 '18 at 11:23
  • Okay thanks, take a look at the updated code, seems to work, and it also numbers the file list correctly. File 1 is listed as 1 – Jim Jamil Apr 03 '18 at 04:21
  • Edit: the files are still numbered incorrectly, `megals -n` numbers them correctly File 1 is listed as 1. However `megals -e` in the background is still listing File 1 as 2 in the list, you just don't see it because only `-n` is echoed. – Jim Jamil Apr 03 '18 at 04:31
  • Any idea how to fix that? I tried `tokens=2 `and `skip=1` neither work for some reason. – Jim Jamil Apr 03 '18 at 04:37
  • **Fixed** by using `-n -e` – Jim Jamil Apr 03 '18 at 04:45
  • The program crashes if the filenames have ( ) type brackets. @Compo – Jim Jamil Apr 08 '18 at 21:30
  • Any ideas on the brackets issue? @LotPings – Jim Jamil Apr 13 '18 at 03:37

4 Answers4

1

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.

Aacini
  • 65,180
  • 12
  • 72
  • 108
1

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.

  • I still need to give the link to Megadl – Jim Jamil Mar 29 '18 at 23:37
  • See my enhanced answer. –  Mar 30 '18 at 10:10
  • I'll try to clean up the post a bit, sorry this is my first post here. Ideally it would be good to have the program prompt for the folder by name (string) instead of number, only the listed files within the folders need to use the number for input. Megals automatically lists the folders by name, so it would be more intuitive for the user to enter the folder name, get the list of files, and then ask for the file number. Cmd has no easy copy paste being the cause of all this lol. I'll take a look at your code here. – Jim Jamil Mar 30 '18 at 15:33
  • If you check mark `Quick-edit-mode` and `insert-mode` in system menu/ settings/options tab of your cmd window copy and paste will work (in recent win10 also Ctrl+V to paste) dragging with the mouse to mark and right-click to copy. You can also redirect output directly to the clipboard with i.e.`dir |clip.exe` –  Mar 30 '18 at 15:41
  • This is on a Win7 system, so cmd is still a bit awkward for that. Pipe to Clip is handy though. – Jim Jamil Mar 30 '18 at 15:50
  • I updated the first post with an ask folder by name method, as I mentioned it will list the files in the directory, but if there are 13 files, file 13 will be numbered as 14 by the list generator, this is because the Root/Directory gets listed as 1, because of how Megals lists the output. – Jim Jamil Mar 30 '18 at 16:24
0

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!

Jim Jamil
  • 55
  • 8
  • No need to use delayed expansion with the `var1` variable. – Squashman Mar 29 '18 at 23:04
  • @JimJamil, is the content of `megals -e /Root/directory`, exactly the same as your original `list.txt` file shows? If not, please edit your question to provide the new requirement and the content of that command. – Compo Mar 29 '18 at 23:35
  • It's a list of megalinks, one on each line https://megatools.megous.com/man/megatools.html#_tools_overview – Jim Jamil Mar 29 '18 at 23:45
  • @JimJamil, that doesn't show the output, I need to see it, if you require advice on parsing it for use in a menu and associated variable system. – Compo Mar 30 '18 at 00:30
  • Th output of Megals -e /Root/anime A list of links like https://mega.nz/#Link There's four blank columns before each link for some reason too. – Jim Jamil Mar 30 '18 at 00:40
  • Have you clicked on that link? where's it supposed to take me? Why could you not just run `megals -e /Root/` and paste the output, _as code_, into your question? – Compo Mar 30 '18 at 09:32
  • So you've revisited the question and edited it, but still haven't, corrected the link or provided the output of that command. _Your question has attracted so much more attention than most on here, we're trying to help you and you're making it difficult._ – Compo Mar 30 '18 at 16:41
  • I added the output to first post. – Jim Jamil Mar 30 '18 at 18:07
0

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
Jim Jamil
  • 55
  • 8