Categories

Games: Beginner 3

We start with a file and have to put the first paragraph into one file and the second into another file. 

001
002
003
004
005
006
007
008
009
010
$flag = $true
if (Test-Path "Shot Put A.txt"){Remove-Item "Shot Put A.txt"}
if (Test-Path "Shot Put B.txt"){Remove-Item "Shot Put B.txt"}
Get-Content "Shot Put.txt" | foreach {
    if ($_ -ne ""){
        if ($flag){Add-Content -Path "Shot Put A.txt" -Value $($_) }
        else {Add-Content -Path "Shot Put B.txt" -Value $($_)}
    }
    else {$flag = $false}
}

 

Create a flag (set true). Test for the existence of the output files and delete if present   Read the file and loop through.  We can identify the paragraph break as there is an empty line – when we get to that we change the value of the flag.  If the flag is true add the line to the first file otherwise add to the second file.

 

Technorati Tags: ,

Leave a Reply