AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77053262
Accepted
Diego
Diego
Asked: 2023-09-06 23:14:23 +0800 CST2023-09-06 23:14:23 +0800 CST 2023-09-06 23:14:23 +0800 CST

.bat 脚本复制明天日期的文件

  • 772

我正在尝试创建一个 .bat 脚本来复制名称中包含明天日期的文件。此脚本有效,但如果该月少于 31 天,则脚本无法正确给出当前日期。

该文件有巴西葡萄牙语的注释。

有人能帮我吗?

@echo off
chcp 850

setlocal enableextensions disabledelayedexpansion

    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" 

rem Obtenha a data atual em formato MM/DD/YYYY

    set "MES=%date:~0,2%"
    set "DIA=%date:~3,2%"
    set "ANO=%date:~6,4%"


rem Atualize a data para representar o dia seguinte
set /a "DIA=%DIA% + 1"

rem Verifique se o mês ultrapassa 12 (dezembro)
if %DIA% gtr 31 (
    set "DIA=1"
    set /a "MES=%MES% + 1"
    
    if %MES% gtr 12 (
        set "MES=1"
        set /a "ANO=%ANO% + 1"
    )
)

rem Formate a data para ter dois dígitos
set "DIA=0%DIA%"

rem Construa o nome do arquivo com a nova data
set "NOVA_DATA=VM_%DIA%-%MES%-%ANO%.mp3"

rem Construa os caminhos completos para os diretórios e arquivos
set "ORIGEM=C:\Users\Admin\Dropbox\CLOUD BEACH\Gratuitos - Gospel\PM - Vida Melhor\%NOVA_DATA%"
set "DESTINO=C:\Users\Admin\Dropbox\CLOUD BEACH\AUTODJ\AUTODJ - Gratuitos - Gospel\AutoDJ - Vida Melhor\VM.mp3"

rem Verifique a existência do arquivo com a nova data
if exist "%ORIGEM%" (
    copy /b "%ORIGEM%" "%DESTINO%"
) else (
    del /s /q "%DESTINO%"
)

rem Exiba uma mensagem na tela
echo.
echo ╔══════════════════════════════╗
echo ║  Favor não forçar o teclado    ║
echo ╚══════════════════════════════╝
echo.


echo "Data Atual: %DIA%-%MES%-%ANO%"

我希望“当前日期”变量提供第二天的日期,无论该月有多少天。运行该脚本的PC是Windows 7 64位。

batch-file
  • 2 2 个回答
  • 30 Views

2 个回答

  • Voted
  1. Best Answer
    Keith Langmead
    2023-09-07T00:29:46+08:002023-09-07T00:29:46+08:00

    虽然我认为我见过有人在批处理脚本中本地处理几个月内的几天,但需要大量(相对于您想要做的事情)代码来实现它,而像 Powershell 这样的东西可以轻松地本地处理它。

    在您的代码中,您可以添加以下内容:

    FOR /F %%i IN ('powershell.exe -command "& {(Get-Date).AddDays(1) | Get-Date -Format MMddyyyy}"') DO (set TOMORROWDATE=%%i)

    然后,%tomorrowdate%通过调用 Powershell,让它吐出明天的日期,并将其保存到变量中,从而保留明天的日期。

    根据您的代码(谷歌翻译告诉我 MES 是 Month 的缩写),我目前已将其设置为以 MMddyyyy 格式输出,因此今天将显示为 09062023,但如果需要,可以对其进行调整,例如将输出更改-Format MM-dd-yyyy为是 09-06-2023 等

    • 0
  2. Aacini
    2023-09-07T11:38:00+08:002023-09-07T11:38:00+08:00

    虽然批处理文件功能确实不如 PowerShell、VBScript 和其他脚本语言中的同等功能强大,但批处理文件确实足够强大,可以解决简单的问题,例如这个问题。Batch 在这方面的声誉不佳是因为程序员在创建 Batch 文件时经常采取糟糕的方法(但在 PowerShell 或 VBScript 中也可能发生同样的情况)。

    这个简单的批处理文件解决了您的问题:

    @echo off
    setlocal
    
    rem Get current date (with Day+1) from WMIC command,
    rem at same time, get the number of days in February
    for /F "tokens=1-3" %%a in ('wmic path win32_localtime get Day^,Month^,Year') do (
       set /A "Day=1+%%a, Month=%%b, Year=%%c, Feb=28+!(Year%%4)"  2>NUL
    )
    
    rem If Day+1 is greater than current DaysPerMonth, fix the date
    for /F "tokens=%Month%" %%d in ("31 %Feb% 31 30 31 30 31 31 30 31 30 31") do (
       if %Day% gtr %%d set /A "Day=1, Month+=1, NextM=!(Month-13), Month-=NextM*12, Year+=NextM"
    )
    
    rem Adjust Day and Month values for left zeros
    set /A "Day+=100, Month+=100"
    
    set "NOVA_DATA=VM_%Day:~1%-%Month:~1%-%Year%.mp3"
    echo %NOVA_DATA%
    

    您还可以使用我的printf.exe版本 2.11 程序以非常不同的方式解决此问题。

    我的 printf.exe 应用程序是一个 Windows 控制台程序,它是众所周知的printf CRT 函数的包装器,从而允许在 cmd.exe 窗口中显示文本和格式化数值。另外,我的程序printf.exe还允许使用基于堆栈的惠普计算器相同的方法和功能对32位整数和64位双浮点数进行逆波兰表示法算术运算。

    新的 printf.exe 版本 2.11 还管理字符串操作,并允许使用最简单的编程方案编写脚本(程序)。下面的批处理文件包含一个 printf.exe 程序,也可以解决此问题。请注意,printf.exe 的方法(在广泛的注释中清楚地解释了)使用更简单(较低级别)的指令,但出于同样的原因,更容易理解......

    @echo off
    setlocal
    
    rem Get current Day, Month, Year
    rem (this could also be done in the printf code)
    for /F "tokens=1-3" %%a in ('wmic path win32_localtime get Day^,Month^,Year') do (
       set /A "Day=%%a, Month=%%b, Year=%%c"  2>NUL
    )
    
    rem The printf.exe program below uses Integer storage registers R1..R12
    rem to store the (corresponding) number of days per Month
    rem and: R13=Day, R14=Month, R15=Year
    
    printf ^
        "VM_%%02i-%%02i-%%4i.mp3\n"         /*  Output format */ ^
        31 28 31 30 31 30 31 31 30 31 30 31 /* Days Per Month */ ^
        12 ]I /" < /"       /* RI = Index of R12 and drop it  */ ^
        (                   /* FOR /L RI IN (12,-1,1) DO (    */ ^
           ]i               /*    Store 31,30,31...           */ ^
           /" < /"          /*    Drop it                     */ ^
           ]--I             /*    Decrement I                 */ ^
           [I ==0? ;        /*    Is zero? : QUIT             */ ^
           /" < /"          /*    Drop the RI                 */ ^
        :                   /* NEXT RI                        */ ^
        )                   /* )                              */ ^
        Year atoi ].5       /* R15 = Year                     */ ^
        ( 4 ( %%? )?        /* IF Year MOD 4 is zero ?        */ ^
            ]++2            /*     Increment Days In February */ ^
        )                   /* ENDIF                          */ ^
        Month atoi ]I ].4   /* RI = Month, R14 = Month        */ ^
        day atoi ++ ].3     /* R13 = Day+1                    */ ^
        [i                  /* Day+1  DaysPerMonth            */ ^
        ( /" <=? /"         /* IF Day+1 GTR DaysPerMonth      */ ^
            1 ].3           /*     Day = 1                    */ ^
            ]++.4           /*     Month += 1                 */ ^
            ( [.4 13 ==?    /*     IF Month EQU 13            */ ^
                1 ].4       /*         Month = 1              */ ^
                ]++.5       /*         Year += 1              */ ^
            )               /*     ENDIF                      */ ^
        )                   /* ENDIF                          */ ^
        /" <* /"            /* Clear Stack                    */ ^
        [.3 [.4 [.5 OUT     /* Show result: Day-Month-Year    */ ^
     > result.txt
    
    set /P "NOVA_DATA=" < result.txt
    echo %NOVA_DATA%
    

    虽然看起来很复杂,但 printf.exe 指令非常简单。HP计算器用户在了解差异和编程方案后,可以在几分钟内开始编写printf.exe程序。您可以在这个答案中查看 printf.exe 程序的另一个示例

    您可以从此链接下载 printf.exe 包

    • 0

相关问题

  • 使用 .bat 文件重命名文件夹中的文件

  • 我的批处理代码在尝试转到 :apps 标签后崩溃

  • 批处理文件删除变量中.#后面的文本

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve