Check if URL exists
Ältere Kommentare anzeigen
Does anyone know how to check if a URL exists? The function exist seems to only work of file paths through a computer.
Antworten (1)
Walter Roberson
am 30 Jul. 2012
1 Stimme
It is possible to detect that a URL does exist, but it is not always possible to detect that a URL does not exist compared to it existing but you not being permitted to examine it.
5 Kommentare
Ryan
am 30 Jul. 2012
Jan
am 31 Jul. 2012
@Ryan: Your original question is vague. Most of all the "existence" of an URL is not well defined. Walter's answer reveals this leak.
If you post the command, you use to access the file on the FTP-server, a suggestion for catching an error is much easier than writing the complete program from the scratch. So please add more details by editing the original question.
Walter Roberson
am 31 Jul. 2012
In the case of ftp, then fetching the URL without the final object should give you a directory of objects at that level, provided that the ftp server uses the common (but not guaranteed) strategy of interpreting a request for a directory to mean to return the directory listing.
Edward Strobach
am 25 Apr. 2017
@Walter, that first response was deep
Edward Strobach
am 26 Apr. 2017
Bearbeitet: Walter Roberson
am 26 Apr. 2017
I found for files online, that I can create a function that examines, iteratively, the presence of a file within a URL to determine whether or not it exists.
Example
function [info,status] = FileExists(t)
t = num2str(8965);
url = strcat('http://podaac-opendap.jpl.nasa.gov/opendap/allData/oscar/preview/L4/oscar_third_deg/oscar_vel',t,'.nc.gz');[~,status] = urlread(url);
filename = strcat('oscar_vel',t,'.nc.gz');
try
Info = ncinfo(url);
status = 1;
catch
Info = [];
status = 0;
end
If status is 1, then file exists, if status is 0, the file doesnt' exist. You can use a tilde for the first output within the square brackets to suppress the output if you only want to look at the status
Kategorien
Mehr zu Software Development finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!