Jump to content

String


gius
 Share

Recommended Posts

Hello everyone,
I'm using this example

'?do=embed' frameborder='0' data-embedContent>>

in my example I search for the word "test" within each file in the folder:

#include <Array.au3>
#include <Constants.au3>
Example()
Func Example()

Local $aArray = _FindInFile('test', @ScriptDir)

_ArrayDisplay($aArray)

EndFunc

I would like to ask if I can
search for the word "test" and have as the result of _ArrayDisplay
the entire string in which is written the word "test"?

thank you

Link to comment
Share on other sites

The function uses the Windows tool FindStr. There is parameter "/X" which "Print lines that match exactly.". So you would need to create a customized version of _FindInFile.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You could also use something like this

#include <File.au3>
#include <Array.au3>

$folder = @ScriptDir
$ext = ".au3"
$word = "func"

; $var = FileSelectFolder("Choose", "", 0, "")
; If not @error Then $folder = $var
$aFiles = _FileListToArrayRec($folder, "*" & $ext, 1, 0, 0, 2)
$lines = ""
For $i = 1 to $aFiles[0]
   $content = FileRead($aFiles[$i])
  ; $res = StringRegExp($content, '(?im)(.*\b\Q' & $word & '\E\b.*)\R?', 3)
   $res = StringRegExp($content, '(?im)(.*\Q' & $word & '\E.*)\R?', 3)
   If IsArray($res) Then
         $lines &= $aFiles[$i] & @CRLF
         $lines &= _ArrayToString($res, @CRLF) & @CRLF
         $lines &= "-----------------------------------" & @CRLF
   EndIf
Next
ConsoleWrite($lines)
Link to comment
Share on other sites

Thanks Water for your reply,

I can not find the parameter  which "Print lines That match exactly."

You can help me?

 

thank you

It is not a parameter of _FindInFile but of FindStr which is used in _FindInFile.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks mikell for your help.

I have changed
ConsoleWrite ($lines)
with
_ArrayDisplay ($lines)
but nothing is displayed,
What I'm wrong?

it is possible to add two different Path

where to search?

$folder = @ScriptDir and  @MyDocumentsDir


thank you

Link to comment
Share on other sites

In the code $lines is a string, to display the results as an array you must first build the array from this string - StringSplit works nice for this

For the rest the code is detailed enough to allow you to easily modify it so that it fits your needs

#include <File.au3>
#include <Array.au3>

$folder = @ScriptDir
$ext = ".au3"
$word = "func"

; $var = FileSelectFolder("Choose", "", 0, "")
; If not @error Then $folder = $var
$aFiles = _FileListToArrayRec($folder, "*" & $ext, 1, 0, 0, 2)   ; list files
$lines = ""
For $i = 1 to $aFiles[0]
   $content = FileRead($aFiles[$i])
   $res = StringRegExp($content, '(?im)(.*\b\Q' & $word & '\E\b.*)\R?', 3)  ; if $word must be a lone word
  ; $res = StringRegExp($content, '(?im)(.*\Q' & $word & '\E.*)\R?', 3)    ; if $word can be part of another word
   If IsArray($res) Then
      ;   $lines &= $aFiles[$i] & @CRLF
         $lines &= _ArrayToString($res, @CRLF) & @CRLF
      ;   $lines &= "-----------------------------------" & @CRLF
   EndIf
Next
;ConsoleWrite($lines)

$array = StringSplit($lines, @crlf, 1)
_ArrayDisplay($array)
Link to comment
Share on other sites

Ha, for this you have to build a 2D array and in this case the approach is different

This code does all you want

#include <File.au3>
#include <Array.au3>

Global $folders[2] = [@ScriptDir, @MyDocumentsDir]
$ext = ".au3"     
$word = "func"   

Global $aResult[1000][2], $n = 0

For $k = 0 to UBound($folders)-1    ; loop through folders
  $aFiles = _FileListToArrayRec($folders[$k], "*" & $ext, 1, 0, 0, 2)   ; list files
  If not @error Then
    For $i = 1 to $aFiles[0]   ; loop through files
       $content = FileRead($aFiles[$i])
       ; the following regex captures the full lines containing $word
       $res = StringRegExp($content, '(?im)(.*\b\Q' & $word & '\E\b.*)\R?', 3)  ; if $word must be a lone word
      ; $res = StringRegExp($content, '(?im)(.*\Q' & $word & '\E.*)\R?', 3)    ; if $word can be part of another word
        If IsArray($res) Then
            $aResult[$n][0] = $aFiles[$i]    ; file path
            For $j = 0 to UBound($res)-1
                $aResult[$n+$j][1] = $res[$j]    ; lines 
            Next
            $n += $j
       EndIf
    Next
  EndIf
Next

Redim $aResult[$n][2]
_ArrayDisplay($aResult)
Link to comment
Share on other sites

Thanks again mikell.

I changed the parameter of _FileListToArrayRec

adding $FLTAR_RECUR (1) - Search in all subfolders

because I want to do the research text even in subfolders.

After a few minutes I have this error:

Error Allocating memory

I can fix this error?
With another method I can have the search also to subfolders?

thank you

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...