Batch rename and replace using Microsoft Powershell

Some people don’t like it, others do, but I create my webservice clients using a local copy of the WSDL definition which I create with Soap-UI. The downside of this method is that things get messy when the webservice becomes more and more complex:

The result of a complex Soap-UI export
Soap-UI export example

Last week I’ve reached a point where Windows wouldn’t allow me to place this export in our src folder so I had to come up with a solution:

  1. Don’t use local copy
  2. Rename all xsd’s

The first solution was a no-go since the webservice URL is secured and the maven plug-in doesn’t support this.
The second solution would not only involve renaming all files, but also replacing all references to these files.
So after messing around with some regular-expressions I wrote a simple PowerShell script which will rename _1_2_3_4_…_yy.xsd to _yy.xsd and the same regular expression will be applied to the content of all files.

Powershell batch rename and replace
Powershell batch rename and replace
Dir *.xsd |
Rename-Item -NewName { $ -replace "(_\d{1,2})*(_\d{1,2}\.xsd)","`$2" }
Get-ChildItem . -Recurse |
Where {$_ -IS [IO.FileInfo]} |
% {
	(Get-Content $_.FullName) |
	ForEach-Object { $_ -replace "(_\d{1,2})*(_\d{1,2}\.xsd)", "`$2" } |
	Set-Content $_.FullName
	Write-Host "Processed: " $_.FullName

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s