uigetfile adds "\\?" to a selected path that is technically too long

11 visualizzazioni (ultimi 30 giorni)
I have found a curios error (feature) about uigetdir() that only triggers when two very specific conditions are met:
  1. A file path on a network drive must be > 255 chars long.
  2. The sytem limit to the path length must be 255 char. You cannot create directories longer than that but you can apparently still open these directories.
The result is that uigetdir() returns an extra "\\?\":
dir = '\\?\L:\Folder\To\AVery\Long\Path\With\A\file.xls' % path > 255 (e.g. length(dir) == 271)
%dirShouldBe = '\L:\Folder\...'
I enter the following in the Matlab console:
[file, dir] = uigetfile({'*.xl*;*.xlsx;*.xlsm;*.xlm','Excel Files'}, 'Select a file...')
Unfortunately, I cannot reproduce this error because I cannot create a directory with more than 255 chars on my machine, locally or on the network. Has anybody else noticed this kind of behavior?
  2 Commenti
Image Analyst
Image Analyst il 2 Mag 2022
dir is the name of a built-in function. Don't use it as the name of a variable. Call it folder instead. (This probably won't fix the problem though.)
If you can't create folders with path lengths longer than the OS limit, then how were they created?
Florian Berzsenyi
Florian Berzsenyi il 2 Mag 2022
Modificato: Florian Berzsenyi il 2 Mag 2022
I have used another varname, something like 'asd' but renamed it for the example.
While I cannot say for sure, I may be able to create a directory with 256 path length and then change the name of a parent folder to something longer.

Accedi per commentare.

Risposta accettata

Florian Berzsenyi
Florian Berzsenyi il 2 Mag 2022
Modificato: Florian Berzsenyi il 2 Mag 2022
I found it out! This has nothing to do with MATLAB but with the Win32 File Namespaces (Link):
For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system. For example, if the file system supports large paths and file names, you can exceed the MAX_PATH limits that are otherwise enforced by the Windows APIs.
Basically Windows add a prefix to the path once its length exeeds MAX_PATH (=260) characters. The best solution is to use shorter path names.
Removing the prefix with
sourceFile = extractAfter(sourceFile, "\\?\");
will not help because the I/O functions will fail. The path is too long after all.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by