"if contains" with forbidden special characters

58 views Asked by At

I'm making a YouTube downloader. A bug has come up with playlists and I want it to check if the inputed link is a playlist. This is done by checking for the presence of list. So, I copied my normal if statement and ran it, but it didn't like this:

if "%link:list=%" == "%link%" goto Playlist

I think the three special characters affecting it that are used in Youtube Playlist links are ? and & and =

ChatGPT kept trying to use either of the first two examples below. The first one I can't use as I rely on %ERRORLEVEL, and log the errors. The second one gave the same error codes as my original attempt.

First version:

echo %link%| findstr /C:"list=" >nul 2>&1
if %ERRORLEVEL% equ 0 goto Playlist

Second version:

if not x%test_link:list=%==x%link% goto Playlist

This third version made me give up on ChatGPT, so now I'm here!

REM Check if 'list=' is part of the string 
for /F "tokens=1,2 delims=&" %%A in ("%test_link%") do (
    if "%%B" neq "" (
        if "%%A" neq "%%Alist=" (
            set "playlist=1"
        )
    )
)
2

There are 2 answers

0
T3RR0R On BEST ANSWER

you were close with your first approach, you just got the logic a little wrong. the substition removes list if it is present in the variable link, meaning the condition will not be true if the string list is present. therefore:

If defined link If /i Not "%link:list=%" == "%link%" goto:Playlist Would be the correct logic. To safely expand user input, it is advisable to perform such comparisons with Setlocal enabledelayedexpansion active and using: If defined link If /i Not "!link:list=!" == "!link!" goto:Playlist

0
Mofi On

The AI of ChatGPT has not learned from code written by the best batch file code writers. The produced code examples are not working for that reason for your task and are all not good for any user input string validation done with the Windows Command Processor cmd.exe during processing of a batch file.

Here is a working batch code example for the task with explaining comments (remarks).

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem The user is prompted until entering something.
:UserPrompt
set "YouTubeUrl="
set /P "YouTubeUrl=Please enter or drag & drop the YouTube playlist url: " || goto UserPrompt
rem Remove all double quotes from user input string.
set "YouTubeUrl=%YouTubeUrl:"=%"
rem The environment variable YouTubeUrl is not defined anymore on user
rem has input before by mistake just one or more double quote characters.
if not defined YouTubeUrl goto UserPrompt
rem The url string must contain the string "list" which is verified by
rem comparing the url string with all occurrences of case-insensitive
rem found "list" in the string replaced by an empty string with the
rem unmodified url string. The string "list" is missing in the url
rem string if the two compared strings are identical.
if "%YouTubeUrl:list=%" == "%YouTubeUrl%" echo That is not a valid playlist url!& goto UserPrompt
rem The value of the environment variable YouTubeUrl can be used here in
rem further command lines before the command endlocal is finally executed.
endlocal

The remarks – the lines beginning with the command rem – can be removed from the the batch file for faster execution with less file system accesses. The batch file would look without the comment lines:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
:UserPrompt
set "YouTubeUrl="
set /P "YouTubeUrl=Please enter or drag & drop the YouTube playlist url: " || goto UserPrompt
set "YouTubeUrl=%YouTubeUrl:"=%"
if not defined YouTubeUrl goto UserPrompt
if "%YouTubeUrl:list=%" == "%YouTubeUrl%" echo That is not a valid playlist url!& goto UserPrompt
rem ... command lines using "%YouTubeUrl%" ...
endlocal

For a full explanation of this code example see my answer on How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? and single line with multiple commands using Windows batch file for an explanation of the operators || and &.