0

I'm looking to split a txt file into multiple files each time there is a line break / no text on the row.

Is this possible using PowerShell?

Does anyone have a script for this?

This is an example of the data:

Notepad screenshot

2 Answers 2

0

The following PowerShell script does exactly what you required. Each section is saved with the same file name and an appended incrementing number.

## Q:\Test\2018\06\27\SU_1334727.ps1
$FileName = '.\test.txt'
If (Test-Path $FileName){
    $File = Get-Item $FileName
    (Get-Content $File.FullName -Raw) -Split "(?<=`r?`n *`r?`n)" |
        ForEach-Object {$i=1}{
            $_ | Set-Content ("{0}_{1}{2}" -f $File.BaseName,$i++,$File.Extension)
        }
}
  • the file is read in as a continous piece of text and split by a nonconsuming Regular Expression using a positive look behind

Sample output:

> gc .\test_1.txt
1        sf
1        s
1        sg
1        sv
1        sgsv
0

There are various ways to do the and a quick search on web using …

Powershell 'split a file on line feed'

… would return several hits.

It could be as simple as using the New-Item, Get-Children and Add-Content cmdlets using a loop and a condition.

$Lines = Get-Content 'D:\Scripts\$data.txt'

New-Item -Path 'D:\Scripts' -Name 'FileName.txt' -ItemType File
$FileName = (Get-ChildItem -Path 'D:\Scripts\FileName.txt').FullName

$FileCount = 0

ForEach($Line in $Lines)
{
    If($Line -ne '')
    {
        'Appending line to a file'
        Add-Content -Value $Line -Path $FileName
    }
    Else
    {
        'Empty line encountered - creating new file'
        $FileName = New-Item -Path 'D:\Scripts' `
        -Name (((Get-ChildItem -Path 'D:\Scripts\FileName.txt').BaseName + 
        ($FileCount = $FileCount + 1) + '.txt')) -ItemType File
    }
}
Get-ChildItem -Path 'D:\Scripts\filename*'

# Results

    Directory: D:\Scripts


Mode                LastWriteTime         Length Name                                                                                                                                                                  
----                -------------         ------ ----                                                                                                                                                                  
-a----        27-Jun-18     12:48              0 FileName.txt                                                                                                                                                          
Appending line to a file
Appending line to a file
Appending line to a file
Empty line encountered - creating new file
Appending line to a file
Appending line to a file
Appending line to a file
Empty line encountered - creating new file
Appending line to a file
Appending line to a file
Appending line to a file
-a----        27-Jun-18     12:48             21 FileName.txt                                                                                                                                                          
-a----        27-Jun-18     12:48             21 FileName1.txt                                                                                                                                                         
-a----        27-Jun-18     12:48             21 FileName2.txt   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.