aria2批量代码调用功能无法正常工作

问题描述 投票:0回答:1

这是我为 torrent 下载的多功能选项编写的代码。 1 是直接 torrent [工作正常],但其他两个没有按照我的预期工作。如果我尝试使用选项 2 或 3,它只会调用或执行 :d.torrent 而不是 :d.magnet_link 或 :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

这是上面的完整代码。使大部分工作自动化。 (如果你问我的话,这真的很有帮助)

我尝试的另一件事是 goto 函数,但它不起作用。以及更改代码的完整结构、更改调用函数等。 我期待的是

if "%choice%"=="2" (
    call :select_download_dir "Magnet"
    call :interval
    call :d.magnet_link

if "%choice%"=="3" (
    call :select_download_dir "Specific File"
    call :interval
    call :selector

在调用 :interval 之后,它不应该调用或转到 :d.torrent [每次都会去]。 如果有人知道要修复什么或我哪里出错了,请指出。修复它将会有很大帮助。

batch-file aria2
1个回答
0
投票

Batch 没有“部分”、“函数”、“过程”或“段落”的概念。标签只是一个参考点。到达标签时,执行不会停止,它只是逐行继续执行,直到到达文件末尾、CALLGOTOEXIT

因此,

set "intervals[8]=3600"

set "interval=!intervals[%summary_interval_choice%]!"

:d.torrent

设置

:d.torrent
后,继续
interval

解决方案:插入

goto :eof

之后

set "interval=!intervals[%summary_interval_choice%]!"

到达物理文件末尾,终止

call

请考虑

choice
作为您的菜单选择 - 它是为此任务而设计的。使用搜索工具查找
[batch-file] choice
,例如。 Gerhard 的示例 或从提示中查看文档 -
choice /?

© www.soinside.com 2019 - 2024. All rights reserved.