我正在使用以下 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
?