EXIST ignores leading slash

조회 수: 5 (최근 30일)
Brian Keating
Brian Keating 2018년 7월 25일
편집: OCDER 2018년 7월 26일
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
OCDER
OCDER 2018년 7월 25일
편집: OCDER 2018년 7월 25일
So what's the unexpected result then? Use this to determine what a leading slash is considered by exist.
SlashDir = dir('/*');
SlashDir(1).folder
%I get 'C:\' for windows 10
Walter Roberson
Walter Roberson 2018년 7월 26일
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.)

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

채택된 답변

OCDER
OCDER 2018년 7월 25일
편집: OCDER 2018년 7월 26일
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
OCDER
OCDER 2018년 7월 26일
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]);
Brian Keating
Brian Keating 2018년 7월 26일
Thanks, OCDER. I'm surprised that this isn't how EXIST works, but the code you posted above does what I need.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by