2

I would like to write powershell script to upgrade all working copies from 1.6 svn to 1.7. The problem is to find all working copies in specified subdirectory and stop on first match for each one. This script could find all .svn directories including subdirs,nested in working copy:

Get-ChildItem -Recurse -Force -Path "d:\Projects\" |?{$_.PSIsContainer -and $_.FullName -match ".svn$"}|Select-Object FullName

Is there any option to stop Get-ChildItem on first match in directory, and cease recurse subdirs processing ? Any hints to look at ?

Another option is to get output results and sort\filter the list with some logic, based on parent\child dir relations. A bit more confusing way, imo, but its also an option...

Alexey Shcherbak
  • 3,394
  • 2
  • 27
  • 44

2 Answers2

5

I found proper filtering condition for such pipeline.

$prev = "^$"

Get-ChildItem -Recurse -Force -Include ".svn" -Path "d:\Projects\" `
| ?{$_.PSIsContainer -and $_.Fullname.StartsWith($prev)-eq $false}`
| %{ $prev=$_.Fullname.TrimEnd(".svn"); $prev}

It's similar to regex backreference and work as expected inside pipeline filter.

Alexey Shcherbak
  • 3,394
  • 2
  • 27
  • 44
  • Hi I have the same need. I was about to ask "How to find all svn working copies on win xp". But, to avoid duplication, can you expand upon this answer. I do not have a powershell. Could i just paste the code in the answer into a bash script and run it? – JW. Mar 27 '12 at 21:32
  • No, JW, unfortunately you cant paste this code to bash script. Neither cmd\batch will work. You need powershell to execute this. But fortunately there is powershell 2.0 for winxp available (ex. check this http://j.mp/Hbr88y). If you still have problems with installing something on your machine - you could try to find same script for bash (as you mentioned) - bash is pretty powerfull and such script could be made easy with it. As for cmd (shipped on WinXP by default) - I dont have a lot of experience with it,but have some doubts that cmd script will be so short and simple as powershell one. WBR – Alexey Shcherbak Mar 28 '12 at 03:57
  • Since I added that comment I have installed [Microsoft Windows PowerShell 1.0 for Windows XP from cnet.com](http://www.cnet.com/) on my Win XP. I then tried out your script on my C:\ and was getting an error `Get-ChildItem : Access to the path 'C:\System Volume Information' is denied.`Before I realised that the script continued running despite that error, I found a [fix for that](http://www.computerperformance.co.uk/powershell/powershell_erroraction_silentlycontinue.htm) by adding `-ErrorAction SilentlyContinue`. Either way the script you gave worked ok on PowerShell 1 on Win XP. So thanks. – JW. Mar 28 '12 at 11:48
1

You can use break. Here's some examples:

Get-ChildItem -Recurse -Force -Path "d:\Projects\" | ? {$_.PSIsContainer } | % {
    if ($_.FullName  -match ".svn$") {
        # 1. Process first hit
            # Code Here... 
        # 2. Then Exit the loop
            break
    }
}

Or a slightly different way:

$dirs = Get-ChildItem -Recurse -Force -Path "d:\Projects\" | ? {$_.PSIsContainer }
foreach ($dir in $dirs) {
    if ($dir.FullName  -match ".svn$") {
        # 1. Process first hit
            # Code Here... 
        # 2. Then Exit the loop
            break
    }
}
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Umm, your scripts will find only one working copy (first one in directory) and then break will be hit. I think if-condition could be modified properly or break should be with some additional checks. Thank you for efforts. I found proper answer with some more study after posting it. PS: I upvote your answer but cannot accept it as question answer. – Alexey Shcherbak Jan 26 '12 at 09:21
  • @AlexeyShcherbak I guess I had trouble interpreting your question. It looked like you wanted to stop processing on the first hit based on your code example and following requirement to get the "first match in directory, and cease recurse subdirs processing". – Andy Arismendi Jan 26 '12 at 10:53
  • Sorry for confusing question =( – Alexey Shcherbak Jan 26 '12 at 11:28