Pages

Wednesday, February 22, 2017

PowerShell Script for Split the Large File Into Small Files

How to use: Attached Powershell script will Split the large files into the multiple small files based on the number of lines required in the each file.Place the large file in the SOURCELOCATION folder.Create an SPLITFILELOCATION and MOVEFILELOCATION folder. Copy the Powershell script and run in Windows PowerShell Console. Files will be created based on the line numbers.
#split File Functionality

$sw = new-object System.Diagnostics.Stopwatch
$sw.Start()
$todaysdate =  (Get-Date).ToString('yyyyMMdd_hhmmss')  # Todays Date
$SourceFileName="TEST_BIGFILE" #Source File Name
$ext = ".csv"   #File Extentsion
$filename = "E:\\SPLITFILES\SOURCELOCATION\" + $SourceFileName + $ext   #Source File location and File Name
$rootName = "E:\\SPLITFILES\SPLITFILELOCATION\SPLIT_FILE_"      # Split File Location 
$moveFileLocation = "E:\\SPLITFILES\MOVEFILELOCATION\" + $SourceFileName + $ext #Move File location and File Name
$renameFileName=$SourceFileName+"_"+$todaysdate+$ext #Rename File
$linesperFile = 100 #Number of Line Records
$filecount = 1
$reader = $null
if (Test-Path $filename)
{
try{
$reader = [io.file]::OpenText($filename)
try{
"Creating file number $filecount"
$writer = [io.file]::CreateText("{0}{1}_{2}{3}" -f ($rootName,$todaysdate,$filecount.ToString("000"),$ext))
$filecount++
$linecount = 0
while($reader.EndOfStream -ne $true) {
"Reading $linesperFile"
while( ($linecount -lt $linesperFile) -and ($reader.EndOfStream -ne $true)){
$writer.WriteLine($reader.ReadLine());
$linecount++
}
if($reader.EndOfStream -ne $true) {
"Closing file"
$writer.Dispose();
"Creating file number $filecount"
$writer = [io.file]::CreateText("{0}{1}_{2}{3}" -f ($rootName,$todaysdate,$filecount.ToString("000"),$ext))
$filecount++
$linecount = 0
}
}
} finally {
$writer.Dispose();
}
} finally {
$reader.Dispose();
}
Write-Host "Move File Started to " $filename $moveFileLocation
Move-Item $filename $moveFileLocation
Write-Host "Rename File " $moveFileLocation+"\"+$SourceFileName + $ext $renameFileName
Rename-Item $moveFileLocation $renameFileName

}
else
{
Write-Host "No File Found to Process " $filename 
}
$sw.Stop()
Write-Host "Split complete in " $sw.Elapsed.TotalSeconds "seconds"

=========================================


Create a Batch File For Split File


PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 
'E:\\SPLITFILES\SplitFilePowerShellCommands\SplitFileScript.ps1'"
pause

No comments:

Post a Comment