简体   繁体   中英

How to search in PDF files and move them using powershell

I have a large number of PDF files in a folder with several subfolders. In this pile of files I need to find the ones with a specific string and move them to a new destination.

I already have a fine piece of code for the search process that gives me the files needed (thx to the creator) - now I need help to combine this code with a move-function. All the files found by the following code should be moved to a new destination.

$searchString = "text i need to find" 
$searchPath = "C:\test" 
$sql = "SELECT System.ItemPathDisplay, System.DateModified, " 
+ "System.Size, System.FileExtension FROM SYSTEMINDEX " 
+ "WHERE SCOPE = '$searchPath' AND FREETEXT('$searchstring')" 
$provider = "provider=search.collatordso;extended properties=’application=windows’;"  
$connector = new-object system.data.oledb.oledbdataadapter -argument $sql, $provider  
$dataset = new-object system.data.dataset  
if ($connector.fill($dataset)) { $dataset.tables[0] } 

The output is like:

SYSTEM.ITEMPATHDISPLAY SYSTEM.DATEMODIFIED SYSTEM.SIZE SYSTEM.FILEEXTENSION
---------------------- ------------------- ----------- --------------------
C:\test\file.pdf       27.08.2019 19:14:57 17119       .pdf   

Thank you for your help!

I found a solution by myself. For anyone interested.

Note: $searchPath must be a local drive on the machine you are running the script on, because the PDF files need to be indexed by the windows search. For that you probably have to install an iFilter: https://superuser.com/questions/402673/how-to-search-inside-pdfs-with-windows-search

$searchString = "Merkblatt für nüchtern eintretende Patienten" 
$searchPath = "Y:\" 
$targetPath = "\\Server\Path\folder"

$sql =  "SELECT System.ItemPathDisplay, System.DateModified, " + 
        "System.Size, System.FileExtension FROM SYSTEMINDEX " + 
        "WHERE SCOPE = '$searchPath' AND FREETEXT('$searchstring')" 
$provider = "provider=search.collatordso;extended properties=’application=windows’;"  
$connector = new-object system.data.oledb.oledbdataadapter -argument $sql, $provider  
$dataset = new-object system.data.dataset  
if ($connector.fill($dataset)) { 
   #$dataset.tables[0] 
   foreach ( $Row in $dataset.tables[0].Rows) {
      $targetFile = $Row[0] -replace "^Y:", $targetPath
      $targetSubfolder = Split-Path -Path $targetFile
      #write-host "Targetfile : $targetFile"
      #write-host "Moving: $($Row[0])"
      Move-Item -Path $($Row[0]) -Destination $targetPath -Force
   }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM