Public/Split-File.ps1
|
function Split-File { <# .SYNOPSIS Splits any file into sequential binary parts of at most ChunkSizeMB megabytes. .DESCRIPTION Reads the source file in binary chunks and writes each chunk as a separate file inside a new subfolder named {BaseName}-Parts, following the naming convention: {BaseName}.001.zip, {BaseName}.002.zip, ... The original file is left intact. .PARAMETER Path Full path to the file to split. .PARAMETER ChunkSizeMB Maximum size in megabytes for each part. Defaults to 14. .OUTPUTS [string[]] Ordered list of full paths to the created part files. .EXAMPLE Split-File -Path "C:\Temp\LargeFile.zip" .EXAMPLE Split-File -Path "C:\Temp\LargeFile.cab" -ChunkSizeMB 25 #> [CmdletBinding()] param( [Parameter(Mandatory)] [string] $Path, [int] $ChunkSizeMB = 14 ) if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "File not found: $Path" } Write-Host "Splitting '$([System.IO.Path]::GetFileName($Path))' into $ChunkSizeMB MB parts..." $parts = Split-ParsedFile -FilePath $Path -ChunkSizeMB $ChunkSizeMB $parts | ForEach-Object { Write-Host " $_" } Write-Host "Split complete: $($parts.Count) part(s)" -ForegroundColor Green return ,$parts } |