0
set vid1=asd  
set vid2=fgh
for /L %%1 in (1,1,2) do (echo %vid%%1%)
pause

doesn't work in my batch file, help

What I need is that cmd resolves first that "%%1" is 1, 2 or whatever; and then resolve %vid1%.

.....................................................................

I think this is a bigger problem, of "variable inside a variable", because this also is not working:

set asd=1  
set vid1=qwe

echo %vid%asd%%
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Try `CALL echo %%vid%%1%%`. As a side note, I wouldn't advise using numbers for the `FOR` variable as it can easily get confused with arguments passed to the script. – Squashman Aug 11 '20 at 15:04
  • Thanks but it didn't work. It echoes literally "%vid1%", but not "asd". And yes, thanks, this is just a prototype to explain the problem. – Debrune e brune Aug 11 '20 at 15:11
  • @Debruneebrune: Squashmans's suggestion works fine for me. It echoes both `asd` and `fgh`. Did you forget the `call` command? – Stephan Aug 11 '20 at 15:16
  • Oh, you're great sorry, yes it worked, thank you so much. I had forgotten the call. – Debrune e brune Aug 11 '20 at 15:19
  • See [here](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) for some explanation of what happens and why. – Stephan Aug 11 '20 at 15:20
  • thanks again. I have been seeing delayed expansion threads, but i guess i didn't understand them. – Debrune e brune Aug 11 '20 at 15:23
  • yes, it's a queer concept (I'm not aware of any other language that does this), but once you grasped the concept, it starts to make sense (not saying you'll never forget to use it correctly) – Stephan Aug 11 '20 at 15:27
  • Do you feel the link I gave you solves your issue/explains enough for you to understand and correct the issue? If yes, we'll close this question as a duplicate. – Stephan Aug 11 '20 at 15:30
  • Sorry, um... I'll read it later sorry. If closing as duplicate is erasing it, I think this could still be useful for other people, to tell them "it's that page, go see it". But idk. I'm finishing the script I was building. – Debrune e brune Aug 11 '20 at 16:03
  • Closing the question does not delete it, we close it with a link to the other other for people to follow and determine how to resolve their issue. – Gerhard Aug 11 '20 at 16:33
  • Ok, I'll see in a second – Debrune e brune Aug 11 '20 at 16:50
  • I want to add: You answered very quick, when I didn't really think I may get an answer, so thanks. – Debrune e brune Aug 11 '20 at 17:19

1 Answers1

1

I should have used delayedexpansion:

setlocal EnableDelayedExpansion

set vid1=asd
set vid2=qwe

for /L %%i in (1,1,3) do (echo !vid%%i!)

pause

It can also be done without delayedexpansion:

for /L %%1 in (1,1,%count%) do (call :yt "%%vid%%1%%" %%1)
pause
exit

:yt
echo video %2
"C:\[path]\youtube-dl.exe" [youtube-dl arguments] %1
echo.
goto:eof

Full script (it's bloated because I was trying if I could do things):

@echo off
set count=0
:beh
set /a count+=1
set /p vid%count%=vid%count%: 
if not defined vid%count% (
set /a count-=1
goto next
) else (
if %count%==1 (echo %count% vid) else (echo %count% vids)
goto beh)
pause

:next
echo %count% videos
pause >nul
choice /m "x?"
IF %ERRORLEVEL% EQU 1 (set x=-x)
echo press enter to run ytdl
pause >nul
echo initiating
echo.
@cd %homepath%/desktop
for /L %%1 in (1,1,%count%) do (call :yt "%%vid%%1%%" %%1)
pause
exit

:yt
echo video %2
"C:\[path]\youtube-dl.exe" %x% --no-check-certificate -w --console-title -i --no-mark-watched -o ".\%%(title)s.%%(ext)s" --prefer-ffmpeg --ffmpeg-location "C:\[ffmpeg path]" %1
echo.
goto:eof
Gerhard
  • 22,678
  • 7
  • 27
  • 43