@echo off
setlocal EnableExtensions
title YTD Batch Song Downloader

set "ROOT=C:\YTD"
set "LIST=%ROOT%\SongsToGet.txt"
set "OUTPUT=%ROOT%\Songs"
set "YTDLP=%ROOT%\yt-dlp.exe"
set "FFMPEG=%ROOT%\FFMPEG\bin"

if not exist "%YTDLP%" (
    echo ERROR: yt-dlp.exe was not found at:
    echo %YTDLP%
    echo.
    pause
    exit /b 1
)

if not exist "%FFMPEG%\ffmpeg.exe" (
    echo ERROR: FFmpeg was not found at:
    echo %FFMPEG%\ffmpeg.exe
    echo.
    pause
    exit /b 1
)

if not exist "%LIST%" (
    echo ERROR: SongsToGet.txt was not found at:
    echo %LIST%
    echo.
    pause
    exit /b 1
)

if not exist "%OUTPUT%" mkdir "%OUTPUT%"

echo.
echo Starting batch song downloads...
echo.

set "FOUND="
for /F "usebackq delims=" %%A in ("%LIST%") do (
    if not "%%A"=="" (
        set "FOUND=1"
        echo ------------------------------------------------------------
        echo Searching for: %%A
        echo ------------------------------------------------------------
        "%YTDLP%" "ytsearch1:%%A" ^
            --no-playlist ^
            -x ^
            --audio-format mp3 ^
            --audio-quality 5 ^
            --add-metadata ^
            --ffmpeg-location "%FFMPEG%" ^
            -o "%OUTPUT%\%%(title)s.%%(ext)s"
        echo.
    )
)

if not defined FOUND (
    echo No songs were found in SongsToGet.txt.
    echo Add one song per line, save the file, and try again.
    echo.
    pause
    exit /b 1
)

echo.
echo All batch song downloads are complete.
echo Files were saved to:
echo %OUTPUT%
echo.
pause
