Does anyone know how to check if a URL exists? The function exist seems to only work of file paths through a computer.

답변 (1개)

Walter Roberson
Walter Roberson 2012년 7월 30일

1 개 추천

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

Ryan
Ryan 2012년 7월 30일
Walter:
Not sure what you're trying to say. What might be useful is that the URL is an FTP file that I'm trying to access. My problem is that if it doesn't find the file my code freaks out so I'm trying to implement an IF statement and if the URL doesn't exist, use "continue" to jump over it
Jan
Jan 2012년 7월 31일
@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
Walter Roberson 2012년 7월 31일
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
Edward Strobach 2017년 4월 25일
@Walter, that first response was deep
Edward Strobach
Edward Strobach 2017년 4월 26일
편집: Walter Roberson 2017년 4월 26일
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

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

카테고리

도움말 센터File Exchange에서 Software Development에 대해 자세히 알아보기

질문:

2012년 7월 30일

편집:

2017년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by