'exist' function returning false positive for directory.
显示 更早的评论
I'm trying to check if a file/directory path is valid before using it. I've seen countless examples of the 'exist' command to check to see if the argument is real, but I've found that MATLAB lies to you if you if the directory is somewhere on the path, but not accessible.
Consider the following example.
cd C:/
mkdir a; cd a
mkdir b; cd b
mkdir c; cd c
% Now current directory is C:/a/b/c
% I'm given an input dir called "b/c"
exist('b/c','dir') % Returns 7 - Indicating that "directory does exist"
% Thinking that's a real directory, I attempt to access it and get an error.
cd b/c
%:: Error using cd
%:: Cannot CD to C:\a\b\c\b\c (Name is nonexistent or not a directory).
What alternatives to 'exist' are there to correctly identify the existence of a folder or file if and only if:
- the absolute path, relative to '/' or 'C:', exists, or
- the relative path, from the current directory, exists?
I could try shelling out to cmd, but that seems hacky:
system('IF EXIST b/c (exit /B 1) ELSE (exit /B 0)')
Any suggestions?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!