How to control loop script RRS feed

  • Question

  • I have a script that runs a loop set to run a preset number of 50 items in a section

    Need some flexibility as sometimes the total items differ enough that some data is not processed.

    What would be the best way to change the script so that instead of a number would run until it came to a text such as Total or EOF ?

    for ($i=$Record; $i -lt $Record+50; $i++) is what I am working with (unsuccessfully).

    Here is what I have:

    $Source = (Get-Content -Path $SourceFile)

    $RecordStart = @()
    0..$Source.count | % {

    # Start of Group
    if ($Source[$_] -match 'Group:') { $RecordStart += $_ }

    $Server = @()

    foreach ($Record in $RecordStart) {

    for ($i=$Record; $i -lt $Record+50; $i++) {

    *** Text Extraction Commands ***


    } # for $i

    $Server | Export-Csv $CSV -NoType

    $Server | FT -Auto

    • Moved by Bill_Stewart Tuesday, June 16, 2015 8:45 PM Poor quality question/shows no research effort
    Tuesday, May 5, 2015 11:08 PM

All replies

  • I would recommend that you use PowerShell to filter content.

    Here is how we would do this in PowerShell:

    Get-Content -Path $SourceFile | 
        Where{$_ -match 'Group:'} | 
        Export-Csv $CSV -NoType

    Notice how much easier it is.  It can be extended to handle almost any set of requirement.


    Wednesday, May 6, 2015 8:59 AM