使用子字符串查看字符串是否包含批处理文件中的子字符串的问题
problem using substring to see if a string contain the substring in a batch file
我是batch新手,发现了一个无法解决的问题。
批处理文件只是一个小脚本,使用 "youtube-dl"(用于下载 youtube 视频的命令行工具)更加实用(文件夹 "youtube-dl.exe" 在 PATH 中)
setlocal enableextensions enabledelayedexpansion
set /p url=url :
set /p qual="quality (360/720 = 1/0) : "
if %qual%==0 (
set qual=22
) else (
set qual=18
)
cd C:\Users\theo\Videos
if not %url:&list=%==%url% (
set /p v="video or playlist ? (V/P)"
if %v%==P (
youtube-dl.exe -f %qual% --yes-playlist "%url%"
) else (
youtube-dl.exe -f %qual% --no-playlist "%url%"
)
) else (
youtube-dl.exe -f %qual% --no-playlist "%url%"
)
endlocal
cmd /k
当到达第二个 if
语句时,cmd window 立即关闭,我只是想不出哪里出了问题!
编辑:Jeb 的解决方案有效 ^^
始终将您的变量包含在 IF 语句中。
出于两个原因,这是必要的。
它避免了语法错误,当变量为空时,包含空格、其他分隔符或特殊字符,如 &|<>...
。
它避免了变量修饰符语法的问题,例如 search/replace !var:search=replace!
或子字符串语法 !var:~<start>,<size>!
延迟扩展
你的第二个问题是当一个块在它被执行之前 parsed 时会发生百分比扩展。
因此 if %v%==P ...
永远不会扩展为输入的值 v
,而是可能扩展为空。
在块中(或更好,总是)使用延迟扩展。
if not "!url:&list=!" == "!url!" (
set /p v="video or playlist ? (V/P)"
if "!v!" == "P" (
youtube-dl.exe -f !qual! --yes-playlist "!url!"
) else (
...
我是batch新手,发现了一个无法解决的问题。 批处理文件只是一个小脚本,使用 "youtube-dl"(用于下载 youtube 视频的命令行工具)更加实用(文件夹 "youtube-dl.exe" 在 PATH 中)
setlocal enableextensions enabledelayedexpansion
set /p url=url :
set /p qual="quality (360/720 = 1/0) : "
if %qual%==0 (
set qual=22
) else (
set qual=18
)
cd C:\Users\theo\Videos
if not %url:&list=%==%url% (
set /p v="video or playlist ? (V/P)"
if %v%==P (
youtube-dl.exe -f %qual% --yes-playlist "%url%"
) else (
youtube-dl.exe -f %qual% --no-playlist "%url%"
)
) else (
youtube-dl.exe -f %qual% --no-playlist "%url%"
)
endlocal
cmd /k
当到达第二个 if
语句时,cmd window 立即关闭,我只是想不出哪里出了问题!
编辑:Jeb 的解决方案有效 ^^
始终将您的变量包含在 IF 语句中。
出于两个原因,这是必要的。
它避免了语法错误,当变量为空时,包含空格、其他分隔符或特殊字符,如
&|<>...
。它避免了变量修饰符语法的问题,例如 search/replace
!var:search=replace!
或子字符串语法!var:~<start>,<size>!
延迟扩展
你的第二个问题是当一个块在它被执行之前 parsed 时会发生百分比扩展。
因此 if %v%==P ...
永远不会扩展为输入的值 v
,而是可能扩展为空。
在块中(或更好,总是)使用延迟扩展。
if not "!url:&list=!" == "!url!" (
set /p v="video or playlist ? (V/P)"
if "!v!" == "P" (
youtube-dl.exe -f !qual! --yes-playlist "!url!"
) else (
...