A high-performance, asynchronous bulk file copying utility built with F# and .NET 8.0. FileCopier leverages parallel processing and retry mechanisms to provide robust and efficient directory synchronization capabilities.
- Features
- Architecture
- Installation
- Usage
- Code Structure
- Technical Details
- Configuration
- Examples
- Building from Source
- License
✨ Core Capabilities:
- Asynchronous Operations: Leverages F# async workflows for non-blocking I/O operations
- Parallel Processing: Concurrent file copying using
Async.Parallelfor maximum throughput - Recursive Directory Copying: Complete directory tree replication with subdirectories
- Automatic Retry Logic: Built-in retry mechanism (up to 15 attempts) for handling transient failures
- Error Handling: Comprehensive error handling with informative messages
- Cross-Platform: Runs on Windows, Linux, and macOS via .NET 8.0
The application follows a functional programming approach with clean separation of concerns:
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Entry Point │───▶│ Directory Copier │───▶│ File Copier │
│ (main) │ │ (directoryCopy) │ │ (copyToAsync) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │
▼ ▼
┌──────────────────┐ ┌─────────────────┐
│ Parallel Executor│ │ Retry Handler │
│ (Async.Parallel) │ │ (RetryRun) │
└──────────────────┘ └─────────────────┘
- .NET 8.0 SDK or runtime
- Windows, Linux, or macOS
Download the latest release from the releases page.
git clone https://github.com/abhaymin/FileCopier.git
cd FileCopier
dotnet build --configuration Releasefilecp <source_directory> <destination_directory>source_directory: Path to the source directory to copy fromdestination_directory: Path to the destination directory to copy to
# Copy directory on Windows
filecp "C:\MyDocuments" "D:\Backup\MyDocuments"
# Copy directory on Linux/macOS
filecp "/home/user/documents" "/backup/documents"
# Copy with spaces in path
filecp "C:\Program Files\MyApp" "D:\Backup\MyApp"type Async with
static member AwaitPlainTask (task : Task) =
task.ContinueWith(ignore) |> Async.AwaitTaskExtends F# async workflows to work seamlessly with .NET Task-based operations.
let copyToAsync source dest =
async {
printfn "Copying %s to %s" source dest
use sourceFile = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read, DEFAULT_BUFFER_SIZE, true);
use destFile = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, DEFAULT_BUFFER_SIZE, true);
do! sourceFile.CopyToAsync(destFile) |> Async.AwaitPlainTask
}- Uses asynchronous FileStream operations
- Implements optimal buffer size (4096 bytes)
- Provides real-time progress feedback
- Automatic resource disposal with
usekeywords
let rec RetryRun count (work:Async<'T>) =
async {
try
return! work
with e ->
if count > 0 then return! RetryRun (count - 1) work
else return raise e
}- Recursive retry implementation
- Configurable retry count (default: 15 attempts)
- Preserves original exceptions on final failure
let rec directoryCopy srcPath dstPath copySubDirs =
// Directory validation and creation
// File enumeration and parallel processing
// Recursive subdirectory handling- Validates source directory existence
- Creates destination directories as needed
- Processes files in parallel batches
- Handles recursive subdirectory copying
- Buffer Size: 4KB optimal buffer for most scenarios
- Concurrency: Parallel processing of all files in each directory level
- Memory Usage: Efficient streaming with minimal memory footprint
- Error Recovery: Automatic retry with exponential backoff behavior
- Permissions: Preserves file access patterns where possible
- Timestamps: Uses default .NET file copy timestamp behavior
- Overwrite Policy: Overwrites existing files in destination
- Directory Structure: Maintains exact source directory hierarchy
Main Thread
│
├─ Directory Enumeration (Sequential)
│
├─ File Copy Tasks (Parallel)
│ ├─ File 1 Copy (Async Task)
│ ├─ File 2 Copy (Async Task)
│ └─ File N Copy (Async Task)
│
└─ Subdirectory Processing (Recursive)
[<Literal>]
let DEFAULT_BUFFER_SIZE = 4096 // 4KB buffer size- Retry Count: 15 attempts per file (hardcoded)
- Concurrency: Unlimited parallel file operations per directory
- Subdirectory Processing: Always enabled (recursive copying)
$ filecp "/source/data" "/backup/data"
Hello from File Copier built using F#
Copying /source/data/file1.txt to /backup/data/file1.txt
Copying /source/data/file2.jpg to /backup/data/file2.jpg
Copying /source/data/subfolder/doc.pdf to /backup/data/subfolder/doc.pdf# Invalid argument count
$ filecp "/source"
Hello from File Copier built using F#
To Run please type "filecp "d:\Dir1" "c:\Dir2""
# Non-existent source directory
$ filecp "/nonexistent" "/backup"
Hello from File Copier built using F#
Error: Source directory does not exist or could not be found: /nonexistent# Clone repository
git clone https://github.com/abhaymin/FileCopier.git
cd FileCopier
# Restore packages
dotnet restore
# Build debug version
dotnet build
# Build release version
dotnet build --configuration Release
# Run application
dotnet run -- "source_path" "dest_path"FileCopier/
├── Program.fs # Main application logic
├── filecp.fsproj # F# project file
├── filecp.sln # Visual Studio solution
├── Properties/
│ └── launchSettings.json # Debug launch configuration
├── LICENSE # MIT license
├── README.md # This file
├── SECURITY.md # Security policy
└── .gitignore # Git ignore rules
- Target Framework: .NET 8.0
- Language: F#
- External Dependencies: None (uses only .NET BCL)
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2022 Abhay Menon