I am making a simple application that have to fetch all images in subdirectories from a site and recreate the file and folder structure locally. Here is what i have so far:
string folder = "c:\someLocalFolder\";
// To keep track of how many files that are processed
int i = 0;
// Folder x
for (int x = 2; x <= 7; x++)
{
// Folder y
for (int y = 0; y < 80; y++)
{
//File z
for (int z = 0; z <= 70; z++)
{
// File, increment
i++;
string destFolderPath = Path.Combine(folder, x.ToString(), y.ToString());
string filePath = Path.Combine(destFolderPath, z.ToString() + ".png");
if (!File.Exists(filePath))
{
var url = string.Format("http://www.somesite.com/images/{0}/{1}/{2}.png", x, y, z);
if (!Directory.Exists(destFolderPath))
// Folder doesnt exist, create
Directory.CreateDirectory(destFolderPath);
var webClient = new WebClient();
webClient.DownloadFileCompleted += (o, e) =>
{
// less than 1 byte recieved, delete
if( (new FileInfo(filePath).Length) < 1 )
{
File.Delete(filePath);
}
// File processed
i--;
Console.WriteLine(i);
};
webClient.DownloadFileAsync(new Uri(url), filePath);
}
else
{
// File processed
i--;
Console.WriteLine(i);
}
}
}
}
So as you can se currently i am iterating and creating the folder structure, then i am download file asynchronously and then check if the file is lesser than 1 byte in file size and deleting it if it is.
I think that i am doing this in an very cumbersome way, it is not very fast, and it makes a whole lot of files only do delete the once that does not meet the requirements.
Is there any faster way of determining if the file exists on the web server, and based on that download if exist, and the way i create the folder structure, is this an appropriate way how i do it to accomplish what i want?