How to work with long paths on Windows?

Preetpal preetpal.sohal at gmail.com
Tue Sep 13 19:54:15 UTC 2022


In Windows 10, Version 1607 (and later), you can [enable long 
paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry) which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use [npm](https://www.npmjs.com/).

Example code:


```
import std.file;
import std.stdio;

/// Command line tool to find files in directories
int main(string[] args) {
     if (args.length < 2) {
         writefln("Usage: %s wildcard", args[0]);
         return 1;
     }
     string wildcard = args[1];

     auto results = dirEntries(".", wildcard, SpanMode.depth);
     foreach(string result; results) {
         writeln(result);
     }
     return 0;
}
```

Example problem:

   If you run it in a directory with paths containing exceeding 
MAX_PATH, it fails.


```
std.file.FileException at std\file.d(4648): 
.\my_webproject\my_webproject\my_webproject\node_modules\bootswatch\docs\3\node_modules\bower\node_modules\update-notifier\node_modules\request\node_modules\har-validator\node_modules\chalk\node_modules\has-ansi\node_modules\ansi-regex: The system cannot find the path specified.
```

How do you work around this issue on Windows? The issue has 
already been 
[reported](https://issues.dlang.org/show_bug.cgi?id=8967).


More information about the Digitalmars-d-learn mailing list