"converting .rtf files to .doc and then .doc back to .rtf in powershell (wmf registry fix) - file size optimization" Code Answer

3

here is the final working script ( without deleting , i decided i do not need it )

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 ="$docpathrtf" + $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 ="$rtfpath2doc" + $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();
By user2398029 on September 29 2022

Answers related to “converting .rtf files to .doc and then .doc back to .rtf in powershell (wmf registry fix) - file size optimization”

Only authorized users can answer the Search term. Please sign in first, or register a free account.