简体   繁体   中英

Powershell Copy and Set-Content

I am trying to copy files from one folder to another, then apply changes to the copied files like so...

#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

}

I have a feeling its the Set-Content thats not right as this code will change the original files, not the copys in Folder 2...

Can anyone please help me out?

Thanks

I'ld do it like this:

#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)"     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM