【问题标题】:Windows Batch: How to use GET return values to append only valid software download links to a text file?Windows Batch:如何使用 GET 返回值仅将有效的软件下载链接附加到文本文件?
【发布时间】:2021-09-15 15:05:30
【问题描述】:

我的问题如下: 我编写了一个解决方法来自动下载包含文件名中包含版本号的程序文件,例如 programv1.55.50.zip(http 或 https 站点)。解决方法来自线性上升版本号(前身是版本 1.55.49,实际是 1.55.50 和后继“也许”1.55.51)我使用excel 构建行 1.55.50 到 1.57.99 并将它们加入下载链接。结果我得到了每个例子:

https:\linktofile\programv1.55.49.zip、https:\linktofile\programv1.55.50.zip .. https:\linktofile\programv1.57.99.zip。

我粘贴到一个 linkstoprogram.txt 文件中的所有链接,每个链接一行用于 -i 的开关 wget

使用

wget --timestamping --referer foo --recursive --no-parent --input-file=C:\Path\linkstoprogram.txt --show-progress --append-output=C:\Path\%Timestamp%_program.log

在所有链接上,我下载了两个文件 programv1.55.49.zip 和实际 programv1.55.50.zip 以及所有其他不可用“虚拟”文件的大量错误日志。这就是为什么我想分离这个查询的日志。下一个过程是按日期对它们进行排序并删除所有文件,排除最新的文件并复制它。

FOR /F "skip=1 eol=: delims=" %%G IN ('dir /b /o-d *.zip') DO SDelete64 -p 3 -r -nobanner "%%G"

copy programv<.zip program.zip

Windows批量使用问题,我也是批量使用最新的CoreUtils for Windows

那么我如何通过检查 200,302,403 和 404 值来检查返回的 HTTP 状态代码是否通过?

如果所有状态代码都通过,则将完整的下载链接附加到此过程中创建的文本文件中,如果一个或多个状态代码失败,则不执行任何操作。

wget -q --spider address 

似乎只针对状态代码 200 给出错误级别。所以所有错误级别似乎都是 0。 我找到了一些关于此上下文的参考,但没有进一步说明:

Here 回答 @DrFloyd5、here 回答 @Jim Davis 和 here 回答 @Stuart Siegler for Windows。

这个Bash-scripts(特别是回答@jm666)接近我想要的,但我无法批量复制它,因此需要一个带有传递下载链接的文本文件,而不是链接上的错误代码!

#!/bin/bash
while read LINE; do
  curl -o /dev/null --silent --head --write-out '%{http_code}\n' "$LINE"
done < url-list.txt

dosomething() {
        code="$1"; url="$2"
        case "$code" in
                200) echo "OK for $url";;
                302) echo "redir for $url";;
                404) echo "notfound for $url";;
                *) echo "other $code for $url";;
        esac
}

#MAIN program
while read url
do
        uri=($(echo "$url" | sed 's~http://\([^/][^/]*\)\(.*\)~\1 \2~'))
        HOST=${uri[0]:=localhost}
        FILE=${uri[1]:=/}
        exec {SOCKET}<>/dev/tcp/$HOST/80
        echo -ne "GET $FILE HTTP/1.1\nHost: $HOST\n\n" >&${SOCKET}
        res=($(<&${SOCKET} sed '/^.$/,$d' | grep '^HTTP'))
        dosomething ${res[1]} "$url"
done << EOF
http://stackoverflow.com
http://stackoverflow.com/some/bad/url
EOF

是否存在使用 wget -A 开关的解决方案,以便我根本不需要解决方法?

我对这两种解决方案都感兴趣!

【问题讨论】:

    标签: windows batch-file curl wget http-status-codes


    【解决方案1】:

    假设您有这个文本文件 linkstoprogram.txt 和同一文件夹中的批处理文件:


    linkstoprogram.txt

    https://www.google.tn/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
    https://linktofile/programv1.55.49.zip
    https://linktofile/programv1.55.50.zip 
    https://linktofile/programv1.57.99.zip
    https://www.google.com
    https://www.stackoverflow.com
    https://downloads.malwarebytes.com/file/mb3/
    https://download.toolslib.net/download/file/1/1388
    

    批次代码:Get_Status_Codes_Curl.bat

    @echo off
    Title Example Batch Script to Get Status Code from cURL
    Set "InputFile=%~dp0linkstoprogram.txt"
    Set "LogFile=%0.txt"
    If Exist "%LogFile%" Del "%LogFile%"
    @for /f "delims=" %%a in ('Type "%InputFile%"') do (
        echo %%a & curl -s -o -I -w "%%{http_code}\n" %%a
        >>"%LogFile%" (echo %%a & curl -s -o -I -w "%%{http_code}\n" %%a)
    )
    If Exist "%LogFile%" Start /MAX "Log" "%LogFile%"
    

    【讨论】:

    • 非常感谢您的回答。我检查了它并为两个现有的下载获得了 302。但也许服务器现在有问题。明天再试并提供反馈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多