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...

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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 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
Since I added that comment I have installed Microsoft Windows PowerShell 1.0 for Windows XP from 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 by adding -ErrorAction SilentlyContinue. Either way the script you gave worked ok on PowerShell 1 on Win XP. So thanks.
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
    }
}

3 Comments

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.
@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".
Sorry for confusing question =(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.