Windows Client Operations Series

🔤 Why Do Files With Identical-Looking Names Have Different Character Counts in Windows? Understanding Unicode Normalization and BOX Migration Trouble From a "Top 1%" Perspective

Introduction

This article is part of the Top 1% Series’ full article guide, and the third article in the Windows Client Operations Series.

Prerequisites

Getting the Big Picture

In a Nutshell

Japanese characters with a dakuten/handakuten mark (such as が or ぱ), and Latin characters with an accent mark (such as é), can be represented in Unicode in two different ways: “as a single code point” or “as a combination of multiple code points.” These two are perfectly identical on-screen (as a glyph), but their internal sequence of code points — which is what gets counted as the character count — differs.

graph LR
    subgraph Nfc["Precomposed form (NFC)"]
        NfcChar["\"が\"<br/>= a single code point, U+304C<br/>(character count: 1)"]
    end
    subgraph Nfd["Decomposed form (NFD)"]
        NfdChar["\"か\" (U+304B) + a combining character for the dakuten (U+3099)<br/>= a combination of two code points<br/>(character count: 2)"]
    end
    Nfc -.identical on-screen appearance.-> Nfd

Fundamentals, Explained Thoroughly

Why the Displayed Character Count Differs

Running a command like the following in PowerShell to compare the character count of filenames that look identical can actually return different numbers:

Get-ChildItem | Select-Object Name, @{Name="Length"; Expression={$_.Name.Length}}

What this command’s .Length returns is the number of code points making up that string (more precisely, the number of UTF-16 code units, .NET’s internal string representation). As noted above, if a character with a dakuten is saved in its precomposed form (NFC, Normalization Form C), it counts as one character; if saved in its decomposed form (NFD, Normalization Form D), the base character and its combining character together count as two (or more) characters. Since the on-screen display (the glyph) looks exactly the same “が” either way, this difference is impossible to notice just by looking at it.

What is a combining character?

A combining character is a special code point that isn’t displayed on its own — it adds a decoration, such as “attach a dakuten” or “attach an accent mark,” to the character immediately before it. Beyond Japanese dakuten/handakuten marks, many languages’ accented characters — French accent marks, German umlauts, and so on — can be represented in this decomposed form using combining characters.

Why These Can Coexist as Separate Files in Windows

What matters here in practice is that Windows’s file system (NTFS) doesn’t perform Unicode normalization when comparing filenames. NTFS does compare filenames case-insensitively, but it doesn’t perform the normalization that would treat the differing code-point sequences of a precomposed and a decomposed form as “the same name.” So even though a filename like “が.txt” looks completely identical, if one copy is encoded in precomposed form and the other in decomposed form, the OS recognizes them as two different filenames, and they can coexist as separate files within the same folder.

Why BOX Judges Them as Files With the Same Name

Many cloud storage services (BOX and others), on the other hand, typically perform Unicode normalization before comparing filenames during upload and sync processing (usually normalizing to NFC form before comparing). Once normalization is applied, a filename encoded in either precomposed or decomposed form gets converted to the same normalization form (say, NFC) before being compared, so two filenames that look identical are judged, at that point, as exactly the same string.

This difference in normalization policy between the two systems is the true cause of the migration trouble. Files that coexisted fine in Windows as separate files, since it performs no normalization, get normalized during the upload to BOX, and a collision occurs — “a file with this name already exists” — causing the migration to fail.

sequenceDiagram
    participant Ntfs as Windows (NTFS)
    participant CloudStorage as BOX

    Note over Ntfs: "が.txt" (NFC form) and<br/>"が.txt" (NFD form)<br/>coexist as separate files
    Ntfs->>CloudStorage: Both files are uploaded
    Note over CloudStorage: At upload time, filenames are normalized to NFC and compared
    CloudStorage-xNtfs: Judged as "a file with this name already exists" — one collides and fails

Where Does a Decomposed-Form Filename Actually Come From?

In practice, this decomposed-form filename issue is often encountered with files created or copied from macOS. The file system macOS traditionally uses (HFS+ and others) had a historical specification of normalizing and saving filenames in decomposed form (NFD), and traces of this remain in some workflows even today. Filenames generated by certain scanner devices, OCR software, or some legacy systems can also end up unintentionally encoded in decomposed form. Organizations that frequently copy or share files between Mac and Windows environments carry a particularly high risk of running into this problem — a practical tendency worth keeping in mind, as it’s a useful clue when investigating the cause.

The View From the Top 1% Perspective

Unifying the Normalization Form with PowerShell

Detecting decomposed-form filenames before a migration and unifying them into precomposed form (NFC) can head off this kind of collision before it happens. PowerShell’s .Normalize() method lets you convert a string to a specified normalization form.

# Example: normalize the current filename to NFC form and rename it
Get-ChildItem -Recurse | ForEach-Object {
    $normalizedName = $_.Name.Normalize([System.Text.NormalizationForm]::FormC)
    if ($_.Name -ne $normalizedName) {
        Rename-Item -Path $_.FullName -NewName $normalizedName
    }
}

Before a large-scale migration, scanning the entire target folder with a script like this to detect and fix filenames with an inconsistent normalization form ahead of time significantly reduces the risk of a failed migration. This can’t be caught by eye, so mechanical detection via a script is essential in practice.

Normalization Policy Varies by Tool and Service

“Surely the OS or application normalizes this automatically” is an expectation you shouldn’t rely on. Whether normalization is performed at all, and if so, which normalization form is used (NFC/NFD/NFKC/NFKD, and so on), varies by policy across the OS, file system, cloud service, and application. When handling files across multiple systems in practice, checking ahead of time what normalization policy each system adopts is important for avoiding this kind of trouble.

Common Misconceptions and Pitfalls

The Troubleshooting Perspective

For the phenomenon of identical-looking filenames being treated as separate, the basic approach is to compare the character (code point) count to mechanically detect the difference in normalization form.

  1. An error like “a file with this name already exists” occurs when migrating to BOX or similar: In the source folder, use Get-ChildItem’s .Length property to check whether identical-looking filenames actually differ in character count.
  2. A difference in character count is found: Unify the normalization form with the .Normalize() method, rename the files, and retry the upload/sync.
  3. You want to identify where the problematic files originated from: Check that file’s creation/modification history, and whether there’s a path by which files were copied from a Mac environment within the organization.

Preventive Measures and Permanent Fixes

Summary

What to Keep in Mind From Today

  1. If you run into an unexplained “duplicate filename” error during a cloud storage migration, check for a character-count difference using Get-ChildItem’s .Length property.
  2. If you regularly share files with a Mac environment, keep in mind that normalization-form inconsistencies are more likely to occur.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).