So the user does not input an IP address as your prompt text set in advance, but an URL which can contain %
, &
, ?
, |
and other characters with a special meaning for the Windows command processor cmd.exe
processing a batch file.
I suggest to use the following batch file:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:UserPrompt
set "InputURL="
set /P "InputURL=Enter the URL: "
if not defined InputURL goto UserPrompt
set "InputURL=%InputURL:"=%"
if not defined InputURL goto UserPrompt
setlocal EnableDelayedExpansion
if not "!InputURL:~-1!" == "/" set "InputURL=!InputURL!/"
set "InputURL=!InputURL!master_1.m3u8|user-agent=KAIOS/2.0"
echo Link 1 : !InputURL!
endlocal
endlocal
echo/
pause
The environment variable InputURL
is undefined explicitly before prompting the user.
The user can press just key RETURN or ENTER without entering anything at all. In this case the environment variable InputURL
is still not defined as explicitly undefined before prompting the user. It would keep its current value if it would be defined before prompting the user.
The first IF condition just checks if the user typed a string at all and prompts the user again if nothing was input by the user.
Next all "
are removed from the string input by the user. This action could result in execution of set "InputURL="
if the user entered just one or more "
and nothing else and therefore the environment variable InputURL
would be once again not defined anymore. For that reason the user is prompted once more on having just input one or more "
intentionally or by mistake.
The double quote left to variable name InputURL
and the double quote at end are very important here. It makes sure that the first (and only) argument string of command set
is enclosed in double quotes. For that reason the Windows command processor interprets the characters like &
and |
inside the double quoted argument string as literal characters and not as operators.
Then delayed environment variable expansion is enabled which pushes the current states of command extensions (enabled) and delayed expansion (disabled) as well as the current directory and all environment variables on stack before enabling delayed expansion. All further commands are executed with a new set of environment variables until endlocal
is executed by cmd.exe
.
The usage of delayed expansion makes it possible to safely process the user input string further as the user input string does not anymore modify the command lines of the batch file before execution as it is done on using %InputURL%
without modifying the file content of the batch file.
It is checked next in this temporary new environment if the user input string does not end with a /
in which case the slash is appended to user input string.
Next is appended the fixed string containing the redirection operator |
which is interpreted here as literal character because of being inside a double quoted argument string. So there is no need to escape the operator with ^
as it would be necessary in case of argument string of command set
is not enclosed in double quotes exactly as done here with "
left to variable name. A "
between equal sign and string value assigned to the variable would not work here. For the reason see for example
How to set environment variables with spaces?
The command echo
is used to output the modified URL with additional text using delayed expansion to be able to output the string without the usage of "
because of echo
would output also the double quotes.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The previous environment is restored with endlocal
which does not only result in disabling delayed expansion as it was before setlocal EnableDelayedExpansion
, but in restoring also state of command extensions (not modified), the current directory (not modified) and the previous list of environment variables with their values. This means the environment variable InputURL
has again the string value without the one or two modifications made between setlocal EnableDelayedExpansion
and endlocal
.
The last endlocal
just explicitly restores the environment before starting this batch file.
Last the user is prompted for pressing any key to see the output and leave batch file processing because of reaching end of the batch file.
See also: