最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

windows - Can't seem to pass the parameter correctly on batch script - Stack Overflow

programmeradmin2浏览0评论

I was trying to use yt-dlp to download some mp3 files ... for research.
I wanted to wrap the whole command line prompt into a batch script, so the only thing I would input would be the link, but I can't seem to get it to pass the parameter through it, even though I can echo it. Any help would be greatly appreciated.
It works if I type into the command line prompt manually.

@echo off 

echo Argument 1: %1

yt-dlp -x --audio-format mp3 --audio-quality 0 -o "%(title)s.mp3" %1

Output

mp3.bat 
Argument 1: 

Usage: yt-dlp [OPTIONS] URL [URL...]

yt-dlp: error: You must provide at least one URL.
Type yt-dlp --help to see a list of all options.

I was trying to use yt-dlp to download some mp3 files ... for research.
I wanted to wrap the whole command line prompt into a batch script, so the only thing I would input would be the link, but I can't seem to get it to pass the parameter through it, even though I can echo it. Any help would be greatly appreciated.
It works if I type into the command line prompt manually.

@echo off 

echo Argument 1: %1

yt-dlp -x --audio-format mp3 --audio-quality 0 -o "%(title)s.mp3" %1

Output

mp3.bat https://www.youtube/watch?v=Dq3V-KdKslU
Argument 1: https://www.youtube/watch?v

Usage: yt-dlp [OPTIONS] URL [URL...]

yt-dlp: error: You must provide at least one URL.
Type yt-dlp --help to see a list of all options.
Share Improve this question asked Jan 30 at 7:33 Michael LMichael L 394 bronze badges 3
  • 1 You need to double the percent sign in "%(title)s.mp3", so you get yt-dlp -x --audio-format mp3 --audio-quality 0 -o "%%(title)s.mp3" %1 – jeb Commented Jan 30 at 7:35
  • Thank you so much. It appears that I'm not too adept at Batch Script syntax. – Michael L Commented Jan 30 at 7:46
  • 2 Yo may have more luck with quoting your equals-containing string with rabbits' ears, then using %~1 to remove the quotes – Magoo Commented Jan 30 at 7:49
Add a comment  | 

2 Answers 2

Reset to default 2

= is treated as a delimiter and the rest of the url is in %2. You may try quoting the parameter to the batch file or if that is not possible, %* contains all the parameters.

Everything that is not a batch variable nor parameter needs its % doubled to escape it.

yt-dlp -x --audio-format mp3 --audio-quality 0 -o "%%(title)s.mp3" %*

No, you did not echo the entire %1. As I can see, =Dq3V-KdKslU is stripped.

The issue is the character =. To avoid stripping, you can enter this argument in quotation marks. Also, you can use %~1 instead of %1:

> mp3.bat "https://www.youtube/watch?v=Dq3V-KdKslU"

Then the entire argument string will be passed. In your echo, you will see

Argument 1: https://www.youtube/watch?v=Dq3V-KdKslU

Still, you can use %1 in the batch file if you want to include quotation marks. With %1, you will get

Argument 1: "https://www.youtube/watch?v=Dq3V-KdKslU"

This value will be correctly passed to yt-dlp and elsewhere.

发布评论

评论列表(0)

  1. 暂无评论