Fast file search on every kind of Windows drive: six methods, one per drive type
· CHA SEONG HYUN
Fast file-name search on Windows needs a list (an index) built ahead of time. But each kind of drive allows different methods: NTFS on this PC, a PC without admin rights, USB sticks and memory cards, company and network drives, and cloud drives. Here is how to build that list on each of them quickly, without putting sudden load on the PC or the server.
0) First, the structure: split C# and Rust by role
The rule set in the original design was “a file explorer’s bottleneck is I/O and drawing, not the language”. So the languages were split by job, not by speed.
- C# .NET 10 + WPF (the app): windows, cleanup, previews, and gathering non-NTFS drives. Runs only with normal user rights. Since drawing is the bottleneck, the file list is drawn by hand (FastFileList): a folder of 100,000 items opens in 0.03 s.
- Rust (privileged and whole-disk work): the $MFT index service (SYSTEM, talks to the app over a named pipe), the parallel copy engine (a C ABI DLL via csbindgen), and the hotkey helper (Win32 only, about 1 MB of memory).
- Why separate processes: only the work that needs admin rights (reading $MFT) runs in the service, so the app never shows a UAC prompt. If one side hangs or crashes, the other stays up. The flip side: a SYSTEM service can’t see the signed-in user’s network or cloud drives, so the C# side gathers those itself.
- Keep the boundary small: work that ends up as the same Win32 call anyway (deleting folders is DeleteFileW) stayed in C# instead of moving to Rust. Every extra FFI boundary is more to test and more places to break.
1) NTFS: read $MFT instead of opening folders
NTFS keeps a table of every file ($MFT). Instead of opening each folder, read that table sequentially in 4–16 MB chunks and you get name, parent, size and timestamps in one pass: about 12 seconds for 1.8 million files. After that, follow the USN journal (the record of creates, deletes and renames).
- Store each node in about 40 bytes (parent id, name offset, size, time, subtree size) in flat arrays: about 40 MB per million entries.
- Save the list to a file and a restart restores it in about 0.1 seconds. It is only used when the trailing 64-bit hash, the volume serial, the journal ID, and the saved position (still inside the journal range) all match. If anything looks off, rebuild.
- Write to a
.tmpfile, sync it to disk, then rename. A power cut leaves the old file in place.
2) Without admin rights: read the USN journal as a normal user
Only administrators can read $MFT. But a normal user can call FSCTL_READ_UNPRIVILEGED_USN_JOURNAL on a handle to C:\. So re-read only folders whose contents changed since the last full pass and keep the previous list for the rest. A C: re-check went from 23 to 9 seconds (7 of 250,000 folders re-read).
- If the journal was recreated, more than 20,000 folders changed, or the volume is ReFS (128-bit file ids), don’t trust it and read everything.
- For search, keep only the names in memory as one block of upper-case UTF-8: 0.94 s → 2–11 ms (69 MB for C:).
3) Read less using folder times, and test whether the times can be trusted
A folder’s modified time changes only when something directly inside it changes. So for leaf folders (no subfolders), ask only for the time and reuse the previous list if it is unchanged.
- Don’t use the subfolder times listed in the parent’s directory listing. NTFS updates those late, so you miss changes (confirmed with a CI test).
- Whether folder times can be trusted differs per drive (on Google Drive the folder times did not change). If 3 or more folders, and more than 10%, have the same time but a different listing, read that drive fully every time. Halve that record every 2,000 checks so the drive gets another chance.
4) USB sticks and memory cards: step aside instead of sleeping
For directly attached drives, read one folder at a time on a thread in Windows background mode (THREAD_MODE_BACKGROUND_BEGIN). Its disk priority goes to the back of the queue, so it yields automatically when another program uses the disk.
- Measured: first pass on a memory card (exFAT) 353 s → 4.5 s, while another program’s read latency stayed at 3.8 ms.
- Catch: this mode also lowers CPU priority, so the thread nearly stalls when the CPU is busy. Restore the CPU priority.
5) Network and company drives: measure how many to read at once
Remote drives answer slowly, so reading several folders in parallel is faster. How many depends on the server. So try 4, 8 and 16 in turn and pick the smallest count whose actual time per folder is within 125% of the best (re-measure every 300 decisions).
- Judge speed over 48 folders or one second of work, and halve only after two slowdowns in a row.
- Start with a pause of 20% of read time and increase it only when replies are more than 3× slower than usual. Batch pauses shorter than 15 ms into one (the Windows timer ticks in about 16 ms).
- Start the next folder as soon as a slot frees up, so one slow folder doesn’t leave the other slots idle.
- Measured: a company security drive went from 2,000 to 7,600–12,800 entries per minute. Google Drive first pass 61 s → 7.5 s.
6) Resume after interruptions, and recognize a swapped disk
- While gathering, every 10 seconds save each folder’s time and list position (append-only) and the folders still to read, so it resumes where it left off after closing, updating, or the drive dropping.
- Plug in a different external disk and it can get the same E:. So name each list
drive letter#volume serial. The volume GUID was not used because it differs per PC even for the same disk.
What went wrong, and the fixes
- No folders showed up on a work drive. It was a cloud-style drive managed by a security product, with 29 GB in use, yet the result was “done, 0 files” and disk analysis said “0 files” too. The user’s guess, “is it because the folder is hidden?”, was right: the top folder was hidden and the folders inside were cloud-file folders (reparse points), and both were being skipped. The fixes:
- Hidden folders are entered. Only Windows folders (Recycle Bin, System Volume Information and so on) and hidden+system files are skipped.
- A reparse point that isn’t a link (no target) is entered; only junctions and symbolic links are skipped.
- If the top was read to the end, nothing was found, and the drive uses more than 64 MB, it reports “couldn’t read this drive” instead of “done”. Failing loudly beats being quietly wrong.
- Disk analysis stops trusting the gathered list when its total size is under a quarter of the used space, and reads the drive directly.
- A memory card took 353 s. At first it slept 10 ms per folder, but the Windows timer ticks in about 16 ms, so it slept longer and that added up per folder. “Work 50 ms, sleep 50 ms” was also slow (28–30 s alone) and still didn’t step aside when another program used the disk. Dropping sleeps for background mode got it to 4.5 s.
- Then it nearly stalled when the PC was busy. Background mode also sends CPU priority to the back, so in testing a busy PC took 0.2 s per folder. After entering the mode, CPU and memory priority are set back (without restoring memory priority, a re-gather took 12 s).
- Saving time checks made it re-read everything. To ask for fewer folder times, it recorded the subfolder times from the parent’s listing. NTFS updates those late, so the next comparison re-read almost every folder (confirmed in CI). It keeps asking each folder for its own time.
- Parallel, but still slow. On remote drives it read 4 folders at once but waited for all 4 before starting the next batch, so one slow folder left the other slots idle. Speed was also judged per request, so a few big folders threw it off (in a simulation: 1.2–1.4 in parallel on average, 32% of the time spent sleeping). Now it starts a folder as soon as a slot frees up and judges over 48 folders or one second of work.
- Lists from two disks got mixed. An interrupted pass on one external disk was resumed on another disk that came up as the same E:. So lists and resume records are keyed by
drive letter#volume serial, not the letter.
Where I implemented this
These methods are in Foldy File Manager, an open-source Windows file manager I’m building (Rust: $MFT parsing and the index service; C#: gathering other drives). Code and more detail are in the technical notes on GitHub. If you know a better way or spot a mistake, please tell me.