razielxx
razielxx

Reputation: 99

Loop through multiple Tab delimited Text files and split them in to smaller files

I have the following script (most of which I found on here) that works great for splitting out individual Tab delimited text files by using the value from the third "column". I need it to work with multiple text files at once. It could be 2 or it could be 10.

I tried adding a foreach statement, but it still only processes 1 of the files, even though the variable $source hold the value for all the files in the source folder. Please note that with the commented out $source line, I was trying to create an array with the full path and file name. It throws errors when using it.

Code is below:

#$source = Get-ChildItem \\srvfile1oke\meas\OEG\Shared\Text_File_Splitter\* -Include *.txt | %{ @{Path=$_.fullname} }
#$target = "\\srvfile1oke\meas\OEG\Shared\Text_File_Splitter\"
$source = Get-ChildItem "\\srvfile1oke\meas\OEG\Shared\Text_File_Splitter\*.txt"
$target = "\\srvfile1oke\meas\OEG\Projects\FlowCal service files\Flow Text Files\MeterTextFilesByImportID\"
$fileIn = New-Object -TypeName System.IO.StreamReader -ArgumentList $source
$header = $fileIn.ReadLine()
$currentFile = ""

foreach ($file in $source) {
    while ($line = $fileIn.ReadLine()) {
        $newFile = "$(($line -split "\t")[2]).txt"
        if ($newFile -ne $currentFile) {
            #starting on a new file
            if ($currentFile -ne "") {
                # Write out contents of current file
                $fileOut.ToString() | Out-File -FilePath $target\$currentFile -Encoding ascii
            }
            # Get ready for a new current file
            $currentFile = $newFile
            $fileOut = New-Object -TypeName System.Text.StringBuilder
            [void]$fileOut.Append($header)
        }
        Write-Verbose "$currentFile, $line"
        [void]$fileOut.Append("`r`n$($line)")
    }
    # Write out contents of last file
    $fileOut.ToString() | Out-File -FilePath $target\$currentFile -Encoding ascii
}

Upvotes: 0

Views: 699

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

You need to create a StreamReader for each individual file inside the loop:

$fileIn = New-Object -TypeName System.IO.StreamReader -ArgumentList $source
$header = $fileIn.ReadLine()
$currentFile = ""

foreach ($file in $source) {
    $fileIn = New-Object IO.StreamReader $file
    $header = $fileIn.ReadLine()
    while ($line = $fileIn.ReadLine()) {
    ...
}

Upvotes: 3

Related Questions