0

I have a batch file for downloading youtube videos as 01-title1.m4a, 02-title2.m4a etc. I'm having trouble getting the video number n to work properly, though. If it's a single digit number I want to append a zero in front, but I can't get it to work.

If I change the IF line in the FOR to IF %n%==%n:~0,1% SET n=0!n!, it does work, but then only the initial value of n is used in the comparison. Yet when I do echo !n!==!n:~0,1! just before the IF, I get 1==1, 2==2 as expected.

:: Enter YouTube link including https, or just the video ID
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET url_list=
:loop
SET url=
SET /P url="Enter URL: "
SET url="%url%"
IF %url%=="" GOTO endloop
IF %url%=="%url:~1,11%" (SET url=%url:~1,11%) ELSE SET url=%url:~33,11%
SET url_list=%url_list% %url%
GOTO loop
:endloop
SET url_list=%url_list:~1%
SET /P n="Enter n: "
IF [%n%] == [] SET n=1
FOR %%u IN (%url_list%) DO (
    IF !n!==!n:~0,1! SET n=0!n!
    youtube-dl --output "D:\Users\Joakim\Downloads\!n!-%%(title)s.%%(ext)s" --extract-audio --format 18 "https://www.youtube.com/watch?v=%%u"
    SET /A n+=1
)
H.v.M.
  • 1,348
  • 3
  • 16
  • 42
  • The problem is you are setting `n` to an octal number by preceding it with a zero. You can't add 1 to 08 and 09. Just do a substring instead. – Squashman Sep 22 '17 at 21:32

1 Answers1

1
....
set /a zn=100+n
youtube-dl --output "D:\Users\Joakim\Downloads\!zn:~-2!-%%(title)s.%%(ext)s" --extract-audio --format 18 "https://www.youtube.com/watch?v=%%u"
....

set /a uses the run-time values of its arguments to set its target variable, so zn is set to the run-time value of n +100. Then use the last 2 characters of zn in the youtube-dl line.

Magoo
  • 77,302
  • 8
  • 62
  • 84