this is the code i wrote for multi-function option for torrent downloads. 1 is direct torrent [which works fine] but other two is not working how I am expecting. if i try to use option 2 or 3, it just call or execute the :d.torrent instead of :d.magnet_link or :selector.
@echo off
setlocal enabledelayedexpansion
title Torrent Downloader using Aria2c-Alpha_Test
::=========================type selection and download managements==========================
::main
echo.
echo [33mFile showing dosen't work for Magnet Links, Only for .torrent files.[0m
echo.
echo Choose an option:
echo 1. Torrent Downloader
echo 2. Magnet Link Downloader
echo 3. Specific File Downloader [Torrent Only]
echo 4. Exit
set /p choice="Enter your Choice (1-4): "
if "%choice%"=="1" (
call :select_download_dir "Torrent"
call :interval
call :d.torrent
) else if "%choice%"=="2" (
call :select_download_dir "Magnet"
call :interval
call :d.magnet_link
) else if "%choice%"=="3" (
call :select_download_dir "Specific File"
call :interval
call :selector
) else if "%choice%"=="4" (
exit
) else (
echo Invalid choice.
timeout /t 5 /nobreak >nul
exit
)
::================Download Selection variables===================
:select_download_dir
set "download_dir="
echo [97mPlease wait.. Download Directory Selection will begin shortly. [0m
for /f "delims=" %%a in ('powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.Title = 'Select Download Directory'; $openFileDialog.CheckFileExists = $false; $openFileDialog.FileName = 'Select'; $openFileDialog.Filter = 'Folders|*.none'; $openFileDialog.ValidateNames = $false; $openFileDialog.ShowReadOnly = $false; $result = $openFileDialog.ShowDialog(); if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $selectedFolder = [System.IO.Path]::GetDirectoryName($openFileDialog.FileName); Write-Output $selectedFolder } else { Write-Output 'No folder or drive selected' }} "') do set "download_dir=%%~a"
if defined download_dir (
set "download_dir=!download_dir:"=\"!"
) else (
echo No folder selected
pause
)
cls
echo Selected Download Directory: %download_dir%
echo.
::==================Ask for summary interval choice==========================
:interval
timeout /t 1 /nobreak >nul
echo.
echo Choose Summary Interval:
echo 0. Silent
echo 1. Default (1 Minute)
echo 2. 5 minutes
echo 3. 10 minutes
echo 4. 15 minutes
echo 5. 20 minutes
echo 6. 25 minutes
echo 7. 30 minutes
echo 8. 60 minutes
echo [33mDefault is 1 Minute.[0m
set /p "summary_interval_choice=Enter your Choice[0-8]: "
rem Check if the input is a valid number between 0 and 8
set "valid_choice="
for %%a in (0 1 2 3 4 5 6 7 8) do (
if "%summary_interval_choice%"=="%%a" set "valid_choice=true"
)
rem If input is not valid, prompt the user again with an error message
if not defined valid_choice (
echo Invalid input. Please enter a number between 0 and 8.
choice /C YN /T 15 /D Y /M "Do you want to try again (Y/N)? Press Y to retry, or N to exit."
if errorlevel 2 exit /b 1
goto interval
)
rem Map choice number to interval value
set "intervals[0]=0"
set "intervals[1]=60"
set "intervals[2]=300"
set "intervals[3]=600"
set "intervals[4]=900"
set "intervals[5]=1200"
set "intervals[6]=1500"
set "intervals[7]=1800"
set "intervals[8]=3600"
set "interval=!intervals[%summary_interval_choice%]!"
::============================Torrent Downloader Variables============================
:d.torrent
set "torrent="
for /f "delims=" %%a in ('powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.Title = 'Select a Torrent File'; $openFileDialog.CheckFileExists = $true; $openFileDialog.FileName = 'Select'; $openFileDialog.Filter = 'Torrent files (*.torrent)|*.torrent|All files (*.*)|*.*'; $openFileDialog.ValidateNames = $true; $openFileDialog.ShowReadOnly = $false; $result = $openFileDialog.ShowDialog(); if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $selectedFile = $openFileDialog.FileName; Write-Output ('"' + $selectedFile + '"') } else { Write-Output 'No torrent selected' }} "') do set "torrent=%%~a"
cls
echo Selected Download Directory: %download_dir%
echo.
echo Selected interval: !interval! seconds
echo.
aria2c.exe --show-files "%torrent%"
timeout /t 2 /nobreak >nul
call :countdown "Download will begin in"
aria2c.exe --dir="%download_dir%" --console-log-level=warn --auto-file-renaming=false --seed-time=0 --seed-ratio=0.0 --summary-interval=!interval! "%torrent%"
if %errorlevel% neq 0 (
echo [31mDownload failed.[0m
) else (
echo [32mDownload Completed.[0m
)
pause >nul
exit
::============================Magnet Link Downloader Variables============================
:d.magnet_link
echo.
set /p magnet_link="Paste the Magnet Link: "
timeout /t 2 /nobreak >nul
cls
echo Selected Download Directory: %download_dir%
echo.
echo Selected interval: !interval! seconds
timeout /t 1 /nobreak >nul
call :countdown "Download will begin in"
aria2c.exe --dir="%download_dir%" --console-log-level=warn --auto-file-renaming=false --seed-time=0 --seed-ratio=0.0 --summary-interval=%interval% "%magnet_link%"
if %errorlevel% neq 0 (
echo [31mDownload failed.[0m
) else (
echo [32mDownload Completed.[0m
)
pause >nul
exit
::==================================Selector downloader variables===============================
:selector
echo.
set "s.torrent="
for /f "delims=" %%a in ('powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.Title = 'Select a Torrent File'; $openFileDialog.CheckFileExists = $true; $openFileDialog.FileName = 'Select'; $openFileDialog.Filter = 'Torrent files (*.torrent)|*.torrent|All files (*.*)|*.*'; $openFileDialog.ValidateNames = $true; $openFileDialog.ShowReadOnly = $false; $result = $openFileDialog.ShowDialog(); if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $selectedFile = $openFileDialog.FileName; Write-Output $selectedFile } else { Write-Output 'No torrent selected' }} "') do set "s.torrent=%%a"
cls
echo Selected Download Directory: %download_dir%
echo.
echo Selected interval: !interval! seconds
aria2c.exe --show-files "%s.torrent%"
echo.
::Ask for file selection
set /p selection="Enter the indices of files to Download [e.g-1,3,5-7]: "
timeout /t 2 /nobreak >nul
call :countdown "Download will begin in"
aria2c.exe --dir="%download_dir%" --console-log-level=warn --auto-file-renaming=false --seed-time=0 --seed-ratio=0.0 --summary-interval=%interval% --select-file="%selection%" "%s.torrent%"
if %errorlevel% neq 0 (
echo [31mDownload failed.[0m
) else (
echo [32mDownload Completed.[0m
)
pause >nul
exit
:: Function to display message with countdown
:countdown
set "message=%~1"
for /l %%i in (3,-1,1) do (
echo %message% %%i...
timeout /nobreak /t 1 >nul
)
endlocal
here is the full code above. which automate most of works. (this is really helpful if you aks me)
another thing i tried is goto function but it didn't work. as well as changing the full structure of the code, changing calling function etc.. what i'm expecting is
if "%choice%"=="2" (
call :select_download_dir "Magnet"
call :interval
call :d.magnet_link
or
if "%choice%"=="3" (
call :select_download_dir "Specific File"
call :interval
call :selector
after the call :interval it should not call or goto :d.torrent [which it goes every time.] if any one have any idea what to fix or where did i go wrong, please point it out. fixing it would be a big help.
Batch has no concept of "sections", "functions", "procedures" or "paragraphs". A label is simply a reference point. Execution does not stop when a label is reached, it simply continues through, line by line, until it reaches end-of-file, a CALL , a GOTO or an EXIT
Hence,
set "intervals[8]=3600"
set "interval=!intervals[%summary_interval_choice%]!"
:d.torrent
proceeds to :d.torrent
having set interval
.
Solution: insert
goto :eof
after
set "interval=!intervals[%summary_interval_choice%]!"
which goes to the physical end-of-file, terminating the call
Please consider choice
for your menu-selection - it is designed for this task. Use the search facility for [batch-file] choice
eg. Gerhard's example or see the documentation - choice /?
from the prompt.