递归地将.docx和.pdf中的文件扩展名更改为.txt

时间:2017-06-15 20:39:06

标签: powershell pdf recursion doc

$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()
}

问题陈述 该程序需要获取任何pdf和doc / x文件并将其转换为.txt文件。现在,我能够递归搜索并从文件系统中提取所有.docx和.pdf文档。现在,我只需要转换它们。

错误

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

1 个答案:

答案 0 :(得分:1)

$ word变量未初始化,并且你的Nothing很复杂(没有冒犯你)。像这样修改你的所有脚本:

id

但是我对使用像这样的Word应用程序将pdf转换为.txt表示怀疑......我认为你应该使用像here这样的itextsharp libray