[英]Comparing last modified timestamps to update a file using batch script
I want to create a batch file which will read a .log file, then picks up errors from it and append to a .txt file. 我想创建一个批处理文件,它将读取一个.log文件,然后从中拾取错误并追加到.txt文件中。 But I don't want the batch to rewrite .txt every time, so now I am looking to compare the last modification timestamps of both these files and then append the latest updation only.
但是我不希望该批处理每次都重写.txt,所以现在我希望比较这两个文件的最后修改时间戳,然后仅附加最新的更新。 Following is the batch file
以下是批处理文件
@echo off
color 3
cls
@FOR %%A IN ("%ProgramFiles(x86)%\Apache Software Foundation\Apache2.2\logs\error.log") DO @(ECHO=%%~tA& set timestamp=%%~tA)
echo %timestamp%
@FOR %%A IN ("D:\error.txt") DO @(ECHO=%%~tA& set timestamp2=%%~tA)
echo %timestamp2%
if %timestamp% gtr %timestamp2% (
set DirToSearch="C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs"
set LineToRead="error"
pushd %DirToSearch%
for /r %%f in (error.log) do (
For /F "tokens=*" %%l in ('Findstr /L /I %LineToRead% "%%f"') do (
if %%l equ " " (
echo File:"%%f" is Not OK >> D:\FileStatus.txt
) else (
echo Line: %%l>>D:\error.txt
)
)
)
Goto :End
:End
popd
)
pause
exit
Now here I am unable to compare the timestamps It would be great help if anyone contributes to achieve this. 现在,我无法比较时间戳,如果有人为实现这一目标做出了很大的帮助。
@echo off
SETLOCAL
set "DirToSearch=C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs"
set "processedfile=D:\error.txt"
set "LineToRead=error"
pushd %DirToSearch%
COPY "%processedfile%" ".\processedfile.log" >nul 2>nul
for /f "delims=" %%a in ('dir /b /a-d /od *.log') do (
if /i "%%a"=="error.log" goto end
if /i "%%a"=="processedfile.log" goto process
)
:process
for /r %%f in (error.log) do (
For /F "tokens=*" %%l in ('Findstr /L /I /C:"%LineToRead%" "%%f"') do (
if %%l equ " " (
echo File:"%%f" is Not OK >> D:\FileStatus.txt
) else (
echo Line: %%l>>D:\error.txt
)
)
)
:end
del "processedfile.log"
popd
pause
exit
I've removed the fluff from the code. 我已经从代码中删除了绒毛。
Note that after an @echo off
statement, @
at the start of a command is redundant ( @command
means "don't echo the command) 请注意,在
@echo off
语句之后,命令开头的@
是多余的( @command
意思是“不要回显命令”)
The setlocal
command ensures that changes made to the environment are discarded when the batch terminates. setlocal
命令确保在批处理终止时放弃对环境所做的更改。
Note the new position of the quotes - to ensure that the value assigned to the variable does not include any possible stray trailing spaces. 注意引号的新位置-确保分配给变量的值不包括任何可能的尾随空格。
After switching directory, the processed file is copied
to the current directory in a unique name (whatever name you choose, as long as it has a .log
extension) - this places a copy of the file in the log directory, so that the magic can be performed. 切换目录后,已处理的文件将以唯一名称(无论您选择的是什么名称,只要具有
.log
扩展名) copied
到当前目录-这会将文件的副本放置在日志目录中,从而使可以执行。
The dir
command reports the names of the .log
files found; dir
命令报告找到的.log
文件的名称; /od
provides the list in timestamp order. /od
按时间戳顺序提供列表。 Therefore, whichever of the two files error.log
and processedfile.log
occurs first in the list will cause the code to branch to either process
to process the data (the processedfile.log
file is earlier than the error.log
so new data has been added) or end
( error.log
is earlier, so no new data has been added) 因此,这两个文件的哪个
error.log
和processedfile.log
第一次出现在列表中会导致代码分支要么process
来处理数据( processedfile.log
文件比早期error.log
所以新的数据已经添加)或end
( error.log
较早,因此未添加新数据)
I've made only one minor change to your findstr
- added /c:
and quoted the target string. 我对您的
findstr
只做了一个小改动-添加了/c:
并引用了目标字符串。 This is largely redundant, but if you change the string to include a space, it provides a verbatim target for findstr
. 这在很大程度上是多余的,但是如果您将字符串更改为包含空格,则它将为
findstr
提供逐字目标。 The remainder of that processing as as it was as I don't know the exact details of the processing required. 我不知道该处理的其余细节,因为我不知道所需处理的确切细节。
Note that in your code, DirToSearch
and LineToRead
were being re-assigned within the code block (series of lines enclosed in parentheses). 请注意,在您的代码中,
DirToSearch
和LineToRead
被重新分配在代码块内(括号内的一系列行)。 This would not work because the entire code block is parsed then executed, and the parsing process replaces any %var%
with its value at parse time . 这将无法正常工作,因为整个代码块都将被解析然后执行,并且解析过程会在解析时将
%var%
替换为其值。 Your code failed to fail - because you were not using a setlocal
, once the code had run, the variables remained assigned, and the code on future runs would use the value assigned in the prior run. 您的代码失败-因为您没有使用
setlocal
,所以一旦代码运行,变量将保持分配状态,以后运行的代码将使用先前运行中分配的值。
Note that :
is not required in a goto
except in the specific goto :eof
which means "go to end-of-file". 需要注意的是
:
在不要求goto
除非在特定goto :eof
,意思是“去档案结尾”。
Your goto end
is redundant, as end
directly follows the goto
- but again, it fails to fail. 您的
goto end
是多余的,因为end
直接跟随goto
-但是同样,它失败了。 A label within a code block terminates the block. 代码块中的标签终止该块。 In this case, it was irrelevant since nothing important followed the label.
在这种情况下,这是无关紧要的,因为标签后面没有重要的内容。
Having reached the label :end
, the process now deletes the copy of the processed file and pops the original directory back with a popd
instruction. 到达标签
:end
,该过程现在删除已处理文件的副本,并使用popd
指令将原始目录弹出回去。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.