Что нового

Поиск самого свежего лог файла

Serje

Новичок
Сообщения
7
Репутация
0
Для простой ситуации - нужно найти самый последний лог файл в папке с логами. Функция очень простая, но кому-то может и пригодиться

Код:
#cs
   @error
   1 = Log directory does not exist
   2 = Function FileGetTime() returns error, @extended = @error for FileGetTime()
#ce

#include <File.au3>

Func _FindLatestLogFile($log_folder_path)
   Local $log_file_array[1], $log_file_date_array[1]
   Local $a, $latest_log_number, $latest_log_date
   Local $error_text

   $log_file_date_array[0] = 0

   $latest_log_date = "00000000000000"

   If Not(StringRight($log_folder_path, 1) == "\") Then
	  $log_folder_path = $log_folder_path & "\"
   EndIf

   If FileExists($log_folder_path) Then
	  $log_file_array = _FileListToArray($log_folder_path)
	  If $log_file_array = False Then
		 Switch @error
		 Case 1
			$error_text = "Folder not found or invalid"
		 Case 2
			$error_text = "Invalid $sFilter"
		 Case 3
			$error_text = "Invalid $iFlag"
		 Case 4
			$error_text = "No File(s) Found"
		 Case Else
			$error_text = "Unknown error"
		 EndSwitch
		 MsgBox(0x10, "Alarm!", "Function FileGetTime() returns error: " & $error_text)
		 SetError(2, @error)
		 Return False
	  Else
		 For $a = 1 To UBound($log_file_array) - 1
			ReDim $log_file_date_array[$a + 1]
			$log_file_date_array[$a] = FileGetTime($log_folder_path & $log_file_array[$a], 0, 1)
			$log_file_date_array[0] = $a
		 Next
	  EndIf
   Else
	  Switch MsgBox(0x15, "Alarm!", "Log directory does not exist!")
	  Case 2
		 SetError(1)
		 Return False
	  Case 4
		 _FindLatestLogFile($log_folder_path)
	  EndSwitch
   EndIf

   For $a = 1 To UBound($log_file_date_array) - 1
	  If $log_file_date_array[$a] > $latest_log_date Then
		 $latest_log_number = $a
		 $latest_log_date = $log_file_date_array[$a]
	  EndIf
   Next

   Return $log_file_array[$latest_log_number]

EndFunc
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Это можно сделать проще:

Код:
#include <File.au3>

$sFile = _FindLatestFile(@DesktopDir, 'log')
MsgBox(64, @error, $sFile)

Func _FindLatestFile($sPath, $sExt = '*')
	If Not FileExists($sPath) Then
		Return SetError(1)
	EndIf
	
	Local $iDate = 0, $sLatest_File
	Local $aFiles = _FileListToArray($sPath, '*.' & $sExt)
	
	For $i = 1 To UBound($aFiles)-1
		$iTmpDate = FileGetTime($sPath & '\' & $aFiles[$i], 0, 1)
		
		If $iTmpDate > $iDate Then
			$iDate = $iTmpDate
			$sLatest_File = $aFiles[$i]
		EndIf
	Next
	
   Return SetError(($sLatest_File = '' ? 2 : 0), 0, $sLatest_File)
EndFunc
 

alexnasa

Новичок
Сообщения
50
Репутация
0
Поиск самой "юной" папки
http://autoit-script.ru/index.php?topic=11505.msg75504#msg75504
 
Верх