This batch script automates the process of downloading content from...

September 3, 2025 at 02:09 AM

@echo off setlocal enableextensions enabledelayedexpansion rem grab 1st non-empty line of clipboard, trimmed for /f "usebackq delims=" %%i in ( `powershell -noprofile -c "$t=Get-Clipboard -Raw; $t=$t.Trim(); ($t -split '\r?\n') | ? { $_ -ne '' } | select -first 1"` ) do set "clipboard=%%i" if not defined clipboard ( echo clipboard empty & exit /b 1 ) rem robust substring test (won’t choke on & ? = etc) echo(!clipboard!| findstr /i "youtube" >nul && ( yt-dlp --add-header "Referer:!clipboard!" -norm "!clipboard!" ) || ( yt-dlp --add-header "Referer:!clipboard!" -P "E:\Stuff\Morestuff\Favourites\New" "!clipboard!" ) endlocal

This batch script automates the process of downloading content from a URL copied to the clipboard, depending on whether the content is from YouTube or some other source. Here's a detailed breakdown of what the code does:


  1. Initial Setup:
    • @echo off: Disables command echoing in the console to make the script cleaner during execution.
    • setlocal enableextensions enabledelayedexpansion: Enables advanced features like delayed variable expansion within the script.

  1. Fetch and Process the Clipboard Content:
    • A Powershell command is executed to:
      • Fetch the raw clipboard content (Get-Clipboard -Raw).
      • Trim any leading/trailing whitespace ($t.Trim()).
      • Split the clipboard content into lines ($t -split '\r?\n') and filter out empty ones (? { $_ -ne '' }).
      • Select the first non-empty line (select -first 1).
    • This processed content is assigned to the variable clipboard using a for loop.

  1. Check if Clipboard is Empty:
    • If the clipboard variable is not defined (indicating the clipboard was empty or contained only whitespace), the script outputs clipboard empty and exits with an error code (exit /b 1).

  1. Detect and Handle YouTube Links:
    • The script performs a case-insensitive substring test by checking whether the clipboard content contains the word "youtube" (using findstr /i "youtube").
    • If "youtube" is found in the clipboard URL:
      • The yt-dlp tool (youtube-dl alternative for downloading from YouTube) is executed with the specified parameters:
        • --add-header "Referer:!clipboard!": Adds the clipboard URL as the HTTP referer.
        • -norm: Normalizes metadata or video titles.
      • The clipboard URL is passed as the target for yt-dlp.

  1. Handle Other URLs:
    • If "youtube" is not found in the clipboard URL:
      • yt-dlp is executed with similar parameters but with an added -P option to specify a download directory:
        • -P "E:\Stuff\Morestuff\Favourites\New": Downloads content to the specified directory.

  1. Cleanup:
    • endlocal: Ends the local environment settings for the batch script.

In Summary:

  • The script retrieves the first non-empty, trimmed line from the clipboard.
  • If the clipboard is empty, it terminates with an error.
  • If the URL contains "youtube", it downloads content using yt-dlp with one set of options.
  • For other URLs, it downloads content using yt-dlp with a different set of options (storing files in a specific directory).
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node