Sign in to follow this  
Followers 0
pbecks1963

show last line of a text file with name: $searchname

4 posts in this topic

Hello Y'all,

I have these logfiles that are named after the pc or the user that produced it. (for instance: John.log or PC123.log)

I need to make a script that will search through the olders containing these files and find the file called $searchname and display the last line of this textfile. So if i search for "John" it will find "John.log" and display the last line.

How do i do this?

Share this post


Link to post
Share on other sites



Hi,

the code is working, if your log file exists only once in your folder structure.

;Startfolder
$folder = "c:\autoit"
;Name to search for, e.g. john
$search = InputBox ("Searching for", "Name to search for: ")
;Recursiv search in $startfolder for $search.log files, return array with full path
$arfiles = _GetFilesFolder_Rekursiv ($folder, $search & ".log", 0)
If $arfiles [0] = 1 Then
    MsgBox (0, "Last Line of " & $arfiles [1], FileReadLine ($arfiles [1], -1))
Else
    MsgBox (0, "Log File for " & $search , "not found")
EndIf

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     recursive listing of files and/or folders
; Parameter(s):    $sPath     Basicpath of listing ('.' -current path, '..' -parent path)
;                  $sExt      Extension for file selection '*' or -1 for all (Default)
;                  $iDir      -1 Files+Folder(Default), 0 only Files, 1 only Folder
;      optional:   $iRetType  0 for Array, 1 for String as Return
;      optional:   $sDelim    Delimiter for string return
;                             0 -@CRLF (Default)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                  Array[0] includes count of found files/folder
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If StringLen ($Folder.Path) > 250 Then $Folder = "\\?\" & $Folder
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then 
            If StringInStr ($Subfolder, "\\?\") Then
                $strFiles &= StringTrimLeft ($Subfolder.Path, 4) & '\' & $Delim
            Else
                $strFiles &= $Subfolder.Path & '\' & $Delim
            EndIf
        EndIf
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc

;-))

Stefan

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks man! works great for the pc log's but unfortunetly the user log's have different names. for instance:

".pbe.Beheer.MBO.NijmegenM.Helicon ( test user).log"

I would like to search for the user "pbe" (in this case) without having to know the rest of the search string (see below)

".Beheer.MBO.NijmegenM.Helicon ( test user).log"

So: what i am looking for is a text-file that starts with <dot>USERNAME<dot>

What do i change in the file?

Edited by pbecks1963

Share this post


Link to post
Share on other sites

Hi,

then try this (changes are only in main part):

#include <array.au3>
;Startfolder
$folder = "c:\autoit"
;Name to search for, e.g. john
$search = InputBox ("Searching for", "Name to search for: ")
;Recursiv search for all log files, return array with full path
$arfiles = _GetFilesFolder_Rekursiv ($folder, "log", 0)
$index = _ArraySearch ($arfiles, $search, 0, 0, 0, 1)
If $index <> - 1 Then
    MsgBox (0, "Last Line of " & $arfiles [$index], FileReadLine ($arfiles [$index], -1))
Else
    MsgBox (0, "Log File for " & $search , "not found")
EndIf

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0