How can I use PowerShell to `save as…` a different file extension?

前端 未结 4 1839
你的背包
你的背包 2021-01-16 12:22

I\'m using the following powershell script to open a few thousand HTML files and \"save as...\" Word documents.

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


        
4条回答
  •  隐瞒了意图╮
    2021-01-16 12:56

    I know this is an older post but I am posting this code here so that I can find it in the future

    **

    This does a recursive walk of a root folder and Converts Doc and DocX to Txt

    **

    Here is a LINK to the diffierent formats you can save to.

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

提交回复
热议问题