How to find files with "*.zip" from a url path with Directory Browsing enabled using c#. For Eg: if my url is : http://www.example.com/myfolder/myfiles and the path consist of the files : ex1.htm,ex2.zip,ex.pdf,ex.swf, Then how can i find the filename with the extension *.zip. Please help me how to find the file name of the zip file in the URL
-
This SO post may help you, it explains how you can parse the directory listing of a url, http://stackoverflow.com/questions/124492/c-sharp-httpwebrequest-command-to-get-directory-listing . Parsing is the first step, then once you parse it you can filter .zip files – Despertar Jul 06 '12 at 06:19
-
Tinoy, by 'finding files', do u mean to download it? If download is permissible, then I have suggestion, I think. – RAJ Jul 06 '12 at 06:34
-
No I only need the filename of the zip file present in that folder.I think we can use the regular expression to find the zip file in the HTML document of the url.. – Tinoy Jameson Jul 06 '12 at 06:41
-
Yeah exactly, I am also thinking the same thing – RAJ Jul 06 '12 at 06:47
4 Answers
Unless an FTP server is also available, I think you will have to resort to loading the directory listing using HttpWebRequest
and then parsing the results to pull out all of the hrefs in the <A> tags.
Here is some sample code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string html = reader.ReadToEnd();
Console.WriteLine("Parsing {0}", html);
Regex regex = new Regex("href=\\\"([^\\\"]*)", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(html);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
if (match.Success)
{
Console.WriteLine("Found {0}", match.Captures[0]);
}
}
}
}
}
Note that the href values you get will be relative to the current directory.

- 8,891
- 3
- 29
- 42
string[] s = Directory.GetFiles(path);
int i = 0;
while (i < s.Length)
{
if (s[i].Substring((s[i].IndexOf(".") + 1), 3).Equals("zip"))
{
Response.Write(s[i].ToString());
i = i + 1;
}
}
Try this.
EDIT
int slashIndex = url.lastIndexOf('/');
int dotIndex = url.lastIndexOf('.zip', slashIndex);
String filenameWithoutExtension;
if (dotIndex == -1)
{
filenameWithoutExtension = url.substring(slashIndex + 1);
}
else
{
filenameWithoutExtension = url.substring(slashIndex + 1, dotIndex);
}
This works for the URL where the filenames are attached at the end/ I will leave the error handling on you/

- 1,202
- 3
- 21
- 43
-
We cannot use Directory.GetFiles because here using a url path not a directory. I am getting the following exception when use Directory.GetFiles: URI formats are not supported. – Tinoy Jameson Jul 06 '12 at 05:53
-
I dont think you can access the files within a web url. Take a look to my edited answer – akhil Jul 06 '12 at 06:03
-
Hi Akhil.. My url path doesnot consist of zip file . Eg: if my url is http://example.com/folder/ then there are lot of files in this folderpath...i don't know the filenames of this folder..Can i get the filename of files in this folder in a collection list?? – Tinoy Jameson Jul 06 '12 at 06:08
-
-
But Akhil, we can browse the folder of a website by enabling directory browse in the IIS.. – Tinoy Jameson Jul 06 '12 at 06:12
-
-
Thank you Akhil..I am also trying for this..Hope to get a good solution after sometime... – Tinoy Jameson Jul 06 '12 at 06:18
As suggested by Michael I got the solution of the question as follows:
string urlpath = "http://www.example.com/folder/"
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlpath);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string html = reader.ReadToEnd();
Regex regEx = new Regex(@".*/(?<filename>.*?)\.zip");
MatchCollection matches = regEx.Matches(html);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
if (match.Success)
{
Console.WriteLine(match.Groups["filename"].Value);
}
}
}
}

- 403
- 2
- 7
- 17
You can use GetFileName
to get file name.
For example:
System.IO.Path.GetFileName(path);
OR
You can try following to get filename from a directory:
var filenames = String.Join(", ", Directory.GetFiles(@"c:\", "*.zip").Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray());

- 9,697
- 1
- 33
- 63
-
Actually My path is a folder path(http://www.example.com/folderpath/) and i need the filename of the zip files present in this directory path..How can i get the filename of the zip file?? – Tinoy Jameson Jul 06 '12 at 05:40
-
1So you can try: var filenames = String.Join(", ", Directory.GetFiles(@"c:\", "*.zip").Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray()); – RAJ Jul 06 '12 at 05:46
-
Directory.GetFiles() function only returns the file s in a directory. But here i am using the files in a url path not directory.. When we use url path (http://www.example.com/filderpath/) in Directory.GetFiles..it returns exception invalid URI format.. – Tinoy Jameson Jul 06 '12 at 05:49
-
2You can just make a System.Uri object, and use IsFile to verify it's a file, then Uri.LocalPath to extract the filename. – RAJ Jul 06 '12 at 05:55
-
There are lot of files in the Url location. I have to extract all the files in the Url location to a local directory to find the zip file. Can you suggest any other method without extracting all the files fron the url location to the local path?? – Tinoy Jameson Jul 06 '12 at 05:58
-