Speaker Posted April 3, 2006 Share Posted April 3, 2006 Hi,Below is an AutoIt script I have written to make it easier to use GREP. (For those who are not familiar with this program: it is a utility to find strings in a bunch of files, real quick. It comes from Unix. It is a great tool for programmers.) GREP needs command line parameters to work, and it's a pain in the neck to type them every time. Output should be redirected to a file which you can then open in Notepad -- more typing or clicking.The script below does everything automatically, options can be specified in a nice GUI by just a few mouse clicks. Search strings can be entered by the Copy/Paste mechanism of Windows -- no more typing errors. The script needs a GREP version which has been compiled as a console mode Win32 program. I use GREP from the UnixUtils package, it works fine. Search results are displayed using the NOTEPAD replacement WIN32PAD which is a much better editor than NOTEPAD and can properly handle Unix-style text lines (only LF at the end). Both these programs are included in the complete string search package which can be downloaded from my WEB site:http://invitel.hu/kapla/prog_files.htmlThe script:expandcollapse popup; String Search ; ; AutoIt Version: 3.0 ; Language: English ; Platform: Win9x/NT ; Author: Laszlo Menczel (menczel@invitel.hu) ; Date: 4/3/2006 ; ; Allows the user to enter an arbitrary search string, then runs GREP to do the search ; and write the result to a text file. The name of file is automatically constructed ; by prefixing the first substring in the search pattern by '0ss_' (a different name ; can also be specified, see below). No extension is added to the filename. ; ; The start directory for search is the current working directory. The result file is ; also written to the current working directory unless explicitly specified otherwise. ; ; The following options can be specified in the dialog box: ; ; - use the search string as a regular exp[b][/b]ression ; - do a recursive search including subdirectories ; - add line numbers to the result ; - number of context lines (lines before and after the matching one) shown ; - specify the name of output file ; - show the results in a text editor window after GREP finished ;-------------------------------- GUI elements --------------------------------- #include <GUIConstants.au3> Opt("ExpandVarStrings", 1) Opt("MouseCoordMode", 2) Opt("RunErrorsFatal", 0) ;---------- main window GUICreate("String Search using GREP", 440, 350, -1, -1, -1) GUISetState(@SW_SHOW) ;---------- controls for specifying the search pattern GUICtrlCreateLabel("Search pattern", 20, 20, 80, 20) $pattern_inp = GUICtrlCreateInput("", 20, 40, 400, 20) ;---------- controls for search option settings GUICtrlCreateLabel("Search options", 20, 100, 80, 20) $regex_check = GUICtrlCreateCheckbox("regular exp[b][/b]ression", 20, 120, 120, 20) $subdir_check = GUICtrlCreateCheckbox("search subdirectories", 160, 120, 120, 20) $linenum_check = GUICtrlCreateCheckbox("show line numbers", 300, 120, 120, 20) $context_inp = GUICtrlCreateInput ("0", 20, 150, 50, 20) $context_adj = GUICtrlCreateUpdown($context_inp) GUICtrlCreateLabel("number of context lines", 80, 153, 150, 20) ;---------- controls for specifying the oputput file GUICtrlCreateLabel("Output file", 20, 200, 100, 20) $outfile_inp = GUICtrlCreateInput("", 20, 220, 200, 20) GUICtrlCreateGroup ("", 230, 210, 200, 35) $auto_rad = GUICtrlCreateRadio ("auto", 245, 220, 55, 20) $default_rad = GUICtrlCreateRadio ("default", 300, 220, 60, 20) $specify_rad = GUICtrlCreateRadio ("specify", 360, 220, 60, 20) GUICtrlCreateGroup ("", -99, -99, 1, 1) ;---------- controls for running the program GUICtrlCreateLabel("Display options", 20, 280, 100, 20) $display_check = GUICtrlCreateCheckbox("show result in a window", 20, 300, 150, 20) $start_but = GUICtrlCreateButton("Start", 280, 285, 60, 30) $cancel_but = GUICtrlCreateButton("Cancel", 360, 285, 60, 30) ;---------- control state initialization GUICtrlSetLimit($context_adj, 20, 0) GUICtrlSetState($linenum_check, $GUI_CHECKED) GUICtrlSetState($display_check, $GUI_CHECKED) GUICtrlSetData($outfile_inp, "0ss_") GUICtrlSetState($auto_rad, $GUI_CHECKED) GUICtrlSetState($pattern_inp, $GUI_FOCUS) GUICtrlSetState($outfile_inp, $GUI_DISABLE) ;-------------------------------- Functions --------------------------------------- ; UpdateOutFile($rad) ; ------------------- ; Sets the status & text of the filename input field appropriate ; for the situation when the radio button '$rad' is checked. Func UpdateOutFile($rad) Dim $pat, $tokens, $out Select Case $rad = $auto_rad $pat = GUICtrlRead($pattern_inp) $tokens = StringSplit($pat, " ") $out = StringFormat("0ss_%s", $tokens[1]) GUICtrlSetData($outfile_inp, $out) GUICtrlSetState($outfile_inp, $GUI_DISABLE) Case $rad = $default_rad GUICtrlSetData($outfile_inp, "0ss0") GUICtrlSetState($outfile_inp, $GUI_DISABLE) Case $rad = $specify_rad GUICtrlSetState($outfile_inp, $GUI_ENABLE) GUICtrlSetState($outfile_inp, $GUI_FOCUS) EndSelect EndFunc ; FocusToPatternInp($x, $y) ; ------------------------- ; Sets input focus to the pattern input field, simulates a mouse click ; to make the text inside the field unselected, then returns the mouse ; cursor to the coordinate [$x,$y]. Func FocusToPatternInp($x, $y) GUICtrlSetState($pattern_inp, $GUI_FOCUS) MouseClick("left", 415, 45, 1, 0) MouseMove($x, $y, 0) EndFunc ; IsFilenameChar($chr) ; -------------------- ; Tests if the character '$chr' is present in the string '$filename_char' ; which contains legal filename characters not in the alphanumeric set. ; Returns 1 if yes, zero otherwise. $filename_char = ".-_!@#$%^&*()[]{}=+<>" Func IsFilenameChar($chr) Dim $i, $next $i = 1 While 1 $next = StringMid($filename_char, $i, 1) If $next = "" Then Return 0 Endif If $next = $chr Then Return 1 Endif $i = $i + 1 Wend EndFunc ;------------------------------- Main loop --------------------------------------- $pattern = GUICtrlRead($pattern_inp) $regex = GUICtrlRead($regex_check) $run = 0 While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $cancel_but ExitLoop Case $msg = $start_but If $pattern = "" Then MsgBox(0, "Error", "No search pattern") FocusToPatternInp(285, 290); Else $tmp = GUICtrlRead($outfile_inp) If $tmp = "" Then MsgBox(0, "Error", "No output file") FocusToPatternInp(285, 290); Else $run = 1 ExitLoop Endif Endif Case $msg = $regex_check $tmp = GUICtrlRead($regex_check) If $tmp <> $regex Then $regex = $tmp If $regex = $GUI_CHECKED Then GUICtrlSetState($auto_rad, $GUI_UNCHECKED) GUICtrlSetState($auto_rad, $GUI_DISABLE) GUICtrlSetState($specify_rad, $GUI_UNCHECKED) GUICtrlSetState($default_rad, $GUI_CHECKED) UpdateOutFile($default_rad) Else GUICtrlSetState($auto_rad, $GUI_ENABLE) GUICtrlSetState($default_rad, $GUI_UNCHECKED) GUICtrlSetState($specify_rad, $GUI_UNCHECKED) GUICtrlSetState($auto_rad, $GUI_CHECKED) UpdateOutFile($auto_rad) Endif FocusToPatternInp(25, 125) Endif Case $msg = $subdir_check FocusToPatternInp(165, 125) Case $msg = $linenum_check FocusToPatternInp(305, 125) Case $msg = $auto_rad UpdateOutFile($auto_rad) FocusToPatternInp(250, 225) Case $msg = $default_rad UpdateOutFile($default_rad) FocusToPatternInp(305, 225) Case $msg = $specify_rad UpdateOutFile($specify_rad) EndSelect $newpat = GUICtrlRead($pattern_inp) If $newpat <> $pattern Then $pattern = $newpat $tmp = GUICtrlRead($auto_rad) If $tmp = $GUI_CHECKED Then UpdateOutFile($auto_rad) Endif Endif Wend ;------------------------------- Program run ----------------------------------- If $run = 1 Then ;--------- clear option strings $r_str = "" $c_str = "" $s_str = "" $l_str = "" $o_str = "" ;--------- check control status & initialize option strings If $regex = $GUI_CHECKED Then $r_str = "-E " Endif $context = GUICtrlRead($context_adj) If $context > 0 Then $c_str = StringFormat("-C %d ", $context) Endif $subdir = GUICtrlRead($subdir_check) If $subdir = $GUI_CHECKED Then $s_str = "-r " Endif $linenum = GUICtrlRead($linenum_check) If $linenum = $GUI_CHECKED Then $l_str = "-n" Endif ;--------- replace illegal characters in output file name $outname = GUICtrlRead($outfile_inp) $pos = 1 While 1 $tmp = StringMid($outname, $pos, 1) If $tmp = "" Then ExitLoop Endif If StringIsAlnum($tmp) OR IsFilenameChar($tmp) Then $o_str = $o_str & $tmp Else $o_str = $o_str & "_" Endif $pos = $pos + 1 Wend ;---------- construct the command line and run GREP $cmd = StringFormat("grep.exe %s%s%s%s %s%s%s * > %s", $r_str, $c_str, $s_str, $l_str, '"', $pattern, '"', $o_str) RunWait(@ComSpec & " /c " & $cmd, "", @SW_HIDE) If @error = 1 Then MsgBox(0, "Error", "GREP did not run") Exit Endif ;---------- display result if requested $tmp = GUICtrlRead($display_check) If $tmp = $GUI_CHECKED Then $cmd = StringFormat("win32pad.exe %s", $o_str) RunWait(@ComSpec & " /c " & $cmd, "") If @error = 1 Then MsgBox(0, "Error", "Cannot run the editor") Exit Endif Endif Endif Exit 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