2013-08-29 66 views
1

我需要建立在PowerShell中腳本將我的所有文件轉換從.RTF爲.doc然後回來。我想這樣做,因爲我已經申請註冊表修復這種轉換後,這將降低我的RTF文件大小(它不會救第二WMF圖像specyfic信息http://support.microsoft.com/kb/224663)。我想象我的腳本工作流程RTF_1保存到文檔,然後關閉RTF1刪除RTF 1,將文檔保存到rtf2,關閉文檔,刪除文檔。在PowerShell中(WMF註冊表修復)轉換.rtf文件爲.doc,然後.DOC回的.rtf - 文件大小優化

回答

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();