1

我正在使用以下 powershell 脚本打开几千个 HTML 文件并“另存为...”Word 文档。

param([string]$htmpath,[string]$docpath = $docpath)   

$srcfiles = Get-ChildItem $htmPath -filter "*.htm*"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocument"); 
$word = new-object -comobject word.application 
$word.Visible = $False          

function saveas-document
{         
    $opendoc = $word.documents.open($doc.FullName);         
    $opendoc.saveas([ref]"$docpath\$doc.FullName.doc", [ref]$saveFormat);         
    $opendoc.close();
}       

ForEach ($doc in $srcfiles)     
{
    Write-Host "Processing :" $doc.FullName         
    saveas-document        
    $doc = $null   
}   

$word.quit(); 

内容转换得很好,但我的文件名不符合预期。

$opendoc.saveas([ref]"$docpath\$doc.FullName.doc", [ref]$saveFormat);导致foo.htm另存为foo.htm.FullName.doc而不是foo.doc.

$opendoc.saveas([ref]"$docpath\$doc.BaseName.doc", [ref]$saveFormat);产量foo.htm.BaseName.doc

如何设置Save As...文件名变量等于和的BaseName串联.doc

4

4 回答 4

0

根据我们上面的评论,您似乎只想完成移动文件。以下对我有用。在当前目录中,它将 .txt 扩展名替换为 .py 扩展名。我在这里找到了命令。

PS C:\testing dir *.txt | Move-Item -Destination {[IO.Path]::ChangeExtension( $_.Name, "py")}

您也可以更改*.txt为,C:\path\to\file\*.txt这样您就不需要从文件的位置执行此行。您应该能够以类似的方式定义目的地,所以如果我找到一种简单的方法来做这件事,我会报告回来。

另外,我在搜索时发现了 Microsoft 的 TechNet 库。它有许多关于使用 PowerShell 编写脚本的教程。文件和文件夹,第 3 部分:Windows PowerShell应该可以帮助您找到有关复制和移动文件的更多信息。

于 2012-11-17T02:17:34.070 回答
0

我只是将文件名从转换.html.docx. 我把你上面的代码改成这样:

function Convert-HTMLtoDocx {
    param([string]$htmpath)
    $srcfiles = Get-ChildItem $htmPath -filter "*.htm*"
    $saveFormat = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatXMLDocument
    $word = new-object -comobject word.application
    $word.Visible = $False

    ForEach ($doc in $srcfiles) {
        Write-Host "Processing :" $doc.fullname
        $name = Join-Path -Path $doc.DirectoryName -ChildPath $($doc.BaseName + ".docx")
        $opendoc = $word.documents.open($doc.FullName)
        $opendoc.saveas([ref]$name.Value,[ref]$saveFormat)
        $opendoc.close()
        $doc = $null
    }  #End ForEach

    $word.quit()
} #End Function

问题是保存格式。无论出于何种原因,将文档另存为.docx您需要在wdFormatXMLDocumentnot指定格式wdFormatDocument

于 2013-07-22T17:39:14.133 回答
0

这会递归遍历根文件夹并将 .doc 写入 .htm 过滤:

$docpath = "\\sf-xyz-serverabc01\ChangeTheseDocuments"
$WdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' -Passthru
$srcfiles =  get-childitem  $docpath  -filter "*.doc" -rec | where {!$_.PSIsContainer}  | select-object  FullName
$saveFormat = $WdTypes | Where {$_.Name -eq 'WdSaveFormat'}
$word = new-object -comobject word.application
$word.Visible = $False

function saveas-filteredhtml
    {
    $opendoc = $word.documents.open($doc.FullName);
    $Name=($doc.Fullname).replace("doc","htm")
    $opendoc.saveas([ref]$Name, [ref]$saveFormat::wdFormatFilteredHTML);
    $opendoc.close();
    }

ForEach ($doc in $srcfiles)
    {
    Write-Host "Processing :" $doc.FullName
    saveas-filteredhtml
    $doc = $null
    }

$word.quit();
于 2013-10-10T22:30:05.383 回答
0

我知道这是一篇较旧的帖子,但我在此处发布此代码,以便将来可以找到它

**

这会递归遍历根文件夹并将 Doc 和 DocX 转换为 Txt

**

这是您可以保存到的不同格式的链接。

$docpath = "C:\Temp"
$WdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' -Passthru
$srcfiles =  get-childitem  $docpath  -filter "*.doc" -rec | where {!$_.PSIsContainer}  | select-object  FullName
$saveFormat = $WdTypes | Where {$_.Name -eq 'WdSaveFormat'}
$word = new-object -comobject word.application
$word.Visible = $False

function saveas-filteredhtml
    {
        $opendoc = $word.documents.open($doc.FullName);
        $Name=($doc.Fullname).replace(".docx",".txt").replace(".doc",".txt")
        $opendoc.saveas([ref]$Name, [ref]$saveFormat::wdFormatDOSText); ##wdFormatDocument
        $opendoc.close();
    }

ForEach ($doc in $srcfiles)
    {
        Write-Host "Processing :" $doc.FullName
        saveas-filteredhtml
        $doc = $null
    }

$word.quit();
于 2016-04-03T14:18:09.700 回答