EXIST ignores leading slash

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 Comments

The behaviour of which when given a path is not really specified. The documentation says
If item is a file name including the extension, and it is in the current working folder or on the MATLAB path, then which displays the full path of item.
which is not incompatible with the idea that the leading part would be stripped off and then the working folder and MATLAB path checked for what was left.
In a quick test on my OS-X system, MATLAB appears to be respecting leading /, at least in the version I tested.
Which release are you using?
Is there a better way to check whether or not the file exists in my filesystem? If EXIST strips the leading slash, how can I check for the existence of a file using its absolute path? I'm trying to avoid using UNIX calls
I get this behavior in 2018a and 2016b on OSX
Did this work?
exist('/Users/brian/apps/copilot/license', 'dir')
yes, it returns 7 as expected
OCDER
OCDER on 25 Jul 2018
Edited: OCDER on 25 Jul 2018
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
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.)

Sign in to comment.

 Accepted Answer

OCDER
OCDER on 25 Jul 2018
Edited: OCDER on 26 Jul 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 Comments

No, this is not correct. The exist() of the file would not return 2 if the /license/license.json did not exist .
The user has two license.json files, one hanging just off the root directory, and it is indeed strange that this is happening.
I have one license file at /Users/brian/apps/copilot/license/license.json, and it is in my path. I do not have a directory /license.
It's something with Windows.
cd('/Users')
will get me to C:\Users no matter where my current directory is.
Brian Keating
Brian Keating on 26 Jul 2018
Edited: Brian Keating on 26 Jul 2018
Your NEW ANSWER makes sense, and would explain what I'm seeing. I'm still not sure how to check if the file '/license/license.json' actually exists on my file system - not in my path, but on my hard drive.
OCDER
OCDER on 26 Jul 2018
Edited: OCDER on 26 Jul 2018
I don't think exist can do a full or wildcard search. How about this?
%Warning. It'll take while to do a full search
JSONFile = dir(fullfile('/**','license','license.json'))
if ~isempty(JSONFile) && ~JSONFile(1).isdir
disp(fullfile(JSONFile(1).folder, JSONFile(1).name));
else
disp('could not find file');
end
If you do not have a directory named /license then why check for /license/license.json ?
If what you want to check is for license/license.json relative to your current path, you would not use a leading / for that . If you find that asking about license/license.json checks along the path and you do not want that, then ask about ./license/license.json
"If you do not have a directory named /license then why check for /license/license.json ?"
The code may be running inside a docker container, in which case the license file is mounted to /license. If the file is not there, then I check our online license server. The license file should take precendence over the online server, so I only want to hit the online server if the license file is not mounted to /license.
OCDER
OCDER on 26 Jul 2018
Edited: OCDER on 26 Jul 2018
That should have been stated in the first question post. The way you phrased the question seems something is wrong with exist, "EXIST ignores leading slash". The question should have been something like this:
--------------------------------
How to search entire HDD or docker container for an exact file in a folder? exist('/folder/file.xxx', 'file') doesn't work.
I'm trying to search for the existence of a "/license/license.json" file in a docker container programatically. If it doesn't exist, then I search the server for one. So I started with this but it doesn't work as I expected:
%license.json is located at /Users/brian/apps/copilot/license/license.json'
exist('/license', 'dir')
= 0; %I expected this as root/license folder doesn't exist.
exist('/license/license.json', 'file')
= 2; %Unexpected. I expected a 0 result because "root/license/license.json doesn't exist
I really want:
exist('search everywhere for: /license/license.json', 'file')
= true; %Any ideas?
What's the best way to search for a folder AND file combo like /license/license.json? Why does exist return 2 for "file" search, but 0 for "dir" search in the case where /license/license.json DOES exist?
------------------------------
I'm not searching, I know the name of the file I want to check for. I just want to check if the exact file '/license/license.json' exists on my file system. The file does not exist, so I expect
exist('/license/license.json', 'file') == 0;
Instead, the above command seems to be searching in my path for 'license/license.json'. This is the behavior I would expect if I did not have the leading slash.
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]);
Thanks, OCDER. I'm surprised that this isn't how EXIST works, but the code you posted above does what I need.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 25 Jul 2018

Edited:

on 26 Jul 2018

Community Treasure Hunt

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

Start Hunting!