I am a beginner in batch and I have found a problem that I can't resolve. the batch file is just a little script to make using "youtube-dl" (a command-lines tool for downloading youtube videos) more practical (the folder "youtube-dl.exe" is in is in 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
the cmd window close instantly when the second if
statement is reached and I just can't figure what is wrong !
EDIT: the solution of Jeb worked ^^
Always enclose your variables in IF statements.
That's necessary for two reasons.
It avoids syntax errors, when the variable is empty, contains spaces, other delimiters or special characters like &|<>...
.
It avoids problems with variable modifier syntax, like search/replace !var:search=replace!
or substring syntax !var:~<start>,<size>!
Delayed expansion
Your second problem is that percent expansion occurs when a block is parsed before it is even executed.
Therefor if %v%==P ...
never expands to the value of the entered v
, instead it was expanded probably to nothing.
In blocks (or better, always) use delayed expansion.
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 (
...