如果创建日期少于 24 小时,则批号
Batch code if date created is less than 24 hours
这个问题我已经写到这里了,我们已经解决了,但是现在我必须添加这个"if the file was created in the last 24 hours copy it else don't"。我在网上找到了几个例子,但我似乎无法理解命令语法。我真的很感激这方面的帮助...这是我找到的网站:
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-ZipzG.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
是错误的,但我不知道如何改正。
这是您的任务的批处理代码:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ReadFolder1=%USERPROFILE%\Desktop\new"
set "Destination=%USERPROFILE%\Desktop\Zacasnamapa"
set "SevenZipApp=%ProgramFiles%-ZipzG.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
有关用于比较时间的方法的详细信息,请阅读我在
上的回答
- Find out if file is older than 4 hours in Batch file
- Batch file to delete files older than N days
注1:
%DATE:~-10% %TIME%
和 %%~tF:0
返回的日期和时间字符串的格式取决于 Windows 区域和语言设置。编写的代码预计日期字符串 dd/mm/yyyy
或 dd.mm.yyyy
或 dd-mm-yyyy
以及时间字符串 hh:mm:ss
在小时、分钟或秒的情况下有或没有前导零小于10.
环境变量 DATE 日期字符串开头的缩写工作日无关紧要,因为如果日期字符串前导零表示日或月,则仅使用最后 10 个字符小于 10,因为它是 Windows.
的标准
对于美国日期格式 mm/dd/yyyy
,命令 rem
必须在子例程 GetSeconds
中从一行移动到另一行,如评论所述。
注二:
请参阅我在 上的回答,因为在必要时使用 set "variable=value with spaces"
和 variable
的参考值用双引号括起来,而不是使用 set variable="value with spaces"
,这可能会导致执行时代码错误。
顺便说一句:
你有没有想过花几块钱买一个 WinRAR 的许可证,因为你压缩最近 24 小时内修改过的所有文件的任务可以用 WinRAR 使用单个命令行还是使用 WinRAR 的 GUI?想一想您和我们使用 WinRAR 为这个非常容易解决的任务编写批处理代码所花费的时间,并将其与 WinRARWinRAR[ 的单一许可证的小钱进行比较=125=]成本。
使用 WinRAR 的命令行是:
%ProgramFiles%\WinRAR\WinRAR.exe a -ac -afzip -cfg- -ep -ibck -m5 -tn24h -y "%USERPROFILE%\Desktop\neustrezne.zip" "%USERPROFILE%\Desktop\new\*_NOT*"
a 是表示 add 到存档的命令。
-ac
是一个开关,用于清除添加到存档的每个文件的存档属性。这在这里不是必需的,但通常很有帮助。
-afzip
明确告诉 WinRAR 创建一个 ZIP 压缩包。这个开关在这里并不是必需的,因为压缩文件名以 .zip
结尾,因此 WinRAR 会自动使用 ZIP 而不是 RAR压缩.
-cfg-
禁用创建档案的标准配置。在定期执行的批处理文件中使用 WinRAR 时使用此开关始终是一个好主意,以使压缩文件创建独立于未在命令行上定义的设置。
-ep
导致将没有路径的文件添加到存档中。
-ibck
告诉 WinRAR 将 运行 最小化到系统托盘作为后台应用程序。但是如果在将文件添加到存档时发生错误,使用选项会显示一个对话框 window。
-m5
使用最佳压缩。
-tn24h
这是一个很好的选项 WinRAR 有哪个 7-Zip 当前不必只添加与指定目录中的模式匹配的文件最近 24 小时内修改,即比当前 date/time 减 24 小时更新。
-y
假定对所有用户查询是。
有关 WinRAR 的更多详细信息,请在 WinRAR 中从菜单 帮助 打开 帮助主题,然后在选项卡 目录 项目 命令行模式 和子项目 开关.
这个问题我已经写到这里了,我们已经解决了,但是现在我必须添加这个"if the file was created in the last 24 hours copy it else don't"。我在网上找到了几个例子,但我似乎无法理解命令语法。我真的很感激这方面的帮助...这是我找到的网站:
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-ZipzG.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
是错误的,但我不知道如何改正。
这是您的任务的批处理代码:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ReadFolder1=%USERPROFILE%\Desktop\new"
set "Destination=%USERPROFILE%\Desktop\Zacasnamapa"
set "SevenZipApp=%ProgramFiles%-ZipzG.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
有关用于比较时间的方法的详细信息,请阅读我在
上的回答- Find out if file is older than 4 hours in Batch file
- Batch file to delete files older than N days
注1:
%DATE:~-10% %TIME%
和 %%~tF:0
返回的日期和时间字符串的格式取决于 Windows 区域和语言设置。编写的代码预计日期字符串 dd/mm/yyyy
或 dd.mm.yyyy
或 dd-mm-yyyy
以及时间字符串 hh:mm:ss
在小时、分钟或秒的情况下有或没有前导零小于10.
环境变量 DATE 日期字符串开头的缩写工作日无关紧要,因为如果日期字符串前导零表示日或月,则仅使用最后 10 个字符小于 10,因为它是 Windows.
的标准对于美国日期格式 mm/dd/yyyy
,命令 rem
必须在子例程 GetSeconds
中从一行移动到另一行,如评论所述。
注二:
请参阅我在 set "variable=value with spaces"
和 variable
的参考值用双引号括起来,而不是使用 set variable="value with spaces"
,这可能会导致执行时代码错误。
顺便说一句:
你有没有想过花几块钱买一个 WinRAR 的许可证,因为你压缩最近 24 小时内修改过的所有文件的任务可以用 WinRAR 使用单个命令行还是使用 WinRAR 的 GUI?想一想您和我们使用 WinRAR 为这个非常容易解决的任务编写批处理代码所花费的时间,并将其与 WinRARWinRAR[ 的单一许可证的小钱进行比较=125=]成本。
使用 WinRAR 的命令行是:
%ProgramFiles%\WinRAR\WinRAR.exe a -ac -afzip -cfg- -ep -ibck -m5 -tn24h -y "%USERPROFILE%\Desktop\neustrezne.zip" "%USERPROFILE%\Desktop\new\*_NOT*"
a 是表示 add 到存档的命令。
-ac
是一个开关,用于清除添加到存档的每个文件的存档属性。这在这里不是必需的,但通常很有帮助。-afzip
明确告诉 WinRAR 创建一个 ZIP 压缩包。这个开关在这里并不是必需的,因为压缩文件名以.zip
结尾,因此 WinRAR 会自动使用 ZIP 而不是 RAR压缩.-cfg-
禁用创建档案的标准配置。在定期执行的批处理文件中使用 WinRAR 时使用此开关始终是一个好主意,以使压缩文件创建独立于未在命令行上定义的设置。-ep
导致将没有路径的文件添加到存档中。-ibck
告诉 WinRAR 将 运行 最小化到系统托盘作为后台应用程序。但是如果在将文件添加到存档时发生错误,使用选项会显示一个对话框 window。-m5
使用最佳压缩。-tn24h
这是一个很好的选项 WinRAR 有哪个 7-Zip 当前不必只添加与指定目录中的模式匹配的文件最近 24 小时内修改,即比当前 date/time 减 24 小时更新。-y
假定对所有用户查询是。
有关 WinRAR 的更多详细信息,请在 WinRAR 中从菜单 帮助 打开 帮助主题,然后在选项卡 目录 项目 命令行模式 和子项目 开关.