I'm working with a bunch (~2000) .csproj files, and in this development staff there's a historical precedent for embedded xcopy in the post-build events to move things around during the build process. In order to get build knowledge into once place, I'm working towards eradicating these xcopy calls in favor of declarative build actions in our automated build process.
With that in mind, I'm trying to come up with a regex I can use to chop out the path arguments supplied to xcopy. The statements come in a couple flavors:
xcopy /F /I /R /E /Y "..\..\..\Microsoft\Enterprise Library\3.1\bin"
xcopy /F /I /R /E /Y ..\Crm\*.* .\
xcopy ..\NUnit ..\..\..\output\debug /I /Y
specifically:
I'm no regex wizard, but this is what I've got so far (the excessive use of parenteses are for match saving in powershell:
(.*x?copy.* '"?)([^ /'"]+)('"/.* '"?)([^ /'"]+)('"?.*)
the ([^ /'"]+)
sections are the part that I intend to be the path arguments, being defined as strings containing no quotes, spaces, or forwards slashes, but I have a feeling I'll have to apply two regexes (one for quote-wrapped paths with spaces and one for no-quote paths)
Unfortunately, when I run this regex it seems to give me the same match for both the first and second path arguments. Most frustrating.
How would I change this to correct it?
In cases like this, I like to leverage PowerShell's argument parsing system. Use a simple regex to grab the whole xcopy line and then run it through a function.
$samples = 'xcopy /F /I /R /E /Y "..\..\..\Microsoft\Enterprise Library\3.1\bin"',
'xcopy /F /I /R /E /Y ..\Crm\*.* .\',
'xcopy ..\NUnit ..\..\..\output\debug /I /Y'
function argumentgrinder {
$args | Where-Object {($_ -notlike "/*") -and ($_ -ne "xcopy")}
}
$samples | foreach { Invoke-Expression "argumentgrinder $_"}
You do have to be careful of anything that looks like a PowerShell variable in the paths though ($
, @
and parentheses).