필터 지우기
필터 지우기

Check if filename is valid.

조회 수: 95 (최근 30일)
Qingyang
Qingyang 2012년 7월 20일
편집: Voss 2023년 12월 19일
Hi, is there a native function or a simple code to check if a user-typed filename is valid for saving a file? (In that it doesn't contain illegal characters like /\":?<>|, etc.)

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 7월 20일
regexp it!
  댓글 수: 4
Jason Nicholson
Jason Nicholson 2014년 2월 14일
This code snippet will not work unless you check if it is empty.
if ~isempty(regexp(filename, '[/\*:?"<>|]', 'once'))
%Do something
end
Voss
Voss 2023년 12월 19일
편집: Voss 2023년 12월 19일
You need to escape the backslash in the regular expression for that to work right:
~isempty(regexp('file\name','[/\*:?"<>|]', 'once')) % no match
ans = logical
0
~isempty(regexp('file\name','[/\\*:?"<>|]', 'once')) % match
ans = logical
1
Of course, on Windows, if absolute or relative path names are allowed, then backslash is a valid character, and if absolute path names are allowed, then colon is a valid character. On Mac/Linux, if absolute or relative path names are allowed, then slash is a valid character.

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

추가 답변 (5개)

Daniel Shub
Daniel Shub 2012년 7월 23일
A long time ago, probably Windows 95 on a FAT16 harddisk, I started using the following:
regexp(fname, ['^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)', ...
'(\..+)?$)[^\x00-\x1f\\?*:\"><|/]+$'], 'once'));
At that time, Windows had the most restrictive file name conventions. I am not sure what the rules are now. You also should check for the length of the path and the total number of characters in the file name.
  댓글 수: 1
Jan
Jan 2012년 7월 23일
Windows 7: Either 260 characters, or start with //?/ and 32767 characters, while each folder can have up to 255 characters. The later works for most of the API-functions, but not e.g. for the Windows Explorer. Moving a file name with a long name to the RecycleBin fails also. The backup program and server based tools might fail also. Therefore I'd prefer the dull limit of 260 characters (including "C\:" and a trailing terminator '\0')!).

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


ola bayo
ola bayo 2017년 10월 16일
편집: Walter Roberson 2017년 10월 16일
Which following is a valid file name in MATLAB?
A) lab 2
B) lab2
C) lab-2
D) 2lab
E) LAB 2
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 10월 16일
For the question as phrased: all of them. For a slightly different question, the answer would be fairly different.

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


Thomas
Thomas 2017년 12월 1일
If you do not want to use UIputfile, another option is fopen. fileID = fopen(filename,'w') if fopen cannot open the file, then fileID is -1.

Daniele Busi
Daniele Busi 2020년 2월 24일
function bool = isLegalPath(str)
bool = true;
try
java.io.File(str).toPath;
catch
bool = false;
end
end

George Abrahams
George Abrahams 2023년 12월 19일
편집: George Abrahams 2023년 12월 19일
Hi Qingyang, I recently released the isvalidpath function on File Exchange which does exactly this!
Like @Daniele Busi's answer, it's based on java.io.File().toPath, which is more robust than anything we'll build ourselves, but isvalidpath also provides:
  • The option to validate that the path refers to a file, rather than a directory, or vice versa.
  • The option to validate that the file extension matches a given set, for example, that the path refers to an image.
  • Human readable feedback, in case the validation fails, for example, a warning that the path lacks a filename or contains an illegal character.

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by