0

I have correctly written the following PowerShell command to traverse every folder and subfolder of a directory and display the contents of each such folders:

Get-ChildItem -Path "C:\whiskey\Tango\Charlie" –Recurse

I have also written a PowerShell script that correctly works and can extract text from each pdf file in a single folder.

cd "C:\whiskey"
$FILES= ls *.pdf
foreach ($f in $FILES) {
  & "C:\Program Files\xpdf-tools-win-4.02\bin32\pdftotext.exe" -enc UTF-8 "$f"
}

I am trying to combine both scripts in order to traverse all folders and subfolders and execute the pdftotext program. And so I wrote:

$files=Get-ChildItem -Path "C:\whiskey\Tango\Charlie" –Recurse
foreach ($f in $files){
    & "C:\Program Files\xpdf-tools-win-4.02\bin32\pdftotext.exe" -enc UTF-8 "$f"
}

; which is incorrect. I receive the following error: pdftotext.exe : I/O Error: Couldn't open file. Note none of the pdf files are protected.

Question: How do I correct this error?

3
  • 1
    $Files should be an array of FileSystemInfo objects (FileInfo, DirectoryInfo). try using $f.FullName to get the full path of the file as a string or $f.Name if you just want the shortname. Jul 27, 2021 at 18:33
  • @FrankThomas something along the lines of $files=@(Get-ChildItem -Path "C:\Users\ahernandez\Desktop\BW\OCR\Mallinckrodt" –Recurse ) ? Jul 27, 2021 at 18:45
  • 1
    based on what I'm seeing here stackoverflow.com/a/32915159/1624069 , I don't think that will work. the issue isn't the array, the issue is that the things in the array are not strings. I suggest that in your last line, you change "$f" to "$f.FullName" or "$f.tostring()". Jul 27, 2021 at 19:06

1 Answer 1

1

This NOT an answer but an extended comment following @FrankThomas. It would appear the following script "works"

$FOLDERS=@(Get-ChildItem -Path "C:\whiskey\Tango\Charlie" –Recurse)
$FOLDERS #print contents of array
foreach ($f in $FOLDERS) {
  & "C:\Program Files\xpdf-tools-win-4.02\bin32\pdftotext.exe" -enc UTF-8 $f.FullName
}

Indeed it goes through each folder and subfolder and executes the pdftotext program and so correctly extracts the contents of the pdf file into a text file. However I still have an error message: pdftotext.exe : I/O Error: Couldn't open file "C:\whiskey\Tango\Charlie". I suspect the name of every folder was saved in the array $FOLDERS. Subsequently PowerShell passes the name of the folder to pdftotext; which will error since it cannot find a .pdf extension.


The following is the correct PS script:

$FOLDERS=@(Get-ChildItem -Path "C:\whiskey\Tango\Charlie" –Recurse -Filter *.pdf)
$FOLDERS #print contents of every folder
foreach ($f in $FOLDERS) {
  & "C:\Program Files\xpdf-tools-win-4.02\bin32\pdftotext.exe" -enc UTF-8 $f.FullName
}

I need to filter for just pdf files.

1
  • 1
    ok, so thats because you are processing a directory. I recommend you change your first line to set the path to C:\whiskey\Tango\Charlie\*.pdf as shown in example 3 here: docs.microsoft.com/en-us/powershell/module/… That should prevent you from processing directories or non-pdf files. Jul 27, 2021 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.