文件是批处理文件旧到4分钟 [英] File is older than 4 minutes in Batch file

查看:120
本文介绍了文件是批处理文件旧到4分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using this script to calculate the time of a file:

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

不过,我有一些错误,并纠正我不得不使用以下条件:

But I had some errors and to correct I had to use the following conditions:

 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

有没有简单的解决问题,而不必使用我创造这些条件的方式?

Is there a simpler way to solve the problem without having to use those conditions that I have created?

推荐答案

正如我上面的评论,纯批是日期的功能太可怕了。这里是一个混合批次/ JScript的脚本,将计算出一个文件(编辑:或目录)的年龄pretty容易。没有必要担心小时翻车,一天的变化,日光节约时间,或任何其他垃圾。这是因为基于毫秒所有易peasy柠檬挤压-Y午夜1970年1月1日。

As I commented above, pure batch is horrible at date math. Here's a hybrid batch / JScript script that'll compute the age of a file (edit: or directory) pretty easily. No need to worry about hour rollovers, day changes, daylight savings time, or any of that other garbage. It's all easy peasy lemon squeeze-y based on milliseconds since midnight Jan 1, 1970.

@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脚本的详细信息,请看到这个GitHub的页面。上面使用的风格是类似Hybrid.bat该网页上。

For more information on batch / JScript hybrid scripts, see this GitHub page. The style used above is similar to Hybrid.bat on that page.

这篇关于文件是批处理文件旧到4分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆