如果创建日期少于24小时,则批量代码

时间:2015-12-29 13:54:53

标签: batch-file if-statement datemodified

我已经在这里写了这个问题,我们解决了它,但现在我必须添加这个“如果文件是在过去24小时内创建的,那么复制它就不会”。我在网上找到了几个例子,但我似乎无法想出Command语法。我真的会对此有所帮助......这是我找到的网站:

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错了,但我不知道如何做对。

1 个答案:

答案 0 :(得分:3)

以下是您的任务的批处理代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
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
    for %%F in ("%%G") do (
        call :GetSeconds "%%~tF:0"
        if !Seconds! GTR %CompareTime% (
            echo Copy file %%G
            xcopy "%%G" "%Destination%\" /C /I /M /Q /R /Y >nul
        )
    )
)

"%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 2 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 "%DateTime: AM=%" NEQ "%DateTime%" (
    set "DateTime=%DateTime: AM=%"
) else if "%DateTime: PM=%" NEQ "%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%"  EQU "0" ( if "%Month:~1%"  NEQ "" set "Month=%Month:~1%"   )
if "%Day:~0,1%"    EQU "0" ( if "%Day:~1%"    NEQ "" set "Day=%Day:~1%"       )
if "%Hour:~0,1%"   EQU "0" ( if "%Hour:~1%"   NEQ "" set "Hour=%Hour:~1%"     )
if "%Minute:~0,1%" EQU "0" ( if "%Minute:~1%" NEQ "" set "Minute=%Minute:~1%" )
if "%Second:~0,1%" EQU "0" ( if "%Second:~1%" NEQ "" 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 2 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:0返回的日期和时间字符串格式取决于Windows区域和语言设置。编写的代码需要日期字符串dd/mm/yyyydd.mm.yyyydd-mm-yyyy,时间字符串hh:mm:ss在小时,分钟或秒的情况下有或没有前导零小于10。

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

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

注2:

请参阅set environment variables with spaces上的回答,原因是使用set "variable=value with spaces"并在必要时用双引号括起来的variable的参考值,而不是使用可能导致错误代码的set variable="value with spaces"执行。

顺便说一下:

你有没有想过花几块钱购买 WinRAR 的许可证,因为压缩所有在过去24小时内修改的文件的任务可以用 WinRAR 完成使用单个命令行或使用 WinRAR 的GUI?考虑一下你和我们花了多少时间使用 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 中打开,然后打开标签内容命令行模式和子项切换