1

(Updates and solution below)

I have a batch file that uses youtube-dl to get information from a url then sends it to Internet Download Manager. As a batch file it works perfectly. It parses the URL then sends it to IDM with the video's name and extension.

For example:

The url: https://www.youtube.com/watch?v=kbBgx0BEuuI

Is proccessed, then opens an IDM GUI download confirmation window with the set folder and name, in this case "c:\users\username\downloads\It's sodium chloride.mp4"

Then I just need to hit "start download".

But after I convert it to exe (using Advanced BAT to EXE Converter v4.11) it messes up the filename and shows up like this:

"c:\users\downloads\videoplayback.mp4"

I'll add the script and explain it. (FIY, all of the programs I use in the script have their path in the PATH environment variable and are set to run as admin)

script with explanation in brackets [this line does this]:

@echo off
pushd "%~dp0" [sets folder as current folder]
for /f %%i in ('powershell -command "Get-Clipboard"') do set link=%%i [sends youtube page url (like the one I linked above) from clipboard to the "link" variable]
youtube-dl -g -f best %link% | clip [copies direct download url to clipboard]
for /f %%i in ('powershell -command "Get-Clipboard"') do set url=%%i [sends direct download url from clipboard to "url" variable]
youtube-dl -f best --get-filename -o "%(title)s.%(ext)s" "%link%" | clip [copies the video's name and extension to the clipboard]
for /f %%i in ('powershell -command "Get-Clipboard"') do set filename=%%i [sends the filename from clipboard to the "filename" variable, so now the filename variable is, in this case "It's sodium chloride.mp4"]
set folder="c:\user\<username>\downloads" [sets the folder variable to my chosen download folder]
IDMan /d "%url%"  /p %folder% /f "%filename%" [sends all of the previous information to IDM as direct url to download, save folder, and filename(including extension)]

To reiterate, with the .bat file and running the commands on cmd - they both work perfectly, but once it's converted to .exe and run, the filename becomes "videoplayback" which would be the video title if it's taken from the direct download url, and not from the normal youtube url that the script takes it from in the beggining (link=regular youtube link, url=direct video file url)

Here's the script again without the brackets so you can copy and test it if you want to (just note the username in the folder variable):

@echo off
pushd "%~dp0"
for /f %%i in ('powershell -command "Get-Clipboard"') do set link=%%i
youtube-dl -g -f best %link% | clip
for /f %%i in ('powershell -command "Get-Clipboard"') do set url=%%i
youtube-dl -f best --get-filename -o "%(title)s.%(ext)s" "%link%" | clip
for /f %%i in ('powershell -command "Get-Clipboard"') do set filename=%%i
set folder="c:\user\<username>\downloads"
IDMan /d "%url%"  /p %folder% /f "%filename%"

Update: For some reason the batch file itself started having the problem as well so I was able to run it with echo on and figured out the problem:

In the following line:

youtube-dl -f best --get-filename -o "%(title)s.%(ext)s" "%link%" | clip

It registers "%(title)s.%" as one variable that doesn't exist, and replaces it with nothing, which causes this command to send only part of the information. I seperated them into 2 different lines and variables "filename" and "extension" but it still doesn't work.... hopefully I'll solve it soon and close this question.

1 Answer 1

1

Apparently the problem was actually a really simple one that I should've noticed earlier. In batch files when you use '%' before anything (for example "%i") compared to using it before and after (when calling a variable - "%variable%"); You actually have to use double percent because when the script actually runs it makes one disappear (in batch "%%i" will turn into "%i" when running). so in the following line and other lines like it:

youtube-dl -f best --get-filename -o "%(title)s" "%link%" | clip

The '%' before "(title)s" disappears when the script runs and leaves the entire string "%(title)s" blank, so the line actually runs like this:

youtube-dl -f best --get-filename -o "%link%" | clip

So I just added another '%' before all iterations that had only one '%' and now it finally works, for example- the final, working, version of the example line I gave above:

youtube-dl -f best --get-filename -o "%%(title)s" "%link%" | clip

I could've figured it out sooner because I copied line by line to cmd and removed one '%' from lines that had "%%i" so it'll run properly, but I completely missed it in the other places because they are parameters for youtube-dl and not batch so I didn't think about it. Case closed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .