gius

String

10 posts in this topic

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks Water for your reply,
I can not find the parameter  which "Print lines That match exactly."
You can help me?

 

thank you

Edited by gius

Share this post


Link to post
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)

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites
 

thanks again mikell,
it is possible to even resulted in the Path of the file
in the next column?

I believe it is the result of

$lines &= $aFiles[$i] & @CRLF

thank you

Share this post


Link to post
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)

Share this post


Link to post
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

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