Tags

Below is the script that loop folder to replace the string in all XML files.

The string I need to place is the href link in InfoPath form. So it look like

href="http://ppp/ppp/template.xsn"

 

So I use the way to replace string in between the characters

'bleeeh' -replace '(bl).+(h)','$1cc$2' 

 

This will print out: blcch

The whole script is look like:

Param($Path = "D:\Temp",[switch]$verbose)

Write-Host

$fc = new-object -com scripting.filesystemobject
$folder = $fc.getfolder($path)
$strFinder = '(href="http://).+(template.xsn)'

function ProcessFiles([string]$folderpath) {

Write-host " + Processing all Forms from: $folderpath"
$Forms = dir $folderpath "*.xml"

Write-host "  + $($Forms.count) Forms found"
Write-host " + Processing all $($Forms.count) Forms located at: $folderpath" -fore White

	foreach($Form in $Forms)
	{
		$strReplace ="$1XXX$2"
		Write-Host "  + +	Processing Form: $($Form.Name)" -fore White
		if($Form.Name.Contains("IPP")){$strReplace ='$1IPP$2'}
		elseif($Form.Name.Contains("EDP")){$strReplace ='$1EDP$2'}
		Replace-String  $strFinder $strReplace $Form.FullName
	}	
}

function Replace-String($find, $replace, $path)
{
	echo "Replacing string `"$find`" with string `"$replace`" in file contents and file names of path: $path"
	ls $path | select-string $find -list |% { echo "Processing contents of $($_.Path)"; (get-content $_.Path) |% { $_ -replace $find, $replace } | set-content $_.Path -Force }
}

Write-Host "$($folder.ShortName) "
Write-Host "  + Total Folders: $($folder.SubFolders.Count)"
foreach ($i in $folder.SubFolders) {
	Write-Host "  + Folder Name: $($i.ShortName)"
	ProcessFiles($i.Path)
}

 

Love PowerShell

Advertisements