Alexey Shcherbak
Alexey Shcherbak

Reputation: 3454

Powershell script to find all svn working copies

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

Upvotes: 2

Views: 1499

Answers (2)

Alexey Shcherbak
Alexey Shcherbak

Reputation: 3454

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.

Upvotes: 5

Andy Arismendi
Andy Arismendi

Reputation: 52587

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

Upvotes: 1

Related Questions