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 amkdir b; cd bmkdir 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?
Best Answer