Open a command prompt window and run there cmd /?
. This command outputs the help for the Windows command processor. On last help page in last paragraph there is written which characters in a directory or file name or arguments of executables and scripts require the usage of double quotes: space and &()[]{}^=;!'+,`~
The character %
has a special meaning in batch files as it marks the beginning and end of an immediately expanded environment variable reference or a batch file argument reference or a loop variable reference. The percent sign must be escaped with one more %
to specify a literally interpreted %
character.
In command prompt window run set
and there are displayed the standard environment variables for the user account on the machine. One of those standard environment variables is USERPROFILE which holds the path to the current user's profile folder containing by default, for example, the subfolder Desktop
.
Now let us look on the following line from your batch file:
youtube-dl.exe --extract-audio --audio-format mp3 --output C:\Users\*******\Desktop\(ext)s.%(ext)s %audio%
It would be good to use here a reference to the environment variable USERPROFILE for the Desktop
directory. The user account name could contain a space character and therefore it is advisable to enclose the path in double quotes. Next there are parentheses and a single percent sign which definitely require double quotes and escaping the percent sign.
The URL stored in environment variable audio
can't contain a space character as in URLs a space character must be encoded with %20. But this single percent sign causes again troubles on interpreting the line by Windows command processor. The solution is using delayed expansion.
Let us look on this batch code:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ToolPath=%~dp0"
cd /D "%USERPROFILE%\Desktop"
rem The directory may not exist. It would be a good idea to check that.
:audio
cls
echo/
echo/
echo Your audio will be downloaded and saved as a .mp3 format
echo/
echo/
set "audio="
:PromptUser
set /P "audio=Enter audio URL here: "
if not defined audio goto PromptUser
set "audio=!audio:"=!"
if not defined audio goto PromptUser
"%ToolPath%youtube-dl.exe" --extract-audio --audio-format mp3 --output "%USERPROFILE%\Desktop\(ext)s.%%(ext)s" "!audio!"
pause
cls
echo/
echo/
echo/
echo/
echo Your audio has now been downloaded.
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 4 >nul
endlocal
The batch file first creates a local copy of all environment variables, enables command extensions and delayed variable expansion, and pushes also current working directory path on stack.
Next the path of the directory containing the batch file and the other executables used by this batch file is assigned to variable ToolPath
. Run in command prompt window call /?
for details on %~dp0
(drive and path of argument 0 – the batch file – always ending with a backslash).
Then the current directory is changed to the Desktop
directory of the currently used user account independent from which drive the batch file was started. Run in command prompt window cd /?
for details about this command and its options.
The line with youtube-dl.exe
is changed as now the executable is called with full path (as current working directory is now the user's Desktop
directory). Also the output directory is enclosed now in double quotes, uses also environment variable USERPROFILE, has escaped the single percent sign with one more %
and the URL is referenced now in double quotes using delayed expansion (exclamation marks instead of percent signs). Run in a command prompt window set /?
for help and details about delayed expansion.
For a timeout of 3 seconds the value used on command PING must be 4 as the first ping is always immediately successful.
The command ENDLOCAL results in discarding the local copy of the table with the environment variables (ToolPath
is not defined anymore after this line and all changes on other variables are lost), restores previous values for delayed expansion (most likely turning it off as not enabled by default) and command extensions (most likely being still enabled as by default) and also restoring previous working directory (most likely the path of the batch file if started with a double click).
See also: