将三个字母的星期几插入 .bat 文件中的字符串中

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

我编写了一个脚本,在计算机上将《纽约时报》每日填字游戏下载为 PDF,然后推送到平板电脑,但希望插入一个三个字母的星期几字符串“MON、TUE 等”。到字符串的末尾,这样我就可以轻松查看哪个是周日拼图等。

不是整个脚本,但这是我到目前为止的相关行,想知道如何将该字符串添加到%fileNameStr%中。

@echo off
setlocal

:: Get the current date and time
for /f "tokens=2 delims==" %%I in ('"wmic os get localdatetime /value"') do set "datetime=%%I"

:: Extract year, month, and day
set "year=%datetime:~0,4%"
set "month=%datetime:~4,2%"
set "day=%datetime:~6,2%"

:: Convert month number to month abbreviation
set "monthname="
for %%m in (01 02 03 04 05 06 07 08 09 10 11 12) do (
    if "%month%"=="%%m" set "monthname=%%m"
)

if "%month%"=="01" set "monthname=Jan"
if "%month%"=="02" set "monthname=Feb"
if "%month%"=="03" set "monthname=Mar"
if "%month%"=="04" set "monthname=Apr"
if "%month%"=="05" set "monthname=May"
if "%month%"=="06" set "monthname=Jun"
if "%month%"=="07" set "monthname=Jul"
if "%month%"=="08" set "monthname=Aug"
if "%month%"=="09" set "monthname=Sep"
if "%month%"=="10" set "monthname=Oct"
if "%month%"=="11" set "monthname=Nov"
if "%month%"=="12" set "monthname=Dec"

:: Format year to two digits
set "year_short=%year:~2,2%"

:: Combine into desired format
set "formatted_date=%monthname%%day%%year_short%"
set fileNameStr=%year%_%month%_%day%

:: Define the filename for the downloaded file
set OUTPUT_FILE="pdfs\%year%-%month%\%fileNameStr%.pdf"
datetime batch-file cmd
1个回答
0
投票

您可以使用 Cscript 转换为 ISO 兼容变量。

我将只留下核心供您适应,但您知道如何将年份提取为 4 位数字,如果有人希望使用“一周中的长日”,则将

Weekday(Now),1,1
更改为
Weekday(Now),0,1

@echo off
setlocal EnableDelayedExpansion

echo Dim ISO,DOW,ISODOW >%tmp%\DOW.vbs
echo ISO = (((year(Now)*100 + month(Now))*100 + day(Now))) >%tmp%\DOW.vbs
echo DOW = WeekdayName(Weekday(Now),1,1) >>%tmp%\DOW.vbs
echo ISODOW = ISO ^& DOW : WScript.Echo ISODOW >>%tmp%\DOW.vbs
cscript //Nologo %tmp%\DOW.vbs >%tmp%\DOW.txt
set /p ISODOW=<%tmp%\DOW.txt
echo !ISODOW:~2,2!-!ISODOW:~4,2!-!ISODOW:~6,2!-!ISODOW:~8,3!

echo continue with above as required

enter image description here

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