Frank Schmitt Frank Schmitt - 1 year ago 105
PowerShell Question

Powershell: SVN revert for all files matching a given pattern

I accidentally added dozens of backup files to SVN and would like to recursively revert all files matching a given pattern (*.~sql)

On UNIX, I'd simply use a

svn revert `svn status .|grep "~sql$"|awk '{print $2}'`

which is a variation of one of the answers to How do I SVN add all unversioned files to SVN?

Using Powershell, I came up with

svn status|findstr "~sql" | %{ $_.Split('`t')[1]; }

but this seems to cut of the first characters of the filenames
(e.g. I get ch_sys.~sql instead of scratch_sys.~sql).

What am I missing here? Or is there a more Powershellish way of doing this?

Answer Source

You would need to use


as a quick fix for your code because currently you have a string containing `t instead of a string containing a tab because single-quoted strings don't allow escape sequences (except ""). string.Split gets a char[] as argument and splits at any of the characters you passed, so in this case it splits on a grave accent and t.

But you are right, that's not really a PowerShell-y way of doing this. First of all, you're using findstr which has plenty of PowerShell equivalents, e.g.

svn status | where { $_ -match '~sql' }
(svn status) -match '~sql'

When working with SVN from PowerShell you have another option, though. Most SVN commands can output XML which is a lot more robust to handle than text output, usually.

([xml](svn status --xml)) |
  select -exp path |
  where {$_ -match '~sql'} |
  foreach { svn revert $_ }

Whether that's prettier is probably debatable, but I prefer working with structured data when possible.


svn revert -R *~sql*

should work, too, I guess.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download