The following creates a numbered list of links from a remote directory like
1 Link!1
2 Link!2
3 Link!3
4 Link!4
5 Link!5
.
@echo off
setlocal ENABLEDELAYEDEXPANSION
megals --reload /Root/
set /p var1="enter folder name: " & megals /Root/var1
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (`megals -n -e /Root/%%var1%%`) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
echo !string[%number%]!
pause
First Problem: All the links contain a !
character which gets removed by delayedexpansion, rendering the link useless. The links require the !
as it is part of the link.
Second Problem: I'm trying to integrate this into a program, and I can't use findstr
because it will list the link and filename on the same line, and when the filenames contain parentheses the program crashes. So I have to use usebackq because it lets me get just the link, without needing to deal with the filenames.
Findstr
will list Link!1 Filename
(the whole line)
Usebackq
lets me just get Link!1
I can't use Findstr
because when filenames contain parentheses the program will crash, which can only be solved by delayedexpansion.
This is a follow-up post from here, which I got stuck on: (Shows the Program)
https://stackoverflow.com/questions/49564553/create-a-numbered-list-based-on-a-given-list-of-strings#=
You can see the findstr method there, and how it causes crashes when filenames contain parentheses, which can be fixed with delayedexpansion, but that removes the !
character which is essential as it is part of the link.
Edit: Seems to be working now, thanks
Working Code
@echo off
:start:
megals --reload /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
)
setlocal DisABLEDELAYEDEXPANSION
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (`megals -n -e /Root/%%var1%%`) do (
set /a c+=1
call set "string[%%c%%]=%%i"
)
set /P number="Enter number: "
FOR /F "tokens=*" %%g IN ('call echo %%string[%number%]%%') do (SET VAR2=%%g)
echo %Var2%
echo.
Megadl %VAR2% & echo. && goto :start:
pause