我有一个名为abc.txt的文件想要根据修改的日期和时间连续阅读
我试过这样但我想根据日期和时间的变化在循环中检查文件。
Global $file
func filess()
$search = FileFindFirstFile("abc.txt")
if @error Then
MsgBox(0, "error", "failed to find first file")
Exit
EndIf
While -1
$file = FileFindNextFile($search)
If @error Then ExitLoop
ConsoleWrite("File:" & $file & @CRLF)
$time = FileGetTime($file,0)
$dmyyyy = $time[3]& ":" & $time[4] & ":" & $time[5]&'_'&$time[2]& "/" & $time[1] & "/" & $time[0]
MsgBox(0, "Modify date and time of file", $dmyyyy)
if @error Then
ConsoleWrite("Error getting time from " & $file & @CRLF)
EndIf
WEnd
Return $file
EndFunc
; Close the search handle
$mss=filess()
答案 0 :(得分:0)
你的脚本中有很多错误。试试这个:
$myfile = "abc.txt"
$OldFileTime = filess($myfile)
while 1
sleep(2000)
if $OldFileTime <> filess($myfile) then MsgBox(0, "Info","File " & $myfile & " has changed!")
wend
func filess($file)
$time = FileGetTime($file,0)
$dmyyyy = $time[3]& ":" & $time[4] & ":" & $time[5]&'_'&$time[2]& "/" & $time[1] & "/" & $time[0]
if @error Then ConsoleWrite("Error getting time from " & $file & @CRLF)
Return $dmyyyy
EndFunc
答案 1 :(得分:0)
$name = "c:\my_file.txt"
; set Monitor File Modification
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
$strComputer & "\root\cimv2")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' and " _
& 'TargetInstance.Name="'&$name & '"')
; Main Loop
While 1
$objLatestEvent = $colMonitoredEvents.NextEvent
your_func()
WEnd