循环遍历多个制表符分隔的文本文件,并将它们拆分为较小的文件

时间:2018-03-08 13:42:35

标签: powershell foreach tab-delimited-text

我有以下脚本(我在这里找到的大多数脚本)非常适合使用第三个“列”中的值来拆分单个制表符分隔的文本文件。我需要它一次处理多个文本文件。可能是2或者可能是10。

我尝试添加foreach语句,但它仍然只处理其中一个文件,即使变量$source保存源文件夹中所有文件的值。请注意,使用注释掉的$source行,我试图创建一个包含完整路径和文件名的数组。它在使用时会抛出错误。

代码如下:

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

1 个答案:

答案 0 :(得分:3)

您需要为循环内的每个单独文件创建StreamReader

$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()) {
    ...
}