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

조회 수: 11 (최근 30일)
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
Image Analyst
Image Analyst 2022년 5월 2일
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 2022년 5월 2일
편집: Florian Berzsenyi 2022년 5월 2일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Florian Berzsenyi
Florian Berzsenyi 2022년 5월 2일
편집: Florian Berzsenyi 2022년 5월 2일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by