check if url exists for large files without downloading the file

36 views (last 30 days)
I've searched this forum for a while and need some help with checking URLs. I have a set of files to download from a site and loop through the formatting options for the files basically the files are zip files and are extremely large (nearly 100MB). If I use the following statement:
raw_file = websave([p_raw,'\',fname_raw],[URL,MO_name,num2str(YR),'/raw/',fname_raw]);
Then for files that do not actually exist a dummy file with 0KB of data in it is made in the saved location. I want to avoid this. I've tried using:
[str status] = urlread([URL,MO_name,num2str(YR),'/raw/',fname_raw]);
but MATLAB still insists on trying to read the actual file instead of just telling me if it's there or not - wasting time in the process.
The function isurl does not apply to named URLs. How do I check merely the existence of a url without actually downloading it?

Answers (4)

Steven Lord
Steven Lord on 11 Oct 2016
Try using Java.
url = '';
J =;
conn = openConnection(J);
status = getResponseCode(conn)
When I ran this code I received status 200 which is OK. I tried it for a URL to a file that didn't exist and received code 404. I don't think creating an object using openConnection actually retrieves the data; I think you'd have to call that object's getContent method or something similar to obtain the data.

Image Analyst
Image Analyst on 11 Oct 2016
Did you try exist()?
itExists = exist(filename, 'file');
filename will the the url of the file you want to check on.
  1 Comment
Diana on 11 Oct 2016
Edited: Walter Roberson on 11 Oct 2016
If you mean in this manner:
itexists = exist('','file')
nope - gives me a 0 return and I'm staring at the file on the URL.

Sign in to comment.

Matthew Eicholtz
Matthew Eicholtz on 11 Oct 2016
It seems I stumbled upon a similar approach to Steven, but a few minutes too late!
I think this function would do the trick:
function tf = urlexist(url)
URL =; %create the URL object
% Get the proxy information using the MATLAB proxy API.
proxy = com.mathworks.webproxy.WebproxyFactory.findProxyForURL(URL);
% Open a connection to the URL.
if isempty(proxy)
urlConnection = URL.openConnection;
urlConnection = URL.openConnection(proxy);
% Try to start the input stream
inputStream = urlConnection.getInputStream;
tf = true;
tf = false;

Fernando Bello
Fernando Bello on 28 Nov 2019
Maybe it is a little bit late, but I'm just reading this post because I had the same trouble.
I solved it this way:
url = ('https://matlab/'); % the URL where data is, it can be what ever you want, png, doc, mat, etc.
filename = 'filename_test'; % the name you want to save the file
options = weboptions('Username','username','Password','passwd','CertificateFilename','');
disp('OK') %if the file exist it is downloades
disp('URL read of link was unsuccessful') % if file do not exist

Community Treasure Hunt

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

Start Hunting!

Translated by