gpt4 book ai didi

windows - 批处理文件中的文件早于 4 分钟

转载 作者:可可西里 更新时间:2023-11-01 10:35:51 25 4
gpt4 key购买 nike

我正在使用这个脚本来计算一个文件的时间:

set "filename=myfile.txt"
rem extract current date and time
for /f "tokens=1-5 delims=.:, " %%a in ("%date% %time%") do (
set day=%%a&set mon=%%b&set yr=%%c&set hr=%%d&set min=%%e
)
rem extract file date and time
for /f "tokens=1-5 delims=.:, " %%a in ('"dir %filename%|find "%filename%""') do
(
set fday=%%a&set fmon=%%b&set fyr=%%c&set fhr=%%d&set fmin=%%e
)
rem calculate age of file (in minutes)
set /a "age=((hr*60+min)-(fhr*60+fmin)+(24*60))%%(24*60)"
set /a "max=8"
if %age% geq %max% echo.file is older than 8 minutes

但是我有一些错误,要更正我必须使用以下条件:

 if %hr% EQU 08 set hr=8
if %hr% EQU 09 set hr=9
if %min% EQU 08 set min=8
if %min% EQU 09 set min=9
if %fhr% EQU 08 set fhr=8
if %fhr% EQU 09 set fhr=9
if %fmin% EQU 08 set fmin=8
if %fmin% EQU 09 set fmin=9

有没有更简单的方法来解决问题,而不必使用我创建的那些条件?

最佳答案

正如我在上面评论的那样,纯批处理在日期数学上很糟糕。这是一个混合批处理/JScript 脚本,可以很容易地计算文件(编辑:或目录)的年龄。无需担心小时翻转、日期更改、夏令时或任何其他垃圾。自 1970 年 1 月 1 日午夜以来的毫秒数,这一切都很简单。

@if (@CodeSection==@Batch) @then

:: age.bat filename.ext
:: get age of file in minutes

@echo off
setlocal

if "%~1"=="" echo Usage: age.bat filename.ext && goto :EOF
if not exist "%~1" echo %1 not found. && goto :EOF

for /f %%I in ('cscript /nologo /e:JScript "%~f0" "%~1"') do (
echo %1 is %%I minutes old.

rem :: Now use "if %%I gtr 4" here to take whatever actions you wish
rem :: on files that are over 4 minutes old.

)

goto :EOF

:: end batch portion / begin JScript
@end

var fso = new ActiveXObject("scripting.filesystemobject"),
arg = WSH.Arguments(0),
file = fso.FileExists(arg) ? fso.GetFile(arg) : fso.GetFolder(arg);

// Use either DateCreated, DateLastAccessed, or DateLastModified.
// See http://msdn.microsoft.com/en-us/library/1ft05taf%28v=vs.84%29.aspx
// for more info.

var age = new Date() - file.DateLastModified;
WSH.Echo(Math.floor(age / 1000 / 60));

有关批处理/JScript 混合脚本的更多信息,see this GitHub page .上面使用的样式类似于该页面上的 Hybrid.bat。

关于windows - 批处理文件中的文件早于 4 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27296934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com