這是給你編寫一個批處理代碼:
@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/yyyy
或dd.mm.yyyy
或dd-mm-yyyy
和時間字符串hh:mm:ss
具有或不具有在小時,分鐘或秒的情況下,前導零小於10
在環境的日期字符串的開頭,一個縮寫平日變量DATE並不重要,因爲如果日期字符串在日期或月份中的前導零爲小於10,而在Windows上爲標準,則僅使用最後10個字符。
對於美國日期格式mm/dd/yyyy
,必須將子命令GetSeconds
中的命令rem
從一行移動到另一行,如註釋所解釋。
注2:
見我的回答對set environment variables with spaces使用set "variable=value with spaces"
和雙引號括起來在必要時代替使用set variable="value with spaces"
這可能導致在執行錯誤的代碼的variable
參考值的原因。
順便說一句:
你有沒有想過花幾塊錢購買的許可證的WinRAR爲你的任務壓縮最近24小時內修改的所有文件與可以做的WinRAR使用單個命令行或使用GUI的WinRAR?想想你和我們花了多少時間寫了一個批處理代碼,這個很容易解決的任務,使用WinRAR,並將它與小錢單獨許可證WinRAR的成本進行比較。
使用WinRAR的命令行是:
%ProgramFiles%\WinRAR\WinRAR.exe a -ac -afzip -cfg- -ep -ibck -m5 -tn24h -y "%USERPROFILE%\Desktop\neustrezne.zip" "%USERPROFILE%\Desktop\new\*_NOT*"
一個是這意味着添加到存檔的命令。
-ac
是一個開關,用於清除添加到檔案中的每個文件的歸檔屬性。這在這裏並不是必要的,但通常很有幫助。
-afzip
明確告訴WinRAR創建ZIP壓縮文件。由於檔案文件名以.zip
結尾,因此WinRAR會自動使用ZIP而不是RAR壓縮。
-cfg-
禁用創建檔案的標準配置。在定期執行的批處理文件中使用WinRAR時,使用此開關總是一個好主意,以使歸檔創建獨立於未在命令行上定義的設置。
-ep
導致添加沒有路徑的檔案。
-ibck
tell WinRAR將系統托盤最小化爲後臺應用程序。但如果在將文件添加到存檔時發生錯誤,則使用所選選項時會顯示一個對話窗口。
-m5
使用最佳壓縮。
-tn24h
這是不錯的選擇WinRAR的具有的7-Zip目前沒有隻添加符合過去24小時內修改指定目錄下的模式,即比當前日期/時間減去24小時較新的文件。
-y
假設是對所有用戶查詢。
有關WinRAR的開關的詳細信息開放的WinRAR的從菜單幫助的幫助主題,並在標籤內容項目命令行模式和子項目開關開放。
'FOR %% f IN(%% G)DO'是否是強制性的。 – Stephan
您無法使用CMD中的日期進行計算。您必須先將日期轉換爲數字。 (或使用外部工具,可以) – Stephan
你可以找到一些有用的鏈接[搜索功能](http://stackoverflow.com/search?q =%5Bbatch-file%5D + calculate + date + time + seconds) – Stephan