Search code examples
powershellpdfdynamicitext

Get-Childitem then call function


Hope we are all well - apologies for lack of understanding

$pdfDirectory ='C:\PDF'
Get-ChildItem -Path $pdfDirectory -Filter *.pdf

Lists the below files, all files have different names.

Mode                 LastWriteTime         Length Name                                                                                                                                        
----                 -------------         ------ ----                                                                                                                                        
-a----        25/01/2024     09:15          79807 1 - Copy (2).pdf                                                                                                                            
-a----        25/01/2024     09:15          79807 1 - Copy.pdf                                                                                                                                
-a----        25/01/2024     09:15          79807 1.pdf                                                                                                                                       

I am trying to pass each dynamic named pdf to a function called Convert-PDFtoText $file

I am rather lost. I need to create $file for each of the files named in the list.

Would love some direction if possible.

When I run this I get

$pdfDirectory ='C:\PDF'
Get-ChildItem -Path $pdfDirectory -Filter *.pdf

'This part is incorrect
$file = $pdfDirectory 

convert-PDFtoText $file
Mode                 LastWriteTime         Length Name                                                                                                                                        
----                 -------------         ------ ----                                                                                                                                        
-a----        25/01/2024     09:15          79807 1 - Copy (2).pdf                                                                                                                            
-a----        25/01/2024     09:15          79807 1 - Copy.pdf                                                                                                                                
-a----        25/01/2024     09:15          79807 1.pdf                                                                                                                                       
New-Object : Exception calling ".ctor" with "1" argument(s): "C:\PDF not found as file or resource."
At C:\PS\PDFtoTEXT.ps1:6 char:9
+     $pdf = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $fi ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
 
You cannot call a method on a null-valued expression.
At C:\PS\PDFtoTEXT.ps1:11 char:2
+     $pdf.Close()
+     ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Solution

  • You are messing up the syntax.

    'C:\PDF' | Get-ChildItem -Filter *.pdf | Foreach-Object { convert-PDFtoText -FilePath $_.fullname }