PowerShell-如何将以下脚本放在一起+调整页眉和页脚

时间:2018-11-21 18:34:38

标签: powershell scripting

这是我目前正在处理的Word文档脚本的示例 该脚本会更改所有文件中的单词,但是****不会****更改部分文件名以及页眉和页脚

$objWord = New-Object -comobject Word.Application  
$objWord.Visible = $false

$list = Get-ChildItem "C:\Users\*.*" -Include *.doc*
foreach($item in $list){
$objDoc = $objWord.Documents.Open($item.FullName,$true)

$objSelection = $objWord.Selection 
$wdFindContinue = 1
$FindText = "COMPUTER" 
$MatchCase = $False 
$MatchWholeWord = $true
$MatchWildcards = $False 
$MatchSoundsLike = $False 
$MatchAllWordForms = $False 
$Forward = $True 
$Wrap = $wdFindContinue 
$Format = $False 
$wdReplaceNone = 0 
$ReplaceWith = "DESKTOP" 
$wdFindContinue = 1 
$ReplaceAll = 2

$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` 
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` 
$Wrap,$Format,$ReplaceWith,$ReplaceAll) 
$objDoc.Save()
$objDoc.Close()
}
$objWord.Quit()

在此以脚本形式更改文件名的一部分:

PS C:\User> get-childitem | foreach { rename-item $_ $_.Name.Replace("file1", "computer") }

如何将所有内容放在一起? 谢谢

1 个答案:

答案 0 :(得分:0)

最后一个代码段超出了您在文字中所做的工作。因此,当您说合并时,就像您已经在做的一样:

您不是在说/也不显示您在最后一个代码段中遇到的错误。

尝试重命名文件时,必须使用完整的文件名,其中包括该文件的路径,就像在单词段中一样。

Get-ChildItem -Path E:\Temp -Filter '*.txt' 

    Directory: E:\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       11/27/2018   3:51 PM              0 OriginalFileName.txt


Get-ChildItem -Path 'E:\Temp' -Filter '*.txt' | 
ForEach { Rename-Item -Path $_.FullName -NewName ($_.FullName.Replace('Original','New')) -WhatIf }


ForEach { Rename-Item -Path $_.FullName -NewName ($_.FullName.Replace('Original','New')) -WhatIf }
What if: Performing the operation "Rename File" on target "Item: E:\Temp\OriginalFileName.txt Destination: E:\Temp\NewFileName.txt".



Get-ChildItem -Path 'E:\Temp' -Filter '*.txt' | 
ForEach { Rename-Item -Path $_.FullName -NewName ($_.FullName.Replace('Original','New')) }



Get-ChildItem -Path E:\Temp -Filter '*.txt' 


    Directory: E:\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       11/27/2018   3:51 PM              0 NewFileName.txt


foreach($item in $list)
{
    $objDoc = $objWord.Documents.Open($item.FullName,$true)

    $objSelection = $objWord.Selection 
    $wdFindContinue = 1
    $FindText = "COMPUTER" 
    $MatchCase = $False 
    $MatchWholeWord = $true
    $MatchWildcards = $False 
    $MatchSoundsLike = $False 
    $MatchAllWordForms = $False 
    $Forward = $True 
    $Wrap = $wdFindContinue 
    $Format = $False 
    $wdReplaceNone = 0 
    $ReplaceWith = "DESKTOP" 
    $wdFindContinue = 1 
    $ReplaceAll = 2

    $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` 
    $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` 
    $Wrap,$Format,$ReplaceWith,$ReplaceAll) 
    $objDoc.Save()
    $objDoc.Close()

    Rename-Item -Path $item.FullName -NewName ($item.FullName.Replace($item.BaseName,$FindText)) -WhatIf
}
$objWord.Quit()

只需删除-WhatIf,即可使其真正运行。