每周运行 API 批处理文件到最后一周(星期五)

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

我有一个 API 批处理文件,可以输出各个文件,但我必须每周设置日期。有没有办法从特定的开始日期(例如 2024 年 1 月 1 日到整个星期五)运行它?

尝试了一些示例,但我不是编码员,并且真的会使用帮助

以下示例:

@echo off
cd C:\scripts
python api_report.py

setlocal enabledelayedexpansion

REM Set the start date (1 January of the current year)
REM set start_year=%date:~10,4%
REM set start_date=%start_year%-08-11

REM Set the end date (10 August of the current year)
REM set end_date=%start_year%-08-16

REM Set the start date (YYYY-MM-DD)
set start_date=2024-10-12

REM Set the end date (YYYY-MM-DD)
set end_date=2024-10-18

REM Loop through each day from the start date to the end date
for /L %%A in (0,1,365) do (
    REM Get the current date in the loop
    set "current_date=!start_date!"
    for /f "tokens=1-3 delims=-" %%B in ("!current_date!") do (
        set year=%%B
        set month=%%C
        set day=%%D
        
        REM Format the output filename as "YYYY-MM-DD Rates"
        set "output_file=%%B-%%C-%%D Rates.txt"
        
        REM Construct the URL by replacing YYYY, MM, DD with the actual year, month, and day
        REM original: set "EXAMPLE"
        set "EXAMPLE"

        rem set "formatted_url=https://example.com/rates/%%B-%%C-%%D"
        
        REM Display the formatted URL
        echo Formatted URL: !formatted_url!
        
        REM Use curl to download the content and save it to the output file
        curl --ssl-no-revoke "!formatted_url!" -o "!output_file!"

        REM Increment the current date
        for /f "tokens=1 delims= " %%E in ('powershell "(Get-Date -Date '!current_date!').AddDays(1).ToString('yyyy-MM-dd')"') do set start_date=%%E

        REM Check if the current date is beyond the end date
        if !start_date! gtr %end_date% (
            goto endloop
        )
    )
)

:endloop
echo Done.
pause
api batch-file url
1个回答
0
投票

这是我的方法,不使用 powersmell:

@ECHO OFF
SETLOCAL

SET "restart_file=u:\restart_file.txt"

:: Produce results for the start date specified (%1) and next 6 days
:: If %1 is missing, read start date from file
:: If %2 is anything (or %1='skip', do not update file)

SET "skipupdate=%~2"
SET "start_date=%~1"
IF /i "%start_date%"=="skip" SET "skipupdate=Y"&SET "start_date="
IF NOT DEFINED start_date FOR /f %%b IN ('type "%restart_file%" 2^>nul') DO SET "start_date=%%b"
IF NOT DEFINED start_date ECHO no START date?&GOTO :eof

SET /a days=7

ECHO starting with %start_date%" for %days%

:nextday

for /f "tokens=1-3 delims=-" %%g in ("%start_date%") do (
 SET "year=%%g"
 SET "Month=1%%h"
 SET "Day=1%%i"
 set "output_file=%%g-%%h-%%i Rates.txt"
 set "formatted_url=https://example.com/rates/%%g-%%h-%%i"
)

ECHO curl --ssl-no-revoke "%formatted_url%" -o "%output_file%"

:: increment date
:: Note Month and Day are 100+actual
SET /a day+=1
IF %day% == 132 GOTO nextmonth
IF %day% leq 128 GOTO reassembledate
:: Feb 29th?
SET /a leap=year %% 4
IF %month%%day%==102129 IF %leap%==0 (GOTO reassembledate) ELSE (GOTO nextmonth)
IF %month%%day%==102130 GOTO nextmonth
FOR %%b IN (04 06 09 11) DO IF "%month%%day%"=="1%%b131" GOTO nextmonth
::
:reassembledate
SET "start_date=%year%-%month:~-2%-%day:~-2%"
SET /a days-=1
IF %days% neq 0 GOTO nextday
:: Done #days
IF NOT DEFINED skipupdate ECHO %start_date%>"%restart_file%"

ECHO done

GOTO :eof

:: 1st of new month

:nextmonth
SET /a day=101
SET /a month+=1
IF %month%==113 (SET /a year+=1&SET /a month=101)
GOTO reassembledate

Soo...第一部分只是确定提供哪些参数。

如果没有提供日期参数,请尝试从文件中读取

根据原始语法分割日期。请注意,

month
day
的设置包括前导
1
,因此例如 9 月将为
109
。这是为了更容易计算,因为
09
将被解释为 八进制 数字(以
0
开头),而
09
是无效的八进制。

所以用零件构建

output_file
formatted_url

然后

echo
curl
行(用于验证 - 删除
echo
关键字以调用
curl

更改日期,适当设置格式(请参阅

set /?
或 docco 上的许多示例)并减少
days
计数。

如果还有剩余,请使用新的

start_date

重试

如果允许,还可以在文件中记录下一周的开始时间。

因此,每周通过周六的计划任务运行不带参数的代码将产生前一个周六..周五的结果。

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