EXIST ignores leading slash
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
The EXIST function is reporting that a file exists when it does not. I'm trying to test the existence of the file '/license/license.json', which does not exist on my file system. The file 'license.json' is in my path, but the directory '/license' does not exist on my system.
>> exist('/license', 'dir')
ans =
0
>> exist('/license/license.json', 'file')
ans =
2
>> which('/license/license.json')
/Users/brian/apps/copilot/license/license.json
>> which('license.json')
/Users/brian/apps/copilot/license/license.json
Am I misunderstanding the use of EXIST? Why is it ignoring the leading slash in an absolute path?
UPDATE: a workaround to check the existence of a file given its absolute path is provided by OCDER below:
FileLoc = dir('/license/license.json')
IsFileThere = ~isempty(FileLoc) && any(~[FileLoc.isdir]);
8 Commenti
Walter Roberson
il 26 Lug 2018
Okay, I am able to reproduce this on OS-X in the circumstance that the directory containing the license directory is on the path (regardless of my current directory.)
Risposta accettata
OCDER
il 25 Lug 2018
Modificato: OCDER
il 26 Lug 2018
NEWER ANSWER
FileLoc = dir('/license/license.json')
IsFileThere = ~isempty(FileLoc) && any(~[FileLoc.isdir]);
NEW ANSWER
exist behaves differently when searching for a file vs a dir. For a file search, it will search for file name in the matlab path, a partial match from the right hand side are valid. SO
exist('/license/license.json', 'file') %Works because it's searching:
1) matlabpath/**/license/license.json (if fail, look at root)
2) root/license/license.json
When looking for a directory, the leading slash is immediately treated as the root directory. It doesn't seem to be looking for a partial match, unless the leading slash is removed.
exist('/license', 'dir') %Looks immediately for root/license
exist('license', 'dir') %Looks at matlab path, but NOT root or elsewhere
OLD ANSWER
The first '/' is treated as the hard drive ("C:\") for Windows 10 at least.
exist('/Users', 'dir') should work
exist('/Users/brian/apps/copilot/license', 'dir') should work
exist('/license', 'dir') should not work, because your license folder is NOT
at '/license' but '/Users/brian/apps/copilot/license'
exist('license', 'dir) should work, because you are now looking for CURRENT_FOLDER/license, where
CURRENT_FOLDER = '/Users/brian/apps/copilot'
11 Commenti
OCDER
il 26 Lug 2018
Ohh... In that case, dir seems better than exist due to the way exist handles a file search.
FileLoc = dir('/license/license.json')
IsFileThere = ~isempty(FileLoc) && any(~[FileLoc.isdir]);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su File Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!