Head - Tail v0.65 Build 2011-03-15 Beta

  • In unserem Server File Migrationsprojekt kopieren wir per Robocopy Ordner und Dateien von einem Server zum Anderen und lassen die Ausgabe von Robocopy in eine Logdatei schreiben.
    Zum größten Teil werden diese Logfiles relative groß und wenn man das Resultat des Copyjobs sehen möchte, muss man die Logdateien öffnen, um das Resultat zu sehen, welches immer am Ende steht.
    Da die Logfiles meistens sehr groß werden können, ich hatte schon mal über 300 MB, dauert das Öffnen relativ lange, um nur die gewünschte Stelle (die letzten Zeilen) in der Logdatei zu sehen.
    Da ich aus meiner Studienzeit die Unix Befehle Head und Tail kenne, dachte ich, man könnte sie ja auch in AutoIt implementieren, um schnell die gewünschte Info zu bekommen.

    Head und Tail kann man aus der CMD als Command Line Tool einsetzen oder als GUI, wenn man direkt die Exe Datei per Mausklick startet.

    Limitierung: gesperrte Dateien kann man leider nicht öffnen.

    Hier die Command Line Parameter für Tail (Head ist analog):

    Head zeigt den "Kopf" und Tail das Ende einer Datei an.

    Um die Command Line Version zu benutzen, müssen die Skripte kompiliert sein!

    Head.au3

    Spoiler anzeigen
    [autoit]


    #AutoIt3Wrapper_Run_Obfuscator=y
    #Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0
    #AutoIt3Wrapper_UseX64=n
    #AutoIt3Wrapper_Change2CUI=y
    #AutoIt3Wrapper_Res_Description=Head displays n bytes from the header of a file
    #AutoIt3Wrapper_Res_Fileversion=0.6.5.0
    #AutoIt3Wrapper_Res_LegalCopyright=UEZ 2011
    #AutoIt3Wrapper_Res_Language=1033
    #AutoIt3Wrapper_Res_Field=Coded by|UEZ
    #AutoIt3Wrapper_Res_Field=Build|2011-03-15 beta
    #AutoIt3Wrapper_Res_Field=Compile date|%longdate% %time%
    #AutoIt3Wrapper_Res_Field=AutoIt Version|%AutoItVer%
    #AutoIt3Wrapper_UseUpx=n
    #AutoIt3Wrapper_UPX_Parameters=--ultra-brute --crp-ms=999999 --all-methods --all-filters
    #AutoIt3Wrapper_Run_After=del /f /q "%scriptdir%\%scriptfile%_Obfuscated.au3"
    #AutoIt3Wrapper_Run_After=Upack.exe %out% -c6 -f273 -red
    ;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

    [/autoit] [autoit][/autoit] [autoit]

    Opt("MustDeclareVars", 1)
    #include <EditConstants.au3>
    #include <File.au3>
    #include <GUIConstantsEx.au3>
    #include <Process.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global Const $name = "Head ", $ver = "v0.65 ", $build = "Build 2011-03-15 beta ", $coder = "by UEZ"
    Global $filename, $bytes = 512, $gui = False, $save = False, $hGUI, $hEdit, $i, $var

    [/autoit] [autoit][/autoit] [autoit]

    If _ProcessGetName(_WinAPI_GetParentProcess(@AutoItPID)) = "cmd.exe" Then
    If Not $CmdLine[0] Then Help()
    CheckParameter()
    Else
    $var = WinList()
    For $i = 1 To $var[0][0]
    If $var[$i][0] <> "" And BitAND(WinGetState($var[$i][1]), 2) Then
    If WinGetProcess($var[$i][0], "") = @AutoItPID Then
    WinSetState($var[$i][1], "", @SW_HIDE)
    ExitLoop
    EndIf
    EndIf
    Next
    GUI(0)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $gui Then
    GUI($filename)
    Else
    PrintHeadofFile($filename)
    EndIf
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    Func GUI($filename)
    Local $nMsg
    If Not $filename Then
    $filename = FileOpenDialog("Select a file", "", "All Files (*.*)")
    If @error Then Exit MsgBox(16, "Error", "No file selected! Closing...", 10)
    Else
    If Not FileExists($filename) Then Exit MsgBox(16, "ERROR ", $filename & " not found! Please check filename and try again.")
    EndIf
    $hGUI = GUICreate($name & $ver & $build & $coder, 800, 600)
    $hEdit = GUICtrlCreateEdit("", 0, 0, 800, 600, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $WS_HSCROLL, $WS_VSCROLL))
    GUICtrlSendMsg($hEdit, $EM_LIMITTEXT, -1, 0)
    GUICtrlSetFont($hEdit, 12, 400, 0, "Courier New")
    GUICtrlSetBkColor($hEdit, 0xFFFFFF)
    GUISetState(@SW_SHOW)
    $gui = True
    PrintHeadofFile($filename)
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd
    EndFunc ;==>GUI

    [/autoit] [autoit][/autoit] [autoit]

    Func PrintHeadofFile($filename)
    Local $data, $em, $hSave
    Local Const $fsize = FileGetSize($filename)
    If FileGetSize($filename) < $bytes Then $bytes = $fsize
    If $bytes > 0x7FFFFF Then $bytes = 512
    Local $hFile = FileOpen($filename, 0)
    If $hFile = -1 Then
    $em = "ERROR: unable to open file. The file may has been locked by another process."
    If Not $gui Then
    ConsoleWrite(@CRLF & $em & @CRLF & @CRLF)
    Exit
    Else
    GUICtrlSetData($hEdit, $em)
    Return
    EndIf
    EndIf
    $data = FileRead($hFile, $bytes)
    FileClose($hFile)
    If Not $gui Then
    ConsoleWrite(@CRLF & $data & @CRLF & @CRLF)
    Else
    GUICtrlSetData($hEdit, $data)
    EndIf
    If $save Then
    $hSave = FileOpen($save, 2)
    If $hSave = -1 Then Exit ConsoleWrite(@CRLF & "ERROR: " & $save & " could not be created!" & @CRLF & @CRLF)
    FileWrite($hSave, $data)
    FileClose($hSave)
    EndIf
    $data = ""
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;~ Func PrintHeadOfFile($filename)
    ;~ Local $nBytes, $data, $em, $hSave
    ;~ Local Const $fsize = FileGetSize($filename)
    ;~ If FileGetSize($filename) < $bytes Then $bytes = $fsize
    ;~ If $bytes > 0x7FFFFF Then $bytes = 512
    ;~ Local $tBuffer = DllStructCreate("byte[" & $bytes & "]")
    ;~ Local $hFile = _WinAPI_CreateFile($filename, 2, 7)
    ;~ ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hFile = ' & $hFile & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    ;~ If Not $hFile Then
    ;~ $em = "ERROR: unable to open file. The file may has been locked by another process."
    ;~ If Not $gui Then
    ;~ ConsoleWrite(@CRLF & $em & @CRLF & @CRLF)
    ;~ Exit
    ;~ Else
    ;~ GUICtrlSetData($hEdit, $em)
    ;~ Return
    ;~ EndIf
    ;~ EndIf
    ;~ _WinAPI_SetFilePointer($hFile, 0)
    ;~ _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $bytes, $nBytes)
    ;~ _WinAPI_CloseHandle($hFile)
    ;~ $data = BinaryToString(DllStructGetData($tBuffer, 1))
    ;~ $tBuffer = ""
    ;~ If Not $gui Then
    ;~ ConsoleWrite(@CRLF & $data & @CRLF & @CRLF)
    ;~ Else
    ;~ GUICtrlSetData($hEdit, $data)
    ;~ EndIf
    ;~ If $save Then
    ;~ $hSave = FileOpen($save, 2)
    ;~ If $hSave = -1 Then Exit ConsoleWrite(@CRLF & "ERROR: " & $save & " could not be created!" & @CRLF & @CRLF)
    ;~ FileWrite($hSave, $data)
    ;~ FileClose($hSave)
    ;~ EndIf
    ;~ EndFunc ;==>PrintHeadOfFile

    [/autoit] [autoit][/autoit] [autoit]

    Func CheckParameter()
    Local $i, $u = UBound($CmdLine)
    If $CmdLine[1] = "-v" Then Version()
    If $CmdLine[1] = "-h" Then Help()
    If $u = 2 Then Help()
    While $i < $u
    If $i < $u - 1 Then
    If $CmdLine[$i] = "-f" Then $filename = $CmdLine[$i + 1]
    If $CmdLine[$i] = "-b" Then $bytes = $CmdLine[$i + 1]
    If $CmdLine[$i] = "-s" Then $save = $CmdLine[$i + 1]
    EndIf
    If $CmdLine[$i] = "-g" Then $gui = True
    $i += 1
    WEnd
    If $bytes < 1 Then Help()
    If $filename = "" Then Exit ConsoleWrite(@CRLF & "ERROR: Value for parameter -f is missing!" & @CRLF & @CRLF)
    If Not FileExists($filename) Then Exit ConsoleWrite(@CRLF & "ERROR: " & $filename & " not found! Please check filename and try again." & @CRLF & @CRLF)
    $filename = _PathFull($filename)
    EndFunc ;==>CheckParameter

    [/autoit] [autoit][/autoit] [autoit]

    Func Help()
    ConsoleWrite(@CRLF & _
    "Usage:" & @TAB & _
    "Head -f <file to load> [-b <bytes>] [-s filename to save] [-g] [-h] [-v]" & @CRLF & @CRLF & @CRLF & _
    "Mandatory:" & @CRLF & @CRLF & _
    @TAB & "-f" & @TAB & "<file to open>" & @CRLF & @CRLF & _
    "Options:" & @CRLF & @CRLF & _
    @TAB & "-b" & @TAB & "Bytes to read from beginning of the file. Default is " & $bytes & "." & @CRLF & @TAB & @TAB & "Must be greater than 0 and smaller than 8MB!" & @CRLF & _
    @TAB & "-s" & @TAB & "Save output to a file. If file already exists it will be" & @CRLF & @TAB & @TAB & "overwritten without confirmation!" & @CRLF & _
    @TAB & "-g" & @TAB & "Enable GUI using commandline parameter" & @CRLF & _
    @TAB & "-h" & @TAB & "Displays this page" & @CRLF & _
    @TAB & "-v" & @TAB & "program version information" & @CRLF & @CRLF)
    Exit
    EndFunc ;==>Help

    [/autoit] [autoit][/autoit] [autoit]

    Func Version()
    ConsoleWrite(@CRLF & $name & $ver & $build & $coder & @CRLF & @CRLF & @CRLF)
    Exit
    EndFunc ;==>Version

    [/autoit] [autoit][/autoit] [autoit]

    Func _WinAPI_GetParentProcess($PID = 0) ;from WinAPIEx.au3 by Yashied
    If Not $PID Then
    $PID = _WinAPI_GetCurrentProcessID()
    If Not $PID Then
    Return SetError(1, 0, 0)
    EndIf
    EndIf
    Local $hSnapshot = DllCall('kernel32.dll', 'ptr', 'CreateToolhelp32Snapshot', 'dword', 0x00000002, 'dword', 0)
    If (@error) Or (Not $hSnapshot[0]) Then
    Return SetError(1, 0, 0)
    EndIf
    Local $tPROCESSENTRY32 = DllStructCreate('dword Size;dword Usage;dword ProcessID;ulong_ptr DefaultHeapID;dword ModuleID;dword Threads;dword ParentProcessID;long PriClassBase;dword Flags;wchar ExeFile[260]')
    Local $pPROCESSENTRY32 = DllStructGetPtr($tPROCESSENTRY32)
    Local $Ret, $Result = 0
    $hSnapshot = $hSnapshot[0]
    DllStructSetData($tPROCESSENTRY32, 'Size', DllStructGetSize($tPROCESSENTRY32))
    $Ret = DllCall('kernel32.dll', 'int', 'Process32FirstW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    While (Not @error) And ($Ret[0])
    If DllStructGetData($tPROCESSENTRY32, 'ProcessID') = $PID Then
    $Result = DllStructGetData($tPROCESSENTRY32, 'ParentProcessID')
    ExitLoop
    EndIf
    $Ret = DllCall('kernel32.dll', 'int', 'Process32NextW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    WEnd
    _WinAPI_CloseHandle($hSnapshot)
    If Not $Result Then
    Return SetError(1, 0, 0)
    EndIf
    Return $Result
    EndFunc ;==>_WinAPI_GetParentProcess

    [/autoit]

    Tail.au3

    Spoiler anzeigen
    [autoit]


    #AutoIt3Wrapper_Run_Obfuscator=y
    #Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0
    #AutoIt3Wrapper_UseX64=n
    #AutoIt3Wrapper_Change2CUI=y
    #AutoIt3Wrapper_Res_Description=Tail displays n bytes from the tail of a file
    #AutoIt3Wrapper_Res_Fileversion=0.6.5.0
    #AutoIt3Wrapper_Res_LegalCopyright=UEZ 2011
    #AutoIt3Wrapper_Res_Language=1033
    #AutoIt3Wrapper_Res_Field=Coded by|UEZ
    #AutoIt3Wrapper_Res_Field=Build|2011-03-15 beta
    #AutoIt3Wrapper_Res_Field=Compile date|%longdate% %time%
    #AutoIt3Wrapper_Res_Field=AutoIt Version|%AutoItVer%
    #AutoIt3Wrapper_UseUpx=n
    #AutoIt3Wrapper_UPX_Parameters=--ultra-brute --crp-ms=999999 --all-methods --all-filters
    #AutoIt3Wrapper_Run_After=del /f /q "%scriptdir%\%scriptfile%_Obfuscated.au3"
    #AutoIt3Wrapper_Run_After=Upack.exe %out% -c6 -f273 -red
    ;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

    [/autoit] [autoit][/autoit] [autoit]

    #include <EditConstants.au3>
    #include <File.au3>
    #include <FileConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <Misc.au3>
    #include <Process.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt("MustDeclareVars", 1)

    [/autoit] [autoit][/autoit] [autoit]

    Global Const $name = "Tail ", $ver = "v0.65 ", $build = "Build 2011-03-15 beta ", $coder = "by UEZ"
    Global $filename, $hFile, $hGUI, $hEdit, $i, $var, $bytes = 512, $gui = False, $save = False, $cont = False, $go = True
    Global $fsize_old, $fsize_new

    [/autoit] [autoit][/autoit] [autoit]

    If _ProcessGetName(_WinAPI_GetParentProcess(@AutoItPID)) = "cmd.exe" Then
    If Not $CmdLine[0] Then Help()
    CheckParameter()
    Else
    $var = WinList()
    For $i = 1 To $var[0][0]
    If $var[$i][0] <> "" And BitAND(WinGetState($var[$i][1]), 2) Then
    If WinGetProcess($var[$i][0], "") = @AutoItPID Then
    WinSetState($var[$i][1], "", @SW_HIDE)
    ExitLoop
    EndIf
    EndIf
    Next
    GUI(0)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $gui Then
    GUI($filename)
    Else
    PrintTailOfFile($filename)
    EndIf
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    Func GUI($filename)
    Local $nMsg
    If Not $filename Then
    $filename = FileOpenDialog("Select a file", "", "All Files (*.*)")
    If @error Then Exit MsgBox(16, "Error", "No file selected! Closing...", 10)
    Else
    If Not FileExists($filename) Then Exit MsgBox(16, "ERROR ", $filename & " not found! Please check filename and try again.")
    EndIf
    $hGUI = GUICreate($name & $ver & $build & $coder, 800, 600)
    $hEdit = GUICtrlCreateEdit("", 0, 0, 800, 600, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_HSCROLL, $WS_VSCROLL))
    GUICtrlSendMsg($hEdit, $EM_LIMITTEXT, -1, 0)
    GUICtrlSetFont($hEdit, 12, 400, 0, "Courier New")
    GUICtrlSetBkColor($hEdit, 0xFFFFFF)
    GUISetState(@SW_SHOW)
    $gui = True
    PrintTailOfFile($filename)
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd
    EndFunc ;==>GUI

    [/autoit] [autoit][/autoit] [autoit]

    Func PrintTailOfFile($filename)
    Local $pos, $data, $em, $hSave
    Local Const $fsize = FileGetSize($filename)
    If FileGetSize($filename) < $bytes Then $bytes = $fsize
    If $bytes > 0x7FFFFF Then $bytes = 512
    $hFile = FileOpen($filename)
    If $hFile = -1 Then
    $em = "ERROR: unable to open file. The file may has been locked by another process."
    PrintError($em)
    EndIf
    $pos = FileSetPos($hFile, -$bytes, $FILE_END)
    If Not $pos Then
    $em = "ERROR: unable to set file position."
    PrintError($em)
    EndIf
    $data = FileRead($hFile)
    If Not $gui Then
    If Not $cont Then
    ConsoleWrite(@CRLF & $data & @CRLF & @CRLF)
    Else
    ConsoleWrite(@CRLF & $data)
    EndIf
    Else
    GUICtrlSetData($hEdit, $data)
    EndIf
    If $cont Then Cont()
    FileClose($hFile)
    If $save And Not $cont Then
    $hSave = FileOpen($save, 2)
    If $hSave = -1 Then Exit ConsoleWrite(@CRLF & "ERROR: " & $save & " could not be created!" & @CRLF & @CRLF)
    FileWrite($hSave, $data)
    FileClose($hSave)
    EndIf
    $data = ""
    EndFunc ;==>PrintTailOfFile

    [/autoit] [autoit][/autoit] [autoit]

    Func PrintError($em)
    If Not $gui Then
    ConsoleWrite(@CRLF & $em & @CRLF & @CRLF)
    Exit
    Else
    GUICtrlSetData($hEdit, $em)
    Return
    EndIf
    EndFunc ;==>PrintError

    [/autoit] [autoit][/autoit] [autoit]

    Func Cont()
    Local $data, $pos, $dll = DllOpen("user32.dll")
    FileClose($hFile)
    $fsize_old = FileGetSize($filename)
    While Sleep(250) * $go
    $fsize_new = FileGetSize($filename)
    $hFile = FileOpen($filename)
    If $fsize_new > $fsize_old Then
    $pos = FileSetPos($hFile, -($fsize_new - $fsize_old - 1), $FILE_END)
    $fsize_old = $fsize_new
    $data = FileRead($hFile)
    If Not $gui Then
    ConsoleWrite($data)
    Else
    GUICtrlSetData($hEdit, $data, 1)
    EndIf
    EndIf
    FileClose($hFile)
    If _IsPressed("1B", $dll) Then $go = False
    WEnd
    DllClose($dll)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;~ Func PrintTailOfFile($filename)
    ;~ Local $nBytes, $data, $em, $hSave
    ;~ Local Const $fsize = FileGetSize($filename)
    ;~ If FileGetSize($filename) < $bytes Then $bytes = $fsize
    ;~ If $bytes > 0x7FFFFF Then $bytes = 512
    ;~ Local $tBuffer = DllStructCreate("byte[" & $bytes & "]")
    ;~ Local $hFile = _WinAPI_CreateFile($filename, 2, 7)
    ;~ If Not $hFile Then
    ;~ $em = "ERROR: unable to open file. The file may has been locked by another process."
    ;~ If Not $gui Then
    ;~ ConsoleWrite(@CRLF & $em & @CRLF & @CRLF)
    ;~ Exit
    ;~ Else
    ;~ GUICtrlSetData($hEdit, $em)
    ;~ Return
    ;~ EndIf
    ;~ EndIf
    ;~ _WinAPI_SetFilePointer($hFile, -$bytes, 2)
    ;~ _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $bytes, $nBytes)
    ;~ _WinAPI_CloseHandle($hFile)
    ;~ $data = BinaryToString(DllStructGetData($tBuffer, 1))
    ;~ $tBuffer = ""
    ;~ If Not $gui Then
    ;~ ConsoleWrite(@CRLF & $data & @CRLF & @CRLF)
    ;~ Else
    ;~ GUICtrlSetData($hEdit, $data)
    ;~ EndIf
    ;~ If $save Then
    ;~ $hSave = FileOpen($save, 2)
    ;~ If $hSave = -1 Then Exit ConsoleWrite(@CRLF & "ERROR: " & $save & " could not be created!" & @CRLF & @CRLF)
    ;~ FileWrite($hSave, $data)
    ;~ FileClose($hSave)
    ;~ EndIf
    ;~ EndFunc ;==>PrintTailOfFile

    [/autoit] [autoit][/autoit] [autoit]

    Func CheckParameter()
    Local $i, $u = UBound($CmdLine)
    If $CmdLine[1] = "-v" Then Version()
    If $CmdLine[1] = "-h" Then Help()
    If $u = 2 Then Help()
    While $i < $u
    If $i < $u - 1 Then
    If $CmdLine[$i] = "-f" Then $filename = $CmdLine[$i + 1]
    If $CmdLine[$i] = "-b" Then $bytes = $CmdLine[$i + 1]
    If $CmdLine[$i] = "-s" Then $save = $CmdLine[$i + 1]
    EndIf
    If $CmdLine[$i] = "-c" Then $cont = True
    If $CmdLine[$i] = "-g" Then $gui = True
    $i += 1
    WEnd
    If $bytes < 1 Then Help()
    If $filename = "" Then Exit ConsoleWrite(@CRLF & "ERROR: Value for parameter -f is missing!" & @CRLF & @CRLF)
    If Not FileExists($filename) Then Exit ConsoleWrite(@CRLF & "ERROR: " & $filename & " not found! Please check filename and try again." & @CRLF & @CRLF)
    $filename = _PathFull($filename)
    EndFunc ;==>CheckParameter

    [/autoit] [autoit][/autoit] [autoit]

    Func Help()
    ConsoleWrite(@CRLF & _
    "Usage:" & @TAB & _
    "Tail -f <file to load> [-b <bytes>] [-s filename to save]" & @CRLF & @TAB & " [-c] [-g] [-h] [-v]" & @CRLF & @CRLF & @CRLF & _
    "Mandatory:" & @CRLF & @CRLF & _
    @TAB & "-f" & @TAB & "<file to open>" & @CRLF & @CRLF & _
    "Options:" & @CRLF & @CRLF & _
    @TAB & "-b" & @TAB & "Bytes to read from end of the file. Default is " & $bytes & "." & @CRLF & @TAB & @TAB & "Must be greater than 0 and smaller than 8MB!" & @CRLF & _
    @TAB & "-s" & @TAB & "Save output to a file. If file already exists it will be" & @CRLF & @TAB & @TAB & "overwritten without confirmation!" & @CRLF & @TAB & @TAB & "Not working together with parameter -c!" & @CRLF & _
    @TAB & "-c" & @TAB & "Read file continuously" & @CRLF & _
    @TAB & "-g" & @TAB & "Enable GUI using commandline parameter" & @CRLF & _
    @TAB & "-h" & @TAB & "Displays this page" & @CRLF & _
    @TAB & "-v" & @TAB & "program version information" & @CRLF & @CRLF)
    Exit
    EndFunc ;==>Help

    [/autoit] [autoit][/autoit] [autoit]

    Func Version()
    ConsoleWrite(@CRLF & $name & $ver & $build & $coder & @CRLF & @CRLF & @CRLF)
    Exit
    EndFunc ;==>Version

    [/autoit] [autoit][/autoit] [autoit]

    Func _WinAPI_GetParentProcess($PID = 0) ;from WinAPIEx.au3 by Yashied
    If Not $PID Then
    $PID = _WinAPI_GetCurrentProcessID()
    If Not $PID Then Return SetError(1, 0, 0)
    EndIf
    Local $hSnapshot = DllCall('kernel32.dll', 'ptr', 'CreateToolhelp32Snapshot', 'dword', 0x00000002, 'dword', 0)
    If (@error) Or (Not $hSnapshot[0]) Then Return SetError(1, 0, 0)
    Local $tPROCESSENTRY32 = DllStructCreate('dword Size;dword Usage;dword ProcessID;ulong_ptr DefaultHeapID;dword ModuleID;dword Threads;dword ParentProcessID;long PriClassBase;dword Flags;wchar ExeFile[260]')
    Local $pPROCESSENTRY32 = DllStructGetPtr($tPROCESSENTRY32)
    Local $Ret, $Result = 0
    $hSnapshot = $hSnapshot[0]
    DllStructSetData($tPROCESSENTRY32, 'Size', DllStructGetSize($tPROCESSENTRY32))
    $Ret = DllCall('kernel32.dll', 'int', 'Process32FirstW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    While (Not @error) And ($Ret[0])
    If DllStructGetData($tPROCESSENTRY32, 'ProcessID') = $PID Then
    $Result = DllStructGetData($tPROCESSENTRY32, 'ParentProcessID')
    ExitLoop
    EndIf
    $Ret = DllCall('kernel32.dll', 'int', 'Process32NextW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    WEnd
    _WinAPI_CloseHandle($hSnapshot)
    If Not $Result Then Return SetError(1, 0, 0)
    Return $Result
    EndFunc ;==>_WinAPI_GetParentProcess

    [/autoit]

    Skripte + Exe Dateien als Download: autoit.de/wcf/attachment/12868/

    Die meisten werden Head und Tail nicht gebrauchen, aber vielleicht doch einige Wenige. :)

    Gruß,
    UEZ

  • Hallo UEZ,

    ich habe mir etwas Ähnliches aus schierer Verzweiflung auch schon gebastelt.

    [autoit]

    Local $hFile = _WinAPI_CreateFile($filename, 2, 2)

    [/autoit]


    Ich denke, Du solltest zumindest $iShare = 4 (Write) setzen, sonst kann es leicht zu Konflikten kommen, wenn das Programm der LOG-Datei noch läuft. Die UDF "verwurstelt" ja die Werte der Windowskonstanten.

  • Hallo UEZ,

    ich habe mir etwas Ähnliches aus schierer Verzweiflung auch schon gebastelt.

    [autoit]

    Local $hFile = _WinAPI_CreateFile($filename, 2, 2)

    [/autoit]


    Ich denke, Du solltest zumindest $iShare = 4 (Write) setzen, sonst kann es leicht zu Konflikten kommen, wenn das Programm der LOG-Datei noch läuft. Die UDF "verwurstelt" ja die Werte der Windowskonstanten.

    Vielen Dank für dein Feedback! :rolleyes:

    Ich verstehen aber den Nutzen von $iShare -> 4 (Sharing mode of an object: Write) nicht ganz. Denn ich öffnen "nur" die Datei, warum sollte ich den sharing mode auf 4 setzen?

    Mit dieser Methode kann ich leider keine gesperrten Dateien öffnen, egal ob $iShare 4 ist oder nicht! Ich weiß nicht, wie das Notepad macht, denn Notepad kann z.B. die Logs von Robocopy öffnen, obwohl Robocopy noch läuft!

    Vielleicht kannst du das mit dem $iShare Parameter näher erklären.

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Keine Ahnung! Die WinAPI Funktion habe ich zuerst gesehen!

    Kann denn FileSetPos() auch gesperrte Dateien öffnen (habe noch nicht getestet)?

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Ich weiß nicht, ob FileOpen das kann. FileSetPos ist nur das Äquivalent zu _WinAPI_SetFilePointer

  • Vielen Dank UEZ für die 2 Skripte. Hab mir mal die Hauptfunktion von Tail für mich passend umgeschrieben.

    Spoiler anzeigen
    [autoit]


    Func _GetTailOfFile($filename, $bytes = 512, $save = 0)
    Local $nBytes, $data, $em, $hSave
    Local Const $fsize = FileGetSize($filename)
    If FileGetSize($filename) < $bytes Then $bytes = $fsize
    If $bytes > 0x7FFFFF Then $bytes = 512
    Local $tBuffer = DllStructCreate("byte[" & $bytes & "]")
    Local $hFile = _WinAPI_CreateFile($filename, 2, 2)
    If Not $hFile Then
    $em = "ERROR: unable to open file. Might be open by another process."
    Return SetError(1, "", $em)
    EndIf
    _WinAPI_SetFilePointer($hFile, -$bytes, 2)
    _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $bytes, $nBytes)
    _WinAPI_CloseHandle($hFile)
    $data = BinaryToString(DllStructGetData($tBuffer, 1))
    $tBuffer = ""

    If $save Then
    $hSave = FileOpen($save, 2)
    If $hSave = -1 Then
    $em = "ERROR: " & $save & " could not be created!"
    Return SetError(1, "", $em)
    EndIf
    FileWrite($hSave, $data)
    FileClose($hSave)
    EndIf
    Return $data
    EndFunc ;==>GetTailOfFile

    [/autoit]
  • Moin UEZ,

    schau mal da: CreateFile

    Die Parameter dwDesiredAccess und dwShareMode regeln die Möglichkeiten für mehrere gleichzeitige Zugriffe auf eine Datei, soweit die Berechtigungen das überhaupt zulassen. Wenn z.B. eine Anwendung die Datei mit GENERIC_WRITE schreibend geöffnet hat und Du versuchst, sie mit GENERIC_READ lesend zu öffnen, brauchst Du zumindest den Sharemode FILE_SHARE_WRITE, sonst:

    Zitat

    If this flag is not specified, but the file or device has been opened for write access or has a file mapping with write access, the function fails.


    Wenn ich das MSDN richtig verstehe, muss allerdings auch die andere Anwendung im CreateFile zumindest den Sharemode FILE_SHARE_READ gesetzt haben, sonst:

    Zitat

    Otherwise, other processes cannot open the file or device if they request read access.


    In meinem Tool (nicht AU3) habe ich den Sharemode generell auf 0x7 (FILE_SHARE_READ + FILE_SHARE_WRITE + FILE_SHARE_DELETE) gesetzt. Es läuft immer mit Administratorrechten und kann so jederzeit auf die mich interessierenden Logdateien zugreifen, ohne dass sich die Anwendungen gegenseitig stören.

    Ob und ggf. welche Sharemodes die AU3-Funktion FileOpen() setzt, weiß ich nicht.

  • Ich habe auch ein _WinAPI_CloseHandle($hFile) beim Fehler Rücksprung vergessen -> zwischen Zeile 9 und 10 würde ich noch ein _WinAPI_CloseHandle($hFile) einbauen!

    Danke für die Quelle! Öffne ich die Datei mit _WinAPI_CreateFile($filename, 2, 4), so wird nichts angezeigt; mit _WinAPI_CreateFile($filename, 2, 7) geht's wieder. Jetzt muss ich testen, ob ich das File öffnen kann, wärend Robocopy fleißig in die Datei schreibt.

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Hallo UEZ,

    ich habe mein oben erwähntes Tool jetzt doch noch nach AU3 portiert, u.a. weil ich wissen wollte, ob es mit den "normalen" File-Funktionen geht. Für die mich interessierenden Logfiles unter Windows Server 2003 (32 Bit) klappt das prächtig:

    Tail.au3
    [autoit]

    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_UseUpx=n
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    ; ===============================================================================================================================
    #include <GuiConstantsEx.au3>
    #include <EditConstants.au3>
    #include <WindowsConstants.au3>
    ; ===============================================================================================================================
    Global Const $INI = @ScriptDir & "\Tail.ini"
    Global Const $W = 750
    Global Const $H = 500
    Global Const $MinW = 600
    Global Const $MinH = 400
    ; ===============================================================================================================================
    Global $LogFile = ""
    Global $LogPath = IniRead($INI, "Tail", "Path", "")
    Global $MaxBytes = 1024
    $aM = DllCall("User32.dll", "Int", "GetSystemMetrics", "Int", 15) ; SM_CYMENU = 15
    $DH = 20 + $aM[0]
    ; ===============================================================================================================================
    Opt("GUIOnEventMode", 1)
    Opt("GUICloseOnESC", 0)
    Opt("GUIResizeMode", BitOR($GUI_DOCKSIZE, $GUI_DOCKLEFT, $GUI_DOCKBOTTOM))
    $hGUI = GUICreate("Tail", $W, $H, -1, -1, BitOr($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_MAXIMIZEBOX))
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $idFileMenu = GUICtrlCreateMenu("&File")
    $idOpen = GUICtrlCreateMenuItem("&Open", $idFileMenu)
    GUICtrlSetOnEvent(-1, "_Open")
    $idRead = GUICtrlCreateMenuItem("&Read", $idFileMenu)
    GUICtrlSetOnEvent(-1, "_Read")
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlCreateMenuItem("", $idFileMenu)
    $idExit = GUICtrlCreateMenuItem("&Exit", $idFileMenu)
    GUICtrlSetOnEvent(-1, "_Exit")
    $idEditMenu = GUICtrlCreateMenu("&Edit")
    $idClear = GUICtrlCreateMenuItem("&Clear", $idEditMenu)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlSetOnEvent(-1, "_Clear")
    $idSave = GUICtrlCreateMenuItem("&Save", $idEditMenu)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlSetOnEvent(-1, "_Save")
    $idTailMenu = GUICtrlCreateMenu("&Tail")
    $idStart = GUICtrlCreateMenuItem("&Start", $idTailMenu)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlSetOnEvent(-1, "_StartStop")
    $idBytesMenu = GUICtrlCreateMenu("&Bytes")
    $idBytes = GUICtrlCreateMenuItem($MaxBytes, $idBytesMenu)
    GUICtrlSetOnEvent(-1, "_Bytes")
    $idEdit = GUICtrlCreateEdit("", 0, 0, $W, $H - $DH, BitOR($GUI_SS_DEFAULT_EDIT, $ES_READONLY))
    GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
    GUICtrlSetColor(-1, 0x000080)
    GUICtrlSetBkColor(-1, 0xFFFFFF)
    GUICtrlSetFont(-1, 9, -1, -1, "Courier New")
    GUICtrlSendMsg(-1, $EM_LIMITTEXT, 0, 0)
    $idStatus = GUICtrlCreateInput("", 0, $H - $DH, $W, 20, $ES_READONLY)
    GUICtrlSetResizing(-1, BitOR($GUI_DOCKLEFT, $GUI_DOCKRIGHT, $GUI_DOCKBOTTOM, $GUI_DOCKHEIGHT))
    $idDummy = GUICtrlCreateDummy()
    GUICtrlSetOnEvent(-1, "_Update")
    GUIRegisterMsg($WM_GETMINMAXINFO, "_MinInfo")
    GUISetState()
    While True
    Sleep(1000)
    WEnd
    ; ===============================================================================================================================
    ; GUI & menu functions
    ; ===============================================================================================================================
    Func _Exit()
    AdlibUnRegister("_Tail_AdLib")
    GUIDelete()
    Sleep(250)
    Exit
    EndFunc
    ; ===============================================================================================================================
    Func _MinInfo($hWnd, $iMsg, $wParam, $lParam)
    Local $MINMAXINFO = DllStructCreate("LONG[10]", $lParam)
    DllStructSetData($MINMAXINFO, 1, $MinW, 7)
    DllStructSetData($MINMAXINFO, 1, $MinH, 8)
    Return $GUI_RUNDEFMSG
    EndFunc
    ; ===============================================================================================================================
    Func _Update()
    Local $Tail = _Tail_Content()
    GUICtrlSendMsg($idEdit, $EM_SETSEL, -2, -1)
    GUICtrlSetData($idEdit, $Tail, 1)
    GUICtrlSetData($idStatus, StringLen($Tail) & " bytes read")
    EndFunc
    ; ===============================================================================================================================
    Func _Open()
    GUICtrlSetData($idStatus, "")
    Local $File, $Pos
    $File = FileOpenDialog( "Tail - Open File", $LogPath, "Text (*.*)", 3, "" , $hGUI)
    If @error Then Return
    $Pos = StringInStr($File, "\", 0, -1)
    $LogPath = StringLeft($File, $Pos)
    IniWrite($INI, "Tail", "Path", $LogPath)
    _Clear()
    GUICtrlSetState($idRead, $GUI_ENABLE)
    GUICtrlSetState($idClear, $GUI_ENABLE)
    GUICtrlSetState($idSave, $GUI_ENABLE)
    GUICtrlSetState($idStart, $GUI_ENABLE)
    WinSetTitle($hGUI, "", "Tail - " & $File)
    $LogFile = $File
    _Tail($LogFile, $MaxBytes)
    EndFunc
    ; ===============================================================================================================================
    Func _Read()
    _Clear()
    _Tail($LogFile, $MaxBytes)
    EndFunc
    ; ===============================================================================================================================
    Func _Clear()
    GUICtrlSetData($idStatus, "")
    GUICtrlSetData($idEdit, "")
    EndFunc
    ; ===============================================================================================================================
    Func _Save()
    GUICtrlSetData($idStatus, "")
    Local $Sel, $File, $Handle, $Now
    $Sel = ControlCommand($hGUI, "", $idEdit, "GetSelected")
    If Not $Sel Then Return GUICtrlSetData($idStatus, "ERROR: Save - No text selected!")
    $File = FileSaveDialog("Tail - Append Selection to File", @ScriptDir, "Text (*.txt)", 0, "Log.txt", $hGUI)
    If @error Then Return
    $Handle = FileOpen($File, 1)
    If $Handle = -1 Then Return GUICtrlSetData($idStatus, "ERROR: Save - Cannot open file for writing!")
    $Now = StringFormat("%04d-%02d-%02d %02d:%02d:%02d > ", @YEAR, @MON, @MDAY, @HOUR, @MIN, @SEC)
    FileWrite($Handle, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" & @CRLF)
    FileWrite($Handle, $Now & $LogFile & @CRLF)
    FileWrite($Handle, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" & @CRLF)
    FileWrite($Handle, $Sel & @CRLF)
    FileClose($Handle)
    GUICtrlSetData($idStatus, "Save: " & StringLen($Sel) & " chars saved to file " & $File)
    EndFunc
    ; ===============================================================================================================================
    Func _StartStop()
    GUICtrlSetData($idStatus, "")
    If GUICtrlRead($idStart, 1) = "&Start" Then
    AdlibRegister("_Tail_AdLib", 500)
    GUICtrlSetData($idStart, "&Stop")
    $State = $GUI_DISABLE
    Else
    AdlibUnRegister("_Tail_AdLib")
    GUICtrlSetData($idStart, "&Start")
    $State = $GUI_ENABLE
    EndIf
    GUICtrlSetState($idOpen, $State)
    GUICtrlSetState($idRead, $State)
    GUICtrlSetState($idExit, $State)
    GUICtrlSetState($idClear, $State)
    GUICtrlSetState($idSave, $State)
    GUICtrlSetState($idBytes, $State)
    EndFunc
    ; ===============================================================================================================================
    Func _Bytes()
    Opt("GUIOnEventMode", 0)
    GUISetState(@SW_DISABLE, $hGUI)
    Local $hBytesGUI = Guicreate("Tail - Bytes", 300, 125, -1, -1, -1, -1, $hGUI)
    GUICtrlCreateLabel("Initial number of bytes (max. 999999):", 20, 20, 260, 20)
    Local $idInp = GUICtrlCreateInput($MaxBytes, 20, 40, 260, 20, $ES_NUMBER)
    GUICtrlSetLimit(-1, 6)
    Local $idBtn = GUICtrlCreateButton("OK", 20, 80, 260, 25)
    GUICtrlSetState(-1, $GUI_DEFBUTTON)
    GUISetState()
    While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    ExitLoop
    Case $idBtn
    $MaxBytes = GUICtrlRead($idInp)
    GUICtrlSetData($idBytes, $MaxBytes)
    ExitLoop
    EndSwitch
    WEnd
    Opt("GUIOnEventMode", 1)
    GUISetState(@SW_ENABLE, $hGUI)
    GUIDelete($hBytesGui)
    EndFunc
    ; ===============================================================================================================================
    ; Tail functions
    ; ===============================================================================================================================
    Func _Tail_AdLib()
    _Tail()
    EndFunc
    ; ===============================================================================================================================
    Func _Tail_Content($bSet = False, $sNew = "")
    Local Static $Tail = ""
    If $bSet Then $Tail = $sNew
    Return $Tail
    EndFunc
    ; ===============================================================================================================================
    Func _Tail($sFile = "", $nBytes = 1024)
    Local Static $TailPos = 0
    Local Static $TailFile = ""
    Local $TailNew = ""
    If $sFile Then
    $TailFile = $sFile
    $TailPos = 0
    EndIf
    Local $Handle = FileOpen($TailFile)
    If $Handle = -1 Then Return
    FileSetPos($Handle, 0, 2)
    Local $Size = FileGetPos($Handle)
    If $TailPos = 0 Then
    If $Size > $nBytes Then
    $TailPos = $Size - $nBytes
    EndIf
    ElseIf $Size < $TailPos Then
    $TailPos = $Size
    EndIf
    If $Size > $TailPos Then
    FileSetPos($Handle, $TailPos, 0)
    $TailNew = FileRead($Handle)
    $TailPos = FileGetPos($Handle)
    EndIf
    FileClose($Handle)
    If $TailNew Then
    _Tail_Content(True, $TailNew)
    GUICtrlSendToDummy($idDummy)
    EndIf
    EndFunc

    [/autoit]


    Edit: Tags im Spoiler korrigiert. :whistling:

  • Hallo Großvater,

    vielen Dank für deine Version. Momentan liege ich wegen einer Grippe im Bett und werde deine Version sobald wie möglich testen, ob ich die Logfiles, die von Robocopy erstellt und beschrieben werden, während der Laufzeit anzeigen kann.

    Die WindowsUpdate.log wird schon mal angezeigt, nach dem ich wuauclt /detectnow gestartet habe.

    Bis denne,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Ich hätte hier auch zwei Funktionen, um Head und Tail zu lesen:

    Spoiler anzeigen
    [autoit]

    Func _FileReadTail($sFile, $iLen=512, $iEncoding=0)
    ; Author: ProgAndy
    If $iEncoding < 0 Or $iEncoding > 4 Then Return SetError(1,0,Binary(''))
    If $iLen < 1 Then Return SetError(2,0,Binary(''))
    Local $hFile = FileOpen($sFile, 16)
    If @error Then Return SetError(3,@error,0)
    If Not FileSetPos($hFile, -$iLen, 2) Then
    FileClose($hFile)
    SetError(4,0,Binary(''))
    EndIf
    Local $bResult = FileRead($hFile)
    If @error Then
    FileClose($hFile)
    Return SetError(5,0,Binary(''))
    EndIf
    FileClose($hFile)
    If $iEncoding Then Return BinaryToString($bResult, $iEncoding)
    Return $bResult
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _FileReadHead($sFile, $iLen=512, $iEncoding=0)
    ; Author: ProgAndy
    If $iEncoding < 0 Or $iEncoding > 4 Then Return SetError(1,0,Binary(''))
    If $iLen < 0 Then Return SetError(2,0,Binary(''))
    Local $hFile = FileOpen($sFile, 16)
    If @error Then Return SetError(3,@error,0)
    If $iLen = 0 Then
    Local $bResult = FileRead($hFile)
    Else
    Local $bResult = FileRead($hFile, $iLen)
    EndIf
    If @error Then
    FileClose($hFile)
    Return SetError(4,0,Binary(''))
    EndIf
    FileClose($hFile)
    If $iEncoding Then Return BinaryToString($bResult, $iEncoding)
    Return $bResult
    EndFunc

    [/autoit]