Reputation: 30775
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?
Upvotes: 2
Views: 2146
Reputation: 2113
If you use Select-String, alias sls you can also use regex for matching.
([xml](svn status --xml)).status.target.entry.path | sls ".+sql" | %{svn revert $_}
(n.b. $_ is a matchinfo that gets converted into a string)
Upvotes: 0
Reputation: 354506
You would need to use
$_.Split("`t")
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)).status.target.entry |
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.
However,
svn revert -R *~sql*
should work, too, I guess.
Upvotes: 6