简体   繁体   English

如何检查文件的大小是否从上次在批处理或Java中修改?

[英]How to check if size of a file was modified from the last time in batch or java?

I have a script that reads a log from a remote machine and in case when some errors are in there, the lines with the errrors are copied to my computer in a Test.txt file . 我有一个脚本,该脚本从远程计算机读取日志,并且如果其中存在一些错误,则带有错误的行会以Test.txt文件的形式复制到我的计算机中。 Here I will do some logic like send email if the size of this file is greater than 0 (this means errors found). 如果此文件的大小大于0(表示发现错误),在这里我将执行一些逻辑,例如发送电子邮件。 The issue is that this script will send email every time even if the log will contain only 1 error. 问题是,即使日志仅包含1个错误,此脚本也会每次发送电子邮件。

So, what I am thinking to do is to check if the file from my computer has the size bigger than the last time size. 因此,我想做的是检查计算机中文件的大小是否大于上次的大小。 I need to do this in batch or in java. 我需要批量或在Java中执行此操作。

It would be possible to compare file size or last modification date of a file since last check by saving file size and/or last modification time into one more file and compare actual file size/last modification date with saved string in the additional file. 通过将文件大小和/或最后修改时间保存到另一个文件中,可以比较自上次检查以来的文件大小或文件的最后修改日期,并将实际文件大小/最后修改日期与附加文件中保存的字符串进行比较。

But much easier to use for this purpose is the archive attribute which is automatically always set if a file is modified. 但是更容易使用的是archive属性,如果修改了文件,它将始终自动设置。 So all to do is remove the archive attribute from file once the data in the file were processed and check next if the archive attribute is set again because of a file modification. 因此,所有要做的就是在处理完文件中的数据后,从文件中删除存档属性,然后检查是否由于文件修改而再次设置了存档属性。

Here is an example code: 这是示例代码:

@echo off
set "LogFile=C:\Temp\Errors.log"
"%SystemRoot%\system32\attrib.exe" "%LogFile%" | "%SystemRoot%\system32\findstr.exe" /B /L A 1>nul
if errorlevel 1 goto EndBatch

rem The log file has archive attribute set as modified since last check
echo File "%LogFile%" was modified since last check.

rem Remove the archive attribute from log file before reading the
rem contents of the log file and send the error lines via email.
rem Of course the archive attribute can be removed also after the
rem data of the log files were processed by additional batch code.
"%SystemRoot%\system32\attrib.exe" -A "%LogFile%"

:EndBatch
set LogFile=

The Windows standard application attrib.exe outputs the file attributes at beginning of the output line and after some spaces the file name. Windows标准应用程序attrib.exe在输出行的开头输出文件属性,并在某些空格后输出文件名。 The output line starts with A if only the archive attribute is set and with a space if whether archive attribute nor any other attribute is set for the file. 如果仅设置了存档属性,则输出行以A开头;如果为文件设置了存档属性或任何其他属性,则输出行以空格开头。

The Windows standard application findstr.exe with options /B and /L searches for A at beginning of the line and if found terminates with exit code 0. But if the output line from attrib.exe does not start with A , the return value of findstr.exe is 1 assigned to environment variable ERRORLEVEL which means the archive attribute is not set on file and therefore the file was not modified since last check. 具有选项/B/L的Windows标准应用程序findstr.exe在行的开头搜索A ,如果找到则以退出代码0终止。但是,如果attrib.exe的输出行不是以A开头,则返回值findstr.exe被分配给1环境变量ERRORLEVEL ,这意味着未在文件上设置archive属性,因此自上次检查以来未修改文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM