How to Split and Merge Files

CK CK 3788 
Created at  
619 0 0
There are times when you need to split a large file into several smaller ones (for whatever the purpose is). The following two methods use Delphi TStream objects to break a file into several files with predefined size; and to merge those files back to the original.

procedure SplitFile(FileName : TFileName; FilesByteSize : Integer) ;
// FileName == file to split into several smaller files
// FilesByteSize == the size of files in bytes
var
   fs, ss: TFileStream;
   cnt : integer;
   SplitName: String;
begin
   fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite) ;
   try
     for cnt := 1 to Trunc(fs.Size / FilesByteSize) + 1 do
     begin
       SplitName := ChangeFileExt(FileName, Format('%s%d', ['._',cnt])) ;
       ss := TFileStream.Create(SplitName, fmCreate or fmShareExclusive) ;
       try
         if fs.Size - fs.Position < FilesByteSize then
           FilesByteSize := fs.Size - fs.Position;
         ss.CopyFrom(fs, FilesByteSize) ;
       finally
         ss.Free;
       end;
     end;
   finally
     fs.Free;
   end;
end;


Note: a 3 KB file 'myfile.ext' will be split into 'myfile._1', 'myfile._2','myfile._3' if FilesByteSize parameter equals 1024 (1 KB).
Tags CopyFrom File I/O Status Check Long Path Name Merge Files Split FIle TFileStream Facebook X
Comments 0
Similar posts
  1. Path shortener: c:\ABC...DEF.ghi
    441
  2. Reading a directory content
    442
  3. Get File 'Last Modified' attribute
    465
  4. From/to the 8.3 (short) format to/from the long format
    579
  5. Delete folders recursively
    423
  6. Delete files with the ability to UNDO
    439
  7. Does my CD-ROM drive contain an audio CD?
    364
  8. Convert a mapped drive to a full UNC path
    832
  9. Checking If File Is In Use
    921
  1. Implementing a lasso drawing technique
    382
  2. Grayscaling a bitmap ?
    531
  3. Get Cursor Image (draw it on a Canvas)
    681
  4. How to convert HIcon to TIcon ?
    448
  5. Extract an icon from an Windows application and paint on a Form
    415
  6. How to draw transparent text on Windows Desktop
    414
  7. How to draw rotated text
    1,331
  8. How to convert an ICO to a BMP
    361
  9. How to Convert Pixels to Millimeters
    622
  10. How to capture Windows Desktop to Bitmap
    560
  11. Cut a rectangle from an Image to Clipboard
    537
  12. Convert TColor to Hex & Hex to TColor
    590
  13. Convert a BMP to a JPG
    408
  14. Reading a directory content
    442
  15. Path shortener: c:\ABC...DEF.ghi
    441
  16. Get File 'Last Modified' attribute
    465
  17. From/to the 8.3 (short) format to/from the long format
    579
  18. Delete folders recursively
    423
  19. Delete files with the ability to UNDO
    439
  20. Does my CD-ROM drive contain an audio CD?
    364
  21. Convert a mapped drive to a full UNC path
    832
  22. Checking If File Is In Use
    921
  23. Browse for Computers, Folders, Files and Printers
    571
  24. How to draw Transparent Text on bitmap
    386
  25. How Do I Remove The Application Icon From The Taskbar?
    1,098
  26. How To Show The Print Dialog And Print Text Files
    1,295
  27. How To Make An Animated Application Icon
    820
  28. How To Pass The Focus To The Next/prior Control
    1,209
  29. How To Make Rounded Windows
    1,227
  30. How To Get Windows Uptime
    1,476
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    260