Jump to content
Jess

Open files and search for word

Recommended Posts

Jess

I am trying to open a number of files inside a folder and then search for a string inside every file. If the word is found/not found the result needs to be written in excel sheet in front of that particular filename. Can anyone help me write a script for that. 

Thank you in advance

Share this post


Link to post
Share on other sites
rootx

old-school CMD

FINDSTR /s /i yourtargetword *.* > 1.txt

with Autoit

Use _FileListToArrayRec to create a list of your file the use a loop for next to open every file one by one and search the string inside, at this point to save the output in a file readable by excell use csv format, use a simple separator like ";" to have your table.

Share this post


Link to post
Share on other sites
Jimwright

Give this a try, you'll need to edit the variables at the top to meet your needs, and it's probably not very performant:

#include <File.au3>

$Output="FileName, TextInFile" & @CRLF

$Folder="C:\test\"
$SearchText="MySearchString"
$SaveCSV=$Folder&"Result.csv"

$Files=_FileListToArray($Folder,"*.*",1)

For $i=1 to $Files[0]
   $Content=fileread($Folder&$Files[$i])
   if StringInStr($Content,$SearchText) Then
      $OutPut=$OutPut&$Files[$i]&","&"Yes"& @CRLF
   EndIf
Next

FileWrite($SaveCSV,$OutPut)

 

Share this post


Link to post
Share on other sites
kylomas

Jess,

This might help get you started...

#include <array.au3>
#include <File.au3>
#include <Excel.au3>

Local $path         = 'C:\K\Autoit\'    ; <---- your top level path
Local $search_word  = 'guiregistermsg'  ; <---- the string to search for
Local $out_str

Local $aFiles = _FileListToArrayRec($path, '*', 1, 1, 0, 2)
If @error Then Exit MsgBox(17, '', @extended)

For $1 = 1 To $aFiles[0]
    $out_str &= (StringRegExp(FileRead($aFiles[$1]), '(?i)' & $search_word) ? StringFormat('%10s\t', 'Found') : StringFormat('%10s\t', 'Not Found')) & $aFiles[$1] & ','
Next

$hfl = FileOpen(@ScriptDir & '\findtext10.txt', 2)
If $hfl = -1 Then Exit MsgBox(17, 'Error', 'Output file open failed')

FileWrite($hfl, $out_str)
FileClose($hfl)

Local $oExcel = _Excel_Open()
If @error Then Exit MsgBox(17, 'EXCEL OPEN ERROR', @extended)

Local $oBook = _Excel_BookNew($oExcel, 1)
If @error Then Exit MsgBox(17, 'EXCEL Book OPEN ERROR', @extended)

Local $aFinal = StringSplit(FileRead(@ScriptDir & '\findtext10.txt'), ',', 3)

_Excel_RangeWrite($oBook, $oBook.Activesheet, $aFinal)
If @error Then Exit MsgBox(17, 'EXCEL rangewrite ERROR', @extended)

_Excel_BookSaveAs($oBook, @ScriptDir & '\findtext10.xls', $xlhtml, True)
If @error Then Exit MsgBox(17, 'EXCEL booksave ERROR', @extended)

_Excel_Close($oExcel)
If @error Then Exit MsgBox(17, 'EXCEL close ERROR', @extended)

ShellExecute(@ScriptDir & '\findtext10.xls')

kylomas

edit: Note - filelisttoarrayrec is set to search all subfolders...change it to whatever your needs are

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
mikell

kylomas,
May I suggest :  StringRegExp(... , '(?i)\Q' & $search_word & '\E')
Useless in most cases, but... for a regex based search engine... who knows ?  :)

Share this post


Link to post
Share on other sites
kylomas

@Mikell - Thanks...given that we have no clue what the search argument might contain.

@Jess - Please note Mikell's correction.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.