1

我需要在 powershell 中创建一个脚本,它将我的所有文件从 .rtf 转换为 .doc 然后再转换回来。我想这样做是因为我已经应用了注册表修复程序,这将在这种转换后减小我的 rtf 文件的大小(它不会保存第二个 WMF 图像特定信息http://support.microsoft.com/kb/224663)。我想我的脚本工作流程为 RTF_1 保存到 doc 然后关闭 rtf1 删除 rtf 1 ,将 doc 保存到 rtf2 ,关闭 doc ,删除 doc。

4

1 回答 1

0

这是最终的工作脚本(不删除,我决定不需要它)

param([string]$rtfpath,[string]$docpath = $rtfpath,[string]$docpath2 = $rtfpath2)
$srcfiles = Get-ChildItem $rtfPath -filter "*.rtf"
$docfiles = Get-ChildItem $docPath -filter "*.doc"
$saveFormat =[Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocument");
$saveFormat_back =[Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatRTF");
$word = new-object -comobject word.application
$word.Visible = $False
function saveas-DOC
     {

  $name = $rtf.basename
  $savepath ="$docpath\rtf" + $name + ".doc"
  write-host $name
  Write-Host $savepath
         $opendoc = $word.documents.open($rtf.FullName);
         $opendoc.saveas([ref]$savepath, [ref]$saveFormat);
         $opendoc.close();
     }

function saveas-back_to_rtf
{
         $name = $doc.basename
   $savepath2 ="$rtfpath2\doc" + $name + ".rtf"
   write-host $name
   Write-Host $savepath
         $opendoc = $word.documents.open($doc.FullName);
         $opendoc.saveas([ref]$savepath2, [ref]$saveFormat_back);
         $opendoc.close();

}

ForEach ($rtf in $srcfiles)
     {
         Write-Host "Processing :" $rtf.FullName
         saveas-DOC

     }

ForEach ($doc in $docfiles)
     {
         Write-Host "Processing doc file :" $doc.FullName
         saveas-back_to_rtf
         $doc = $null
     }


$word.quit();
于 2014-08-20T11:56:06.517 回答