UEZ Posted February 10, 2011 Share Posted February 10, 2011 (edited) For the file migration we are using Robocopy and writing copy progress to a log file which will grow sometime to a huge file.To check the result overview which is written to the end of the log file you need to open the whole file just to check summery.Idea was to write a simple tool a la Unix/Linux which displays only the head or the tail of a file.Here my versions:Head.au3expandcollapse popup#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_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 Opt("MustDeclareVars", 1) #include <EditConstants.au3> #include <File.au3> #include <GUIConstantsEx.au3> #include <Process.au3> #include <WinAPI.au3> #include <WindowsConstants.au3> 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 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 If $gui Then GUI($filename) Else PrintHeadofFile($filename) EndIf Exit 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 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 ;~ 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 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 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 Func Version() ConsoleWrite(@CRLF & $name & $ver & $build & $coder & @CRLF & @CRLF & @CRLF) Exit EndFunc ;==>Version 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_GetParentProcessTail.au3expandcollapse popup#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_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 #include <EditConstants.au3> #include <File.au3> #include <FileConstants.au3> #include <GUIConstantsEx.au3> #include <Misc.au3> #include <Process.au3> #include <WinAPI.au3> #include <WindowsConstants.au3> Opt("MustDeclareVars", 1) 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 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 If $gui Then GUI($filename) Else PrintTailOfFile($filename) EndIf Exit 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 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 Func PrintError($em) If Not $gui Then ConsoleWrite(@CRLF & $em & @CRLF & @CRLF) Exit Else GUICtrlSetData($hEdit, $em) Return EndIf EndFunc ;==>PrintError 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 ;~ 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 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 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 Func Version() ConsoleWrite(@CRLF & $name & $ver & $build & $coder & @CRLF & @CRLF & @CRLF) Exit EndFunc ;==>Version 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_GetParentProcessDownload both source codes (27 downloads previously): Head-Tail v0.65 Build 2011-03-15 beta.7zDownload compiled executables only: Head-TailYou can use it in a CMD Box as a command line tool or GUI version.Here the command line parameters for Tail:Usage: Tail -f <file to load> [-b <bytes>] [-s filename to save] [-c] [-g] [-h] [-v] Mandatory: -f <file to open> Options: -b Bytes to read from end of the file. Default is 512. Must be greater than 0 and smaller than 8MB! -s Save output to a file. If file already exists it will be overwritten without confirmation! Not working together with parameter -c! -c Read file continuously -g Enable GUI using commandline parameter -h Displays this page -v program version informationLimitation: locked files cannot be opened!Maybe useful for someone. Br,UEZPS: thanks to Yashied for the _WinAPI_GetParentProcess() function taken from WinAPIEx.au3History:2011-02-10: release of Head-Tail2011-02-11: forgot to close file when an error has occurred during opening the file but that's not important because handle is always 02011-02-25: fixed some typos2011-03-12: changed from WinAPI file functions to default functions, added -c parameter (read file continuously)2011-03-15: -c parameter was not working properly - should be fixed now Edited March 15, 2011 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now