Last active
September 11, 2024 21:37
-
-
Save jehugaleahsa/e23d90f65f378aff9aa254e774b40bc7 to your computer and use it in GitHub Desktop.
PowerShell Script to Split Large Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function join($path) | |
{ | |
$files = Get-ChildItem -Path "$path.*.part" | Sort-Object -Property @{Expression={ | |
$shortName = [System.IO.Path]::GetFileNameWithoutExtension($_.Name) | |
$extension = [System.IO.Path]::GetExtension($shortName) | |
if ($extension -ne $null -and $extension -ne '') | |
{ | |
$extension = $extension.Substring(1) | |
} | |
[System.Convert]::ToInt32($extension) | |
}} | |
$writer = [System.IO.File]::OpenWrite($path) | |
foreach ($file in $files) | |
{ | |
$bytes = [System.IO.File]::ReadAllBytes($file) | |
$writer.Write($bytes, 0, $bytes.Length) | |
} | |
$writer.Close() | |
} | |
#join "C:\path\to\file" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function split($path, $chunkSize=107374182) | |
{ | |
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($path) | |
$directory = [System.IO.Path]::GetDirectoryName($path) | |
$extension = [System.IO.Path]::GetExtension($path) | |
$file = New-Object System.IO.FileInfo($path) | |
$totalChunks = [int]($file.Length / $chunkSize) + 1 | |
$digitCount = [int][System.Math]::Log10($totalChunks) + 1 | |
$reader = [System.IO.File]::OpenRead($path) | |
$count = 0 | |
$buffer = New-Object Byte[] $chunkSize | |
$hasMore = $true | |
while($hasMore) | |
{ | |
$bytesRead = $reader.Read($buffer, 0, $buffer.Length) | |
$chunkFileName = "$directory\$fileName$extension.{0:D$digitCount}.part" | |
$chunkFileName = $chunkFileName -f $count | |
$output = $buffer | |
if ($bytesRead -ne $buffer.Length) | |
{ | |
$hasMore = $false | |
$output = New-Object Byte[] $bytesRead | |
[System.Array]::Copy($buffer, $output, $bytesRead) | |
} | |
[System.IO.File]::WriteAllBytes($chunkFileName, $output) | |
++$count | |
} | |
$reader.Close() | |
} | |
#split "C:\path\to\file" |
helpdesk-hkg
commented
Mar 24, 2021
- [ ]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment