Powershell复制和设置内容

时间:2014-03-31 13:09:01

标签: powershell

我正在尝试将文件从一个文件夹复制到另一个文件夹,然后将更改应用于复制的文件,如此...

#Get all HTML files and copy them to Folder2

$allHTML=get-childitem $PSScriptRoot *.html 
$copyPath = "Folder1/Folder2"

foreach ($all in $allHTML)
{

Copy-Item $all $copyPath

}



#Get all HTML files inside Folder2, change and re-save them

$copyHTML=get-childitem $copyPath *.html 

foreach ($allFiles in $copyHTML)
{

(Get-Content $allFiles) | ForEach-Object { $_ -replace "this text", "with this text" } | Set-Content $allFiles

}

我感觉它的Set-Content不正确,因为此代码会更改原始文件,而不是文件夹2中的copys ...

有人可以帮帮我吗?

由于

1 个答案:

答案 0 :(得分:0)

我这样做:

#Get all HTML files and copy them to Folder2    
$allHTML = get-childitem $PSScriptRoot *.html 
$copyPath = "Folder1\Folder2"    
foreach ($all in $allHTML)
{    
   (Get-Content $all) | 
        % { $_ -replace "this text", "with this text" } | 
              Set-Content -Path "$copypath\$($all.name)"     
}