Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

15,000 Word Docs Searched in 6 mins with OutPut :D


  • Please log in to reply
8 replies to this topic
AhkLearner
  • Members
  • 101 posts
  • Last active: Aug 27 2016 12:58 PM
  • Joined: 25 Feb 2014

Hi Community,

I have 100s of word documents (.doc [2003 version]), i need to search each of them in loop for a particular word, and collect that line in a word document.

Thanks in advance for your help and support. :clap:

 

CODE FOLLOWS:

#persistent
#SingleInstance Force
SetBatchLines, -1

FolderToSearchFor = c:\abc
OutPutFolder = c:\def
needle = %NeedleContent%
FileRecycle, %OutPutFolder%\FileName.txt
Loop, %FolderToSearchFor%\*.doc, , 1
{
    Loop, read, %A_LoopFileFullPath%
    {
        If A_LoopReadLine contains ><
        continue
        If A_LoopReadLine contains %needle%
        {
        FileAppend, %A_LoopReadLine%`n, %OutPutFolder%\FileName.txt
        }
    }
}

 



flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009

Would something like this work: (this retrieves a paragraph rather than a line).

needle := "particular word"
oWord := ComObjCreate("Word.Application") 

Loop, Files, %a_scriptdir%\*.doc                          ;or docx for more recent versions
	{
	if SubStr(A_LoopFileName,1,1) = "~"               ;in case there are open doc files
		continue
	oWord.Documents.Open(A_LoopFileFullPath)
	oword.Selection.WholeStory
	content := oword.selection.text
	loop, parse, content, `r, `n
		if InStr(a_loopfield, needle)
			list .= A_LoopFileFullPath " :`n`t" a_loopfield "`n`n"
	oword.activedocument.close(0)
	}
list := "Searching on " needle ":`n`n" list	
oWord.Documents.Add
oWord.visible := 1
oWord.ActiveDocument.Content.InsertAfter(list)


Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”


kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

My reply in your other thread: http://ahkscript.org...?p=46548#p46548



bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011

For 2003 I guess you need to replace .docx with .doc

wdApp := ComObjCreate("Word.Application")
wdApp.Visible := True

; Search options and constants
vbTrue := -1
wdDoNotSaveChanges := 0
wdExtend := 1
wdLine := 5
wdMove := 0
wdReplaceNone := 0

FindText := "Abc123"	; The word to search for
MatchCase := false
MatchWholeWord := vbTrue
Wrap := false
Replace := wdReplaceNone

Result := ""
Loop, %A_DeskTop%\*.docx	; The directory to look in for .docx files
{
	wdDoc := wdApp.Documents.Open(A_LoopFileLongPath)
	wdFind := wdApp.Selection.Find
	wdFind.ClearFormatting
	wdFind.Replacement.ClearFormatting
 
	; Execute the search - Find.Execute Method (Word)
	;	https://msdn.microsoft.com/en-us/library/office/ff193977.aspx
	while wdFind.Execute(FindText, MatchCase, MatchWholeWord,,,,, Wrap,,, Replace) {
		wdApp.Selection.HomeKey(wdLine, wdExtend)	; Extend the selectin to the start of the line
		wdApp.Selection.Expand(wdLine)				; Extend the selectin to the end of the line
		Result .= wdApp.Selection.Text "`r`n"		; Store the line text
 
		; Move the selection to the end of the line so the same line is not found by the next search.
		wdApp.Selection.EndKey(wdLine, wdMove)		
	}
	wdDoc.Close(wdDoNotSaveChanges)
}
wdApp.Quit(wdDoNotSaveChanges)
MsgBox, %Result%
return


AhkLearner
  • Members
  • 101 posts
  • Last active: Aug 27 2016 12:58 PM
  • Joined: 25 Feb 2014

Thanks a lot, i have some docs password protected and some corrupt docs, and for every one error is showing, I want to ignore error and just want final output.

thanks!

 

 

 

Would something like this work: (this retrieves a paragraph rather than a line).

needle := "particular word"
oWord := ComObjCreate("Word.Application") 

Loop, Files, %a_scriptdir%\*.doc                          ;or docx for more recent versions
	{
	if SubStr(A_LoopFileName,1,1) = "~"               ;in case there are open doc files
		continue
	oWord.Documents.Open(A_LoopFileFullPath)
	oword.Selection.WholeStory
	content := oword.selection.text
	loop, parse, content, `r, `n
		if InStr(a_loopfield, needle)
			list .= A_LoopFileFullPath " :`n`t" a_loopfield "`n`n"
	oword.activedocument.close(0)
	}
list := "Searching on " needle ":`n`n" list	
oWord.Documents.Add
oWord.visible := 1
oWord.ActiveDocument.Content.InsertAfter(list)



AhkLearner
  • Members
  • 101 posts
  • Last active: Aug 27 2016 12:58 PM
  • Joined: 25 Feb 2014

one more thing if the file is open in some other system (Docs located in Shared Drive) , this error is coming.

 

Thanks a lot, i have some docs password protected and some corrupt docs, and for every one error is showing, I want to ignore error and just want final output.

I need to bypass all the errors and get final output.

Thanks a lot in advance!

 

IMAGE: http://ahkscript.org...file.php?id=751



AhkLearner
  • Members
  • 101 posts
  • Last active: Aug 27 2016 12:58 PM
  • Joined: 25 Feb 2014

Results are Great :D

File in Use and other Error messages messing it up!



AhkLearner
  • Members
  • 101 posts
  • Last active: Aug 27 2016 12:58 PM
  • Joined: 25 Feb 2014

15,000 Word Docs Searched in 6 mins with OutPut :D

 

#persistent
#SingleInstance Force
SetBatchLines, -1

; ---------------------------------------------

FolderToSearchFor = c:\abc
OutPutFolder = c:\def
needle = %NeedleContent%
FileRecycle, %OutPutFolder%\FileName.txt
Loop, %FolderToSearchFor%\*.doc, , 1
{
    Loop, read, %A_LoopFileFullPath%
    {
        If A_LoopReadLine contains ><
        continue
        If A_LoopReadLine contains %needle%
        {
        FileAppend, %A_LoopReadLine%`n, %OutPutFolder%\FileName.txt
        }
    }
}

; ---------------------------------------------



AhkLearner
  • Members
  • 101 posts
  • Last active: Aug 27 2016 12:58 PM
  • Joined: 25 Feb 2014

Thanks flyingDman, kon, bruno for all your help and support !