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

批处理文件双击正常运行,Task Scheduler执行无输出问题排查

问题分析与解决方案

问题确实是工作目录不匹配导致的:任务计划程序默认以系统目录(如C:\Windows\System32)作为工作目录,而你的批处理使用了相对路径(pdfs/...、nyt-cookies.txt),这会导致curl找不到Cookie文件,或是将PDF下载到了系统目录而非你指定的Dropbox路径。

解决步骤

1. 修改批处理为绝对路径模式

以下是优化后的完整批处理代码,核心是基于脚本自身所在目录生成绝对路径,彻底避免相对路径的问题:

@echo off
setlocal enabledelayedexpansion

:: 获取脚本所在的绝对目录(包含末尾反斜杠)
set "SCRIPT_DIR=%~dp0"

:: 获取当前日期时间
for /f "tokens=2 delims==" %%I in ('"wmic os get localdatetime /value"') do set "datetime=%%I"

:: 提取年月日
set "year=%datetime:~0,4%"
set "month=%datetime:~4,2%"
set "day=%datetime:~6,2%"

:: 用数组映射简化月份缩写转换
set "month_map[01]=Jan"
set "month_map[02]=Feb"
set "month_map[03]=Mar"
set "month_map[04]=Apr"
set "month_map[05]=May"
set "month_map[06]=Jun"
set "month_map[07]=Jul"
set "month_map[08]=Aug"
set "month_map[09]=Sep"
set "month_map[10]=Oct"
set "month_map[11]=Nov"
set "month_map[12]=Dec"
set "monthname=!month_map[%month%]!"

:: 格式化日期字符串
set "year_short=%year:~2,2%"
set "formatted_date=%monthname%%day%%year_short%"
set "fileNameStr=%year%_%month%_%day%"

echo %formatted_date%

:: 定义下载URL
set "CROSSWORD_URL=https://www.nytimes.com/svc/crosswords/v2/puzzle/print/%formatted_date%.pdf"
echo %CROSSWORD_URL%

:: 用绝对路径定义输出文件和Cookie文件
set "OUTPUT_FILE=%SCRIPT_DIR%pdfs\%fileNameStr%.pdf"
set "COOKIES_FILE=%SCRIPT_DIR%nyt-cookies.txt"

:: 自动创建pdfs目录(如果不存在)
if not exist "%SCRIPT_DIR%pdfs\" mkdir "%SCRIPT_DIR%pdfs\"

:: 执行curl下载
echo Downloading file using cookies...
curl -b "%COOKIES_FILE%" -o "%OUTPUT_FILE%" "%CROSSWORD_URL%"

:: 检查下载结果并输出错误码
if %ERRORLEVEL% equ 0 (
    echo Download completed successfully: %OUTPUT_FILE%
) else (
    echo Failed to download the file. Error code: %ERRORLEVEL%
)

endlocal

2. 关键修改说明

  • set "SCRIPT_DIR=%~dp0":获取批处理脚本所在的绝对路径,确保所有文件操作都基于这个固定目录。
  • 用延迟变量扩展+数组映射替代冗余的月份判断代码,精简逻辑。
  • 添加目录自动创建逻辑:避免因pdfs目录不存在导致下载失败。
  • 所有路径均使用绝对路径,彻底脱离对任务计划工作目录的依赖。

3. 任务计划程序可选设置(无需修改批处理)

如果你不想修改批处理,也可以在任务计划程序中调整:

  • 编辑目标任务 → 切换到「操作」标签 → 点击「编辑」
  • 在「起始于(可选)」中填入批处理所在的完整目录(如C:\Users\你的用户名\Dropbox\Documents\Projects\Programming\nyt_daily)
  • 保存后,任务执行时会以该目录作为工作目录,相对路径即可正常生效

内容的提问来源于stack exchange,提问作者Tyler Moore

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 06:53:22