schnipdip
schnipdip

Reputation: 151

Recursively Changing File extensions from .docx and .pdf to .txt

$findPDF = Get-ChildItem -Path "$fileDrive" -Filter *.pdf -r 
$findDOCX = Get-ChildItem -Path "$fileDrive" -Filter *.docx -r

$pullFiles += $findPDF
$pullFiles += $findDOCX
#[array]$pullFiles 

#$pullFiles.length

$holdPath = @()
for($i = 0; $i -lt $pullFiles.length; $i++){
        #get the full path of each document
        $fullPath = Resolve-Path $pullFiles.fullname[$i]
        #stores the information in a global array
        $holdPath += $fullPath.path
}
#$holdPath

<#
.DESCRIPTION Uses the word.APPLICATION object to open and convert the word documents into .txt.
#>

#https://stackoverflow.com/questions/13402898/how-can-i-use-powershell-to-save-as-a-different-file-extension
#wdFormatDOSTextLineBreaks  5   Microsoft DOS text with line breaks preserved.


foreach($fi in $holdPath){
    $Doc = $word.Documents.Open($fi.name)

    $NameDOCX = ($Doc.name).replace("docx","txt")
    $Doc.saveas([ref] $NameDOCX, [ref] 5)

    $NamePDF = ($Doc.name).replace("pdf","txt")
    $Doc.saveas([ref] $NamePDF, [ref] 5)

    $Doc.close()
}

The problem Statement The program needs to get any pdf and doc/x file and convert it to a .txt file. Now, I am able to recursively search and pull all .docx and .pdf documents from the file system. Now, I just need to convert them.

The Error

You cannot call a method on a null-valued expression.
At C:\Users\p617824\Documents\files\powershell\fileExtRename.ps1:38 char:2
+     $Doc = $word.Documents.Open($fi.name)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\p617824\Documents\files\powershell\fileExtRename.ps1:40 char:2
+     $NameDOCX = ($Doc.name).replace("docx","txt")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

[ref] cannot be applied to a variable that does not exist.
At C:\Users\p617824\Documents\files\powershell\fileExtRename.ps1:41 char:2
+     $Doc.saveas([ref] $NameDOCX, [ref] 5)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (NameDOCX:VariablePath) [], RuntimeException
    + FullyQualifiedErrorId : NonExistingVariableReference

You cannot call a method on a null-valued expression.
At C:\Users\p617824\Documents\files\powershell\fileExtRename.ps1:43 char:2
+     $NamePDF = ($Doc.name).replace("pdf","txt")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

[ref] cannot be applied to a variable that does not exist.
At C:\Users\p617824\Documents\files\powershell\fileExtRename.ps1:44 char:2
+     $Doc.saveas([ref] $NamePDF, [ref] 5)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (NamePDF:VariablePath) [], RuntimeException
    + FullyQualifiedErrorId : NonExistingVariableReference

You cannot call a method on a null-valued expression.
At C:\Users\p617824\Documents\files\powershell\fileExtRename.ps1:46 char:2
+     $Doc.close()
+     ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Upvotes: 0

Views: 856

Answers (1)

Esperento57
Esperento57

Reputation: 17492

$word variable are not initialized, and your are complicated for Nothing (without offense you). Modify all your script like this :

$fileDrive ="C:\temp"
$word = new-object -ComObject Word.Application 

Get-ChildItem -Path $fileDrive -file -r -Include "*.docx", "*.pdf"  | %{

    $Doc = $word.Documents.Open($_.FullName)

    $NameDOC = $_.FullName.replace(".docx",".txt").replace(".pdf",".txt")

    $Doc.saveas([ref] $NameDOC, [ref] 5)
    $Doc.close()

}

$word.Quit()

But i have doubt on convert pdf to .txt with Word application like this... I think you should use itextsharp libray like here

Upvotes: 1

Related Questions