Jump to content

Search PDF files for specific word and delete them


Recommended Posts

I have about 7000 PDF files that I need to go through and purge the ones with a specific word in them (Obsolete). Of course the PDF files are all compressed so a simple grep Obsolete will not work. If i use pdftotext, part of gnuwin32, I can convert the files to text and then perform the grep search. Some brief background this is a backup to the primary document repository so it will be run daily.

I looked on here last week and nothing jumped out and and said I have done that before. Below is what I have started, if someone has already done either what I am talking about, knows of a better way, please point me in the right direction.

This is what I have been working on so far:

$search = FileFindFirstFile("g:\test\*.pdf")

While 1

$file = FileFindNextFile($search)

If @error <> 1 Then

ConsoleWrite("c:\gnuwin32\bin\pdftotext.exe " & $file & " " & $file & ".txt" & @CRLF)

ConsoleWrite($file & @CRLF)

EndIf

WEnd

; Close the search handle

FileClose($search)

Link to post
Share on other sites

Your method is bad !

Try this Posted Image

#include <File.au3>

$search = FileFindFirstFile ( @DesktopDir & "\*.pdf") ;  "g:\test\*.pdf"
$_pdftotextPath = 'c:\gnuwin32\bin\pdftotext.exe'
$_OutPutFilepath = 'C:\file.txt'
_FileCreate ( $_OutPutFilepath )

While 1
    $file = FileFindNextFile ( $search )
    If @error Then ExitLoop
    ConsoleWrite ( '-->-- $file : ' & $file & @CRLF )
    $_RunWait = '"' & $_pdftotextPath & '" "' & @DesktopDir & '\' & $file & '" ' & $_OutPutFilepath
    ConsoleWrite ( '-->-- $_RunWait : ' & $_RunWait & @CRLF )
    RunWait ( $_RunWait, '', @SW_HIDE )
    ShellExecuteWait ( $_OutPutFilepath ) ; see the text content
    FileDelete ( $_OutPutFilepath )
WEnd

FileClose ( $search )

Edit : don't forget double quotes in paths.

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to post
Share on other sites

Edit : don't forget double quotes in paths.

... and in filenames! Better enclose the complete filepath in double quotes.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

I forget the string to find in pdf text, now it's working like you want ! Posted Image

$search = FileFindFirstFile ( @DesktopDir & "\*.pdf") ;  "g:\test\*.pdf"
$_pdftotextPath = 'c:\gnuwin32\bin\pdftotext.exe'
$_OutPutFilepath = 'C:\file.txt'
_FileCreate ( $_OutPutFilepath )
$_StringToFind = 'DavidFromLafayette'

While 1
    $file = FileFindNextFile ( $search )
    If @error Then ExitLoop
    ConsoleWrite ( '-->-- $file : ' & $file & @CRLF )
    $_RunWait = '"' & $_pdftotextPath & '" "' & @DesktopDir & '\' & $file & '" ' & $_OutPutFilepath
    ConsoleWrite ( '-->-- $_RunWait : ' & $_RunWait & @CRLF )
    RunWait ( $_RunWait, '', @SW_HIDE )
    ShellExecuteWait ( $_OutPutFilepath ) ; see the text content
    $_RetVal = _ReplaceStringInFile ( $_OutPutFilepath, $_StringToFind, $_StringToFind )
    If Not @error And $_RetVal Then FileDelete ( @DesktopDir & '\' & $file )
    FileDelete ( $_OutPutFilepath )
WEnd

FileClose ( $search )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to post
Share on other sites

thanks for the help wakillon .

Below is what I ended up using to take care of the recursive directories.

#include <File.au3>

$current = "r:\updatecd\gcr_document"

$ext = "*.pdf"

$_pdftotextPath = 'c:\gnuwin32\bin\pdftotext.exe'

$_OutPutFilepath = 'C:\temp\file.txt'

_FileCreate($_OutPutFilepath)

Search($current, $ext)

Func Search($current, $ext)

Local $search = FileFindFirstFile($current & "\*.*")

While 1

Dim $file = FileFindNextFile($search)

If @error Or StringLen($file) < 1 Then ExitLoop

;ConsoleWrite('-->-- $file : ' & $file & @CRLF)

If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

$_RunWait = '"' & $_pdftotextPath & '" "' & $current & '\' & $file & '" ' & $_OutPutFilepath

RunWait($_RunWait, '', @SW_HIDE)

$filecontents = FileRead($_OutPutFilepath)

$ObsoleteinFile = StringInStr($filecontents, "Obsolete")

If $ObsoleteinFile > 0 And $ObsoleteinFile < 100 Then ; check to ensure Obsolete is on title page

ConsoleWrite("Obsolete in File" & $file & $ObsoleteinFile & @CRLF)

FileDelete($current & '\' & $file)

EndIf

FileDelete($_OutPutFilepath)

EndIf

If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

Search($current & "\" & $file, $ext)

EndIf

Sleep(1000)

WEnd

FileClose($search)

EndFunc ;==>Search

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...