Skip to content

Instantly share code, notes, and snippets.

@bigga
Forked from awayken/Split-File.ps1
Created March 29, 2022 15:19
Show Gist options
  • Select an option

  • Save bigga/8ce09af63b4a072600ba7b99b5f8316c to your computer and use it in GitHub Desktop.

Select an option

Save bigga/8ce09af63b4a072600ba7b99b5f8316c to your computer and use it in GitHub Desktop.

Revisions

  1. @awayken awayken created this gist Jun 25, 2013.
    34 changes: 34 additions & 0 deletions Split-File.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # Modified from: http://stackoverflow.com/a/11010158/215200

    $fromFolder = "D:\FOLDER\"
    $rootName = "FILENAME"
    $ext = "EXT"

    $from = "{0}{1}.{2}" -f ($fromFolder, $rootName, $ext)
    $fromFile = [io.file]::OpenRead($from)

    $upperBound = 100MB
    $buff = new-object byte[] $upperBound

    $count = $idx = 0

    try {
    "Splitting $from using $upperBound bytes per file."
    do {
    $count = $fromFile.Read($buff, 0, $buff.Length)
    if ($count -gt 0) {
    $to = "{0}{1}.{2}.{3}" -f ($fromFolder, $rootName, $idx, $ext)
    $toFile = [io.file]::OpenWrite($to)
    try {
    "Writing to $to"
    $tofile.Write($buff, 0, $count)
    } finally {
    $tofile.Close()
    }
    }
    $idx ++
    } while ($count -gt 0)
    }
    finally {
    $fromFile.Close()
    }