1

我已经在这里写过这个问题,我们解决了它,但现在我必须添加这个“如果文件是在过去 24 小时内创建的,则复制它,否则不要”。我在网上找到了几个例子,但我似乎无法弄清楚命令语法。我真的很想为此提供一些帮助……这是我找到的网站:

https://github.com/npocmaka/batch.scripts/blob/master/fileUtils/fileModifiedTime.bat

这是我的代码:

ECHO OFF
set ReadFolder1="C:\Users\ugrum\Desktop\new"    
set Destination="C:\Users\ugrum\Desktop\Zacasnamapa"
set sevenZipDir="C:\Program Files\7-Zip\7zG.exe"
set currentDate=%date%

IF NOT EXIST %Destination% MKDIR %Destination%

FOR /R %ReadFolder1% %%G IN (*_NOT*) DO (
    ECHO %%G 
    FOR %%f IN %%G DO SET filedatetime=%%~tf
    IF %currentDate% - %filedatetime:~0, 10% <= 24      
    XCOPY "%%G" %Destination% /C
)

%sevenZipDir% a -tzip neustrezne.zip %Destination%
RMDIR /Q /S %Destination%

PAUSE

我知道这if是错误的,但我不知道如何让它正确。

4

1 回答 1

3

这是您的任务的批处理代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ReadFolder1=%USERPROFILE%\Desktop\new"
set "Destination=%USERPROFILE%\Desktop\Zacasnamapa"
set "SevenZipApp=%ProgramFiles%\7-Zip\7zG.exe"

if not exist "%Destination%" mkdir "%Destination%"

rem Get seconds since 1970-01-01 for current date and time.
rem From date string only the last 10 characters are passed to GetSeconds
rem which results in passing dd/mm/yyyy or dd.mm.yyyy in expected format
rem to this subroutine independent on date string of environment variable
rem DATE is with or without abbreviated weekday at beginning.
call :GetSeconds "%DATE:~-10% %TIME%"

rem Subtract seconds for 24 hours (24 * 3600 seconds) from seconds value.
set /A "CompareTime=Seconds-24*3600"

for /R "%ReadFolder1%" %%G in (*_NOT*) do (
    echo %%G
    set "FullFileName=%%G"
    for %%H in ("%%G") do (
        call :GetSeconds "%%~tH:0"
        setlocal EnableDelayedExpansion
        if !Seconds! GTR %CompareTime% (
            echo Copy file !FullFileName!
            %SystemRoot%\System32\xcopy.exe "!FullFileName!" "!Destination!\" /C /I /M /Q /R /Y >nul
        )
        endlocal
    )
)

"%SevenZipApp%" a -tzip neustrezne.zip "%Destination%"
rmdir /Q /S "%Destination%"

endlocal
goto :EOF


rem No validation is made for best performance. So make sure that date
rem and hour in string is in a format supported by the code below like
rem MM/DD/YYYY hh:mm:ss or M/D/YYYY h:m:s for English US date/time.

:GetSeconds
rem If there is " AM" or " PM" in time string because of using 12 hour
rem time format, remove those two strings and in case of " PM" remember
rem that 12 hours must be added to the hour depending on hour value.
set "DateTime=%~1"
set "Add12Hours=0"
if not "%DateTime: AM=%" == "%DateTime%" (
    set "DateTime=%DateTime: AM=%"
) else if not "%DateTime: PM=%" == "%DateTime%" (
    set "DateTime=%DateTime: PM=%"
    set "Add12Hours=1"
)

rem Get year, month, day, hour, minute and second from first parameter.
for /F "tokens=1-6 delims=,-./: " %%A in ("%DateTime%") do (
    rem For English US date MM/DD/YYYY or M/D/YYYY
    rem set "Day=%%B" & set "Month=%%A" & set "Year=%%C"
    rem For German date DD.MM.YYYY or English UK date DD/MM/YYYY
    set "Day=%%A" & set "Month=%%B" & set "Year=%%C"
    set "Hour=%%D" & set "Minute=%%E" & set "Second=%%F"
)

rem Remove leading zeros from the date/time values or calculation could be wrong.
if "%Month:~0,1%"  == "0" if not "%Month:~1%"  == "" set "Month=%Month:~1%"
if "%Day:~0,1%"    == "0" if not "%Day:~1%"    == "" set "Day=%Day:~1%"
if "%Hour:~0,1%"   == "0" if not "%Hour:~1%"   == "" set "Hour=%Hour:~1%"
if "%Minute:~0,1%" == "0" if not "%Minute:~1%" == "" set "Minute=%Minute:~1%"
if "%Second:~0,1%" == "0" if not "%Second:~1%" == "" set "Second=%Second:~1%"

rem Add 12 hours for time range 01:00:00 PM to 11:59:59 PM,
rem but keep the hour as is for 12:00:00 PM to 12:59:59 PM.
if %Add12Hours% == 1 if %Hour% LSS 12 set /A Hour+=12

set "DateTime="
set "Add12Hours="

rem Must use two arrays as more than 31 tokens are not supported
rem by command line interpreter cmd.exe respectively command FOR.
set /A "Index1=Year-1979"
set /A "Index2=Index1-30"

if %Index1% LEQ 30 (
    rem Get number of days to year for the years 1980 to 2009.
    for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
    for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
    rem Get number of days to year for the years 2010 to 2038.
    for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
    for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)

rem Add the days to month in year.
if "%LeapYear%" == "N" (
    for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
    for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)

rem Add the complete days in month of year.
set /A "Days+=Day-1"

rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"

rem Exit this subroutine.
goto :EOF

有关用于比较时间的方法的详细信息,请阅读我的答案

注1:

返回的日期和时间字符串的格式%DATE:~-10% %TIME%取决于%%~tF:0Windows 区域和语言设置。所编写的代码期望日期字符串dd/mm/yyyydd.mm.yyyydd-mm-yyyy时间字符串hh:mm:ss在小时、分钟或秒的情况下带有或不带有前导零,小于 10。

环境变量DATE的日期字符串开头的缩写工作日无关紧要,因为如果日期字符串在小于 10 的日期或月份的前导零时仅使用最后 10 个字符,这在 Windows 上是标准的。

对于美国日期格式mm/dd/yyyy,命令rem必须在子程序中从一行移动到另一行,GetSeconds如注释所述。

笔记2:

请参阅我对带空格的设置环境变量的答案,因为在必要时使用双引号括起来的原因set "variable=value with spaces"和参考值而不是使用可能导致执行错误代码的原因。variableset variable="value with spaces"

顺便一提:

您有没有想过花几块钱购买WinRAR的许可证,因为您可以使用WinRAR使用单个命令行或使用WinRAR的 GUI来完成压缩过去 24 小时内修改的所有文件的任务?想想您和我们为使用WinRAR这个非常容易解决的任务编写批处理代码所花费的时间,并将其与单个WinRAR许可证的成本进行比较。

使用WinRAR的命令行是:

%ProgramFiles%\WinRAR\WinRAR.exe a -ac -afzip -cfg- -ep -ibck -m5 -tn24h -y "%USERPROFILE%\Desktop\neustrezne.zip" "%USERPROFILE%\Desktop\new\*_NOT*"
  • a是表示添加到存档的命令。

  • -ac是一个开关,用于清除添加到存档的每个文件的存档属性。这在这里并不是真正必要的,但通常很有帮助。

  • -afzip明确告诉WinRAR创建一个 ZIP 存档。这个开关在这里并不是真正需要的,因为存档文件名以结尾,.zip因此WinRAR 会自动使用ZIP而不是RAR压缩。

  • -cfg-禁用创建档案的标准配置。在定期执行的批处理文件中使用WinRAR时使用此开关总是一个好主意,以使存档创建独立于未在命令行中定义的设置。

  • -ep导致添加没有存档路径的文件。

  • -ibck告诉WinRAR以最小化的方式运行到系统托盘作为后台应用程序。但是,如果在将文件添加到存档时发生错误,则使用使用的选项会显示一个对话框窗口。

  • -m5使用最佳压缩。

  • -tn24h这是WinRAR的一个不错的选择,7-Zip 目前不必只添加与最近24 小时内修改的指定目录中的模式匹配的文件,即比当前日期/时间减去24 小时更新。

  • -y假设所有用户查询都是。

有关WinRAR开关的更多详细信息,请从菜单帮助中的帮助主题中打开WinRAR,并在选项卡上打开目录命令行模式和子项开关

于 2015-12-29T19:23:36.487 回答