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 commentaires

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.
Walter Roberson
Walter Roberson le 25 Juil 2018
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?
Brian Keating
Brian Keating le 25 Juil 2018
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
Brian Keating
Brian Keating le 25 Juil 2018
I get this behavior in 2018a and 2016b on OSX
OCDER
OCDER le 25 Juil 2018
Did this work?
exist('/Users/brian/apps/copilot/license', 'dir')
Brian Keating
Brian Keating le 25 Juil 2018
yes, it returns 7 as expected
OCDER
OCDER le 25 Juil 2018
Modifié(e) : OCDER le 25 Juil 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
Walter Roberson
Walter Roberson le 26 Juil 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.)

Connectez-vous pour commenter.

 Réponse acceptée

OCDER
OCDER le 25 Juil 2018
Modifié(e) : OCDER le 26 Juil 2018

0 votes

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 commentaires

Walter Roberson
Walter Roberson le 25 Juil 2018
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.
Brian Keating
Brian Keating le 25 Juil 2018
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.
OCDER
OCDER le 25 Juil 2018
It's something with Windows.
cd('/Users')
will get me to C:\Users no matter where my current directory is.
Brian Keating
Brian Keating le 26 Juil 2018
Modifié(e) : Brian Keating le 26 Juil 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 le 26 Juil 2018
Modifié(e) : OCDER le 26 Juil 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
Walter Roberson
Walter Roberson le 26 Juil 2018
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
Brian Keating
Brian Keating le 26 Juil 2018
"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 le 26 Juil 2018
Modifié(e) : OCDER le 26 Juil 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.
OCDER
OCDER le 26 Juil 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]);
Brian Keating
Brian Keating le 26 Juil 2018
Thanks, OCDER. I'm surprised that this isn't how EXIST works, but the code you posted above does what I need.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by