Sign in to follow this  
Followers 0
martin

Find string in files

7 posts in this topic

#1 ·  Posted (edited)

The Find in Files Search tools in SciTE is very useful but sometimes has an annoying limitation. It uses the windows program findstr.exe to search for the string which only recognises @CRLF as the end of a line. So if you have files you want to search which use only @CR then this the whole file will be shown instead of the single line where the string is found.

This makes the search slow because most of the time is taken up writing the lines to the output pane, and even longer is spent by the user trying to identify where the string was found.

This script gets round that limitation, and also allows you to search more than one file type.

To replace the SciTE "Find in Files" option from the Search menu

Compile this script to findstr.exe and put it in the @ProgramDir\AutoIt3\SciTE folder then it will use the parameters you type into the Find in Files dialog from the SciTE Search menu. Or, if you leave any of the fields blank it will use the gui in the script so you can also choose case sensitivity and whether to search subfolders.

Since very few files use only @CR for line ends this script has limited appeal, but it solved a problem for me.

findstring2.au3

Icon used FindStr2.ico(Improved 10th Dec)

History

2.1 - fixed '\\' in search path error. Improved text to output pane

2.2 fixed error not saving last search folder. Converted body of script into Main function.

2.3 can now turn regexp on or off

2.4 fix problem when files found not using RegExp but then not shown. Redesigned gui.

2.5 fix error when search button pressed with an empty field

2.6 correct regexp for start of word and end of word ("\<" and "\>") matches

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Looks nice and would like to add it to the standard installer.

Couple of suggestions:

Change the Systray Icon.

Add option to systray menu to stop the findstr process (also killing the background findstr.exe program)

Thanks

Jos

edit:

I added this to the script to be able to cancel the search:

Func OnAutoItExit()
    ProgressOff()
    If @ExitMethod Then
        ConsoleWrite(@CRLF & "! search cancelled." & @CRLF)
        $aP = ProcessList("findstr.exe")
    ; Kill all FindStr.exe that are not this programs PID
        For $i = 1 To $aP[0][0]
            If $aP[$i][1] <> @AutoItPID Then ProcessClose($aP[$i][1])
        Next
    EndIf
    FileDelete(@ScriptDir & "findstrtmp.txt");remove temp file
EndFunc  ;==>OnAutoItExit
Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Looks nice and would like to add it to the standard installer.

Couple of suggestions:

Change the Systray Icon.

Add option to systray menu to stop the findstr process (also killing the background findstr.exe program)

Thanks

Jos

edit:

I added this to the script to be able to cancel the search:

Func OnAutoItExit()
    ProgressOff()
    If @ExitMethod Then
        ConsoleWrite(@CRLF & "! search cancelled." & @CRLF)
        $aP = ProcessList("findstr.exe")
; Kill all FindStr.exe that are not this programs PID
        For $i = 1 To $aP[0][0]
            If $aP[$i][1] <> @AutoItPID Then ProcessClose($aP[$i][1])
        Next
    EndIf
    FileDelete(@ScriptDir & "findstrtmp.txt");remove temp file
EndFunc ;==>OnAutoItExit
Thanks Jos.

It took me by surpise that you were interested in it so now I'm feeling guilty that I didn't take more time over it. :) Maybe I'll find some time over Christmas.

@ExitMethod seems to be > 0 even when the script closes naturally so I used my own variable.

I have made the changes you suggested and a few other improvements.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Thanks Jos.

It took me by surpise that you were interested in it so now I'm feeling guilty that I didn't take more time over it. :) Maybe I'll find some time over Christmas.

@ExitMethod seems to be > 0 even when the script closes naturally so I used my own variable.

I have made the changes you suggested and a few other improvements.

I am always open for making things better and one of the things I like is the ability to stop the scan for files in a "simpler" way.

The reason is that you have Exit in the script which will set @ExitMethod to 1.

So either test for "> 1" or remove the Exit statement.

Thanks,

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I am always open for making things better and one of the things I like is the ability to stop the scan for files in a "simpler" way.

The reason is that you have Exit in the script which will set @ExitMethod to 1.

So either test for "> 1" or remove the Exit statement.

Thanks,

Jos

I see, I forgot that was there. Now removed and back to using @exitMethod. Thanks again.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Updated so can control whether or not RegExpr used in search. The regexp is restricted to what WIndows findstr.exe can handle.

Any volunteers to test this?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Just noticed I totally missed your last post here.... sorry.

When compiling and running it from SciTE it still shows the Menu. shouldn't it automatically continue when ran from SciTE by schanging this portion of code:

If $CmdLine[0] >= 5 Then; we have been run from SciTE?  find.command=findstr /n /s /I $(find.what) $(find.files)
        $LineNumbers = $CmdLine[1] <> "/n"
        $subFolders = $CmdLine[2] = "/s"
        $CaseSensitive = $CmdLine[3] <> "/I"
        $tofind = $CmdLine[4]
        $FileTypes = $CmdLine[5]
        If $CmdLine[0] >= 6 Then
            $SearchFolder = $CmdLine[6]
        Else
            $SearchFolder = @WorkingDir
        EndIf
        
    Else

        While 1
            $fields = DataFromGui()
            If IsArray($fields) Then
                $tofind = $fields[0]
                $FileTypes = $fields[1]
                $SearchFolder = $fields[2]
                $CaseSensitive = $fields[3]
                $subFolders = $fields[4]
                $LineNumbers = $fields[5]
                $useRegExp = $fields[6]
                ExitLoop
            Else
                Exit
            EndIf
        WEnd
    EndIf

EDIT: It doesn't find anything when I made this change.

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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