CodeGym /Courses /Kotlin SELF /Bulk operations engine: copy/move, partial failures, and ...

Bulk operations engine: copy/move, partial failures, and results

Kotlin SELF
Level 46, Lesson 3
Available

1. Why you need a “bulk operations engine”

Honestly, copying one file is a “mom, look, I’m a programmer” level task. But as soon as you have 3000 files, it suddenly turns out the world is full of surprises: somewhere you don’t have permissions, somewhere the name is too long, somewhere a file with the same name already exists, somewhere the disk is full, and somewhere renameTo() just said “false” and walked away without explanations (like a cat you asked to take a bath).

That’s exactly why we need a bulk operations engine: a layer of code that takes a plan (a list of “what goes where”) and executes operations one item at a time, carefully accumulating results. The main principle of this lecture: an error on one file must become a record in the report, not the end of the program.

To keep your head organized, it’s useful to think of the process as a pipeline:

flowchart TD
    A[PlanItem list: source -> target] --> B[Operation on one file]
    B --> C[OpResult: status/error]
    C --> D[List of results]
    D --> E[Report/statistics]

2. Operation results: statuses and reporting

Statuses and the result model

Before we touch anything on disk, let’s agree on what we consider a “result”. In bulk operations, the result is not only “success”, but also “skipped” and “failed”. If we reduce everything to Boolean, we lose the explanation of why a file was skipped or why it failed. And then we’ll be “fixing” the program with shaman dances and restarts.

So we’ll introduce Status as an enum class and OpResult as a result data model. By the way, enums in Kotlin have a handy entries collection for iterating values (in newer Kotlin versions this is a distinct feature that makes enums a bit nicer to live with).

A meanings table for statuses — to make the report easier to read:

Status What it means in our File Organizer
COPIED
The file was successfully copied to target
MOVED
The file was successfully moved to target
SKIPPED
We deliberately did nothing (conflict + policy
SKIP
, or another “legal” skip)
FAILED
The operation attempted to run but could not (exception,
renameTo == false
, failed to create a directory, etc.)

Mini-models:

enum class Status { COPIED, MOVED, SKIPPED, FAILED }

data class OpResult(
    val source: String,
    val target: String,
    val status: Status,
    val error: String? = null
)

Note: we store paths as String. Yes, somewhere we have File and Path, but for a report it’s more convenient for a human to see strings. Also, strings are easier to serialize/write to a text log (in this lecture we only prepare the data; a nice report comes later).

Accumulating results and simple statistics

At first glance, List<OpResult> is just a regular list. In practice, it’s the most important “artifact” of a run. If you lose it or forget to fill it, you lose the ability to clearly explain to the user what happened.

The results list has two uses.

First: a human-readable report (later we’ll assemble nice text). Second: statistics — how many succeeded, how many were skipped, how many failed. And you can do it literally in one line via groupBy, because OpResult is a normal data structure.

fun countByStatus(results: List<OpResult>): Map<Status, Int> {
    return results.groupBy { it.status }
        .mapValues { (_, list) -> list.size }
}

It’s important to understand the idea: toList() and similar operations create a “snapshot” of a collection at a moment in time — this is often useful when you want to separate source data from the result.
We’re not diving into performance or subtleties yet, but the principle “results are separate data; you can analyze them” is key.

3. Operations on one file: copying, moving, failures

Why “one file → one result” matters

From the outside, bulk processing looks like a normal for. But architecturally these are two different tasks: “how to process one file” and “how to process a thousand files”. If you mix them into one giant block, chaos will move in quickly: half the checks will be about conflicts, a quarter about directories, another part about try/catch, and in the end you’ll find you forgot to add the result to the list.

The right decomposition is: make a small function that tries to process one file and returns OpResult. And the loop over the whole plan will just call it many times and collect the results.

There’s also a subtle practical benefit: you’ll be able to test and debug the logic on a single example (one file) without running the whole plan every time.

Preparing the destination directory

One of the most common newbie surprises: “I’m copying a file, why did it crash even though the target path looks nice?” The answer is usually boring: because the destination folder doesn’t exist yet. You want to put the file into target/images/2026/january/photo.jpg, but the folder target/images/2026/january/ hasn’t been created.

So before any write/copy/move, we must ensure that target.parentFile exists, or try to create it via mkdirs().

Let’s make a small helper:

import java.io.File

fun ensureParentDir(target: File): Boolean {
    val parent = target.parentFile ?: return false
    return parent.exists() || parent.mkdirs()
}

Here we return Boolean because this is purely “technical preparation”: if it worked — great; if it didn’t — there’s no point trying to copy further.

Copying one file: a “narrow” try/catch

Now we’re ready for the “copy one file” operation. Here it’s important not to try to catch literally everything under the sun, while also not crashing on the first issue. A simple compromise for a learning project: catch IOException (and optionally a more general Exception as a safety net) and turn it into FAILED.

Another important nuance: copyTo() in Kotlin works with File and can throw exceptions. We’re not trying to “defeat” them — we record them in the result.

import java.io.File
import java.io.IOException

fun copyOne(source: File, target: File): OpResult {
    return try {
        if (!ensureParentDir(target)) {
            return OpResult(source.path, target.path, Status.FAILED, "cannot create target directory")
        }

        source.copyTo(target)
        OpResult(source.path, target.path, Status.COPIED)
    } catch (e: IOException) {
        OpResult(source.path, target.path, Status.FAILED, e.message)
    }
}

Notice the style: we do an early return when there’s a directory problem. This is called a “guard clause”, and it really saves you from nested if statements inside try.

Moving one file: why renameTo() can silently fail

Moving is a bit more fun, because many people expect “move” to be just “cut and paste”. In practice they often do source.renameTo(target). And here Kotlin/Java sometimes behave like a cat: no exceptions, just false. That is, the operation did not happen, but “there is no error”. Thanks, very informative.

So we’ll also implement moving as “one file → one result” and обязательно check the Boolean:

import java.io.File

fun moveOne(source: File, target: File): OpResult {
    return try {
        if (!ensureParentDir(target)) {
            return OpResult(source.path, target.path, Status.FAILED, "cannot create target directory")
        }

        val ok = source.renameTo(target)
        if (ok) OpResult(source.path, target.path, Status.MOVED)
        else OpResult(source.path, target.path, Status.FAILED, "renameTo returned false")
    } catch (e: Exception) {
        OpResult(source.path, target.path, Status.FAILED, e.message)
    }
}

We’re not diving into platform-specific filesystem subtleties yet. For a learning project it’s enough to clearly understand the practical rule: if renameTo() returned false, it’s the same kind of failure as an exception — just without a stack trace.

Where to put try/catch so processing is “resilient”

When people first run into I/O errors, they get a strong urge to do this:

try {
    // processing the ENTIRE plan
} catch (e: Exception) {
    // sad println
}

The problem is that it turns one error (for example, one “broken” file) into a failure of the entire run. The user gets half the work done (or none) and minimal information.

We need a “partial failures” model: we process a list, and each file is either successful or yields a failure-result, while the rest keep being processed.

That’s why try/catch should be placed inside the operation on a single file (copyOne/moveOne) or around the logic for one plan item, not around the entire loop. Then a “bad” file becomes one FAILED record, and we move on.

It sounds philosophical, but in practice it simply means: “catch narrowly”.

4. Executing the plan: loop, conflicts, and wiring in main

executePlan(): one loop and a unified conflict protocol

Now the best part: we have PlanItem (from the previous lecture about the layout plan), we have Mode and ConflictPolicy (from the contract), and we have copyOne/moveOne. All that’s left is to assemble it into one engine executePlan().

First, here are minimal models (so the code below reads as a cohesive unit). We won’t complicate them with extra fields — we only need what’s required for execution and reporting.

import java.io.File
import java.nio.file.Path

data class PlanItem(
    val source: File,
    val relative: Path,
    val target: File
)

enum class Mode { COPY, MOVE }
enum class ConflictPolicy { SKIP, BACKUP }

Note: relative is barely needed for execution (target is already computed), but it can be useful for reporting, so let it live.

Now the “engine” itself. In this lecture we implement only SKIP as the conflict policy (and BACKUP was discussed earlier and can be plugged in if we have a helper). The main point is to show a structure where the conflict is handled before attempting copy/move.

fun executePlan(plan: List<PlanItem>, mode: Mode, policy: ConflictPolicy): List<OpResult> {
    val results = mutableListOf<OpResult>()

    for (item in plan) {
        if (item.target.exists() && policy == ConflictPolicy.SKIP) {
            results += OpResult(item.source.path, item.target.path, Status.SKIPPED, "target exists")
            continue
        }

        val r = when (mode) {
            Mode.COPY -> copyOne(item.source, item.target)
            Mode.MOVE -> moveOne(item.source, item.target)
        }
        results += r
    }

    return results
}

Notice how readable when is here: it literally says “depending on the mode, we copy or move”. And this is exactly the case where an enum is better than a Boolean, because with a Boolean you’d have to remember what true means (copy? move? or “dry run”?), while Mode.MOVE explains itself.

Minimal wiring in main

To make everything look like a single application (rather than a set of unrelated functions), it’s useful to picture the final wiring in main: earlier we built the config, candidates, and plan, and now we call the engine and print a short summary. Here I’ll show the “skeleton”, without the details of directory traversal and plan building (those were covered in previous lectures).

fun main() {
    val plan: List<PlanItem> = buildPlanSomehow() // from previous lectures
    val results = executePlan(plan, mode = Mode.COPY, policy = ConflictPolicy.SKIP)

    val stats = countByStatus(results)
    println("Done. Stats: $stats") // Done. Stats: {COPIED=10, SKIPPED=2, FAILED=1}
}

Yes, buildPlanSomehow() is a stub here — because today we’re learning the “engine” itself, not directory traversal and not layout rules. In a real project you’ll plug in your collectCandidates(...) and buildPlan(...) here.

5. Typical mistakes when implementing a bulk operations engine

Mistake #1: one global try/catch around the entire run.
When try/catch wraps the whole batch, any error turns into “everything crashed”. The user gets fewer processed files, and you get less information. It’s far more robust to catch errors at the “one file → one OpResult” level, so the rest of the processing continues.

Mistake #2: not creating the destination directory before copying/moving.
It’s very easy to forget that you can’t put a file into a folder that doesn’t exist. If you don’t do target.parentFile.mkdirs(), copying will fail (or return an error) even with perfectly correct paths. The habit “first ensureParentDir, then the operation” makes behavior predictable.

Mistake #3: treating renameTo() as reliable and not checking the result.
renameTo() can return false without throwing, and if you ignore that, you get a “silent failure”: the program says “all good”, but the file stays in place. In a bulk operations engine, false must become Status.FAILED with a clear message, otherwise debugging turns into detective work.

Mistake #4: losing the failure reason.
If in catch you set only Status.FAILED without text, you destroy the most useful part of the result — the explanation. Even e.message (even if it’s sometimes null) helps a lot to understand what happened: “Permission denied”, “No space left on device”, “File not found”.

Mistake #5: mixing plan building and execution in one place.
When a function categorizes a file and immediately copies it, you lose control: it’s harder to do a “dry-run”, harder to test, harder to write a report. It’s much cleaner to keep the rule: the plan is pure computation, the engine is I/O and error handling, and the result is data for reporting.

1
Task
Kotlin SELF, level 46, lesson 3
Locked
Operation statuses
Operation statuses
1
Task
Kotlin SELF, level 46, lesson 3
Locked
Directory before writing
Directory before writing
1
Task
Kotlin SELF, level 46, lesson 3
Locked
Copying a File
Copying a File
1
Task
Kotlin SELF, level 46, lesson 3
Locked
Moving a file
Moving a file
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION