You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows批处理中findstr命令失效,wget输出为空求技术排查

问题分析与解决方案

哈哈,这个坑我之前踩过!你的问题根源其实很简单:wget默认把错误信息输出到标准错误流(stderr),而不是标准输出流(stdout)。你用管道|只能传递stdout的内容,所以findstr根本接不到任何带"error"的文本,自然每次errorlevel都是1,代码就一直走else分支输出"success"。

给你两种靠谱的解决办法:

办法1:把wget的错误流转到标准输出

通过重定向把stderr合并到stdout,这样管道就能捕获到错误信息,让findstr正常工作。我还顺便帮你处理了URL带空格的情况(避免参数拆分出错),修改后的代码如下:

@echo off
for /f "delims=" %%i in (TEST.txt) do call :request "%%i"
set /p in=Finish!
goto :eof

:request
echo | set /p= %~1 >> result.txt
:: 2>&1 表示把stderr重定向到stdout,再传给findstr
wget.exe -T 3 --tries=2 %~1 2>&1 | findstr /I "error"
if %errorlevel% == 0 (
    echo error >> result.txt
) else (
    echo success >> result.txt
)

办法2:直接检查wget的退出码(更可靠)

其实wget本身会用退出码告诉你执行结果:成功时返回0,失败(比如网络超时、404、连接失败)时返回非0值。这种方式比找"error"字符串靠谱多了,毕竟不同环境下wget的错误提示可能不一样。修改后的代码:

@echo off
for /f "delims=" %%i in (TEST.txt) do call :request "%%i"
set /p in=Finish!
goto :eof

:request
echo | set /p= %~1 >> result.txt
:: >nul 2>&1 丢弃所有输出,只关注退出码
wget.exe -T 3 --tries=2 %~1 >nul 2>&1
if %errorlevel% neq 0 (
    echo error >> result.txt
) else (
    echo success >> result.txt
)

内容的提问来源于stack exchange,提问作者김현우

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:30:09