Split the large files into small files in windows using PowerShell



Create a PowerShell file(.ps1)  with the below script to split large files into small files.

In this example, the large file(decoded000838.log) is converted into small files as log_1.log, log_2.log... log_n with each file as 50MB.


$upperBound = 50MB # calculated by Powershell

$ext = "log"

$rootName = "log_"


$reader = new-object System.IO.StreamReader("D:\backup\11-04-22\decoded000838.log")

$count = 1

$fileName = "{0}{1}.{2}" -f ($rootName, $count, $ext)

while(($line = $reader.ReadLine()) -ne $null)

{

    Add-Content -path $fileName -value $line

    if((Get-ChildItem -path $fileName).Length -ge $upperBound)

    {

        ++$count

        $fileName = "{0}{1}.{2}" -f ($rootName, $count, $ext)

    }

}

$reader.Close()

Comments

Popular Posts