Created
December 22, 2013 22:04
-
-
Save lucamauri/8089018 to your computer and use it in GitHub Desktop.
Check and download images from website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Net | |
Imports System.IO | |
Module modImages | |
Function getimages() As Boolean | |
Dim prefixFull As String = "http://www.yoursite.com/images/" | |
Dim prefixShort As String = "http://www.yoursite.com" | |
Dim basePath As String = "c:\your path here" | |
Dim imgClient As WebClient | |
Dim imgRequest As WebRequest | |
Dim imgResp As WebResponse | |
Dim lastMod As Date | |
Dim imgInfo As FileInfo | |
Dim imgPath As String | |
Dim destPath As String | |
Dim filePath As String | |
Dim folder As String | |
Try | |
For Each immagine As DataRow In dsImgs.Tables(0).rows | |
Select Case immagine("file").ToString.Substring(0, 1) | |
Case "/" | |
imgPath = prefixShort & immagine("file") | |
filePath = immagine("file").ToString.Substring(1).Replace("/", "\") | |
Case Else | |
imgPath = prefixFull & immagine("file") | |
filePath = immagine("file").ToString.Replace("/", "\") | |
End Select | |
destPath = Path.Combine(basePath, filePath) | |
imgClient = New System.Net.WebClient | |
If File.Exists(destPath) Then | |
Try | |
imgRequest = WebRequest.Create(imgPath.TrimStart) | |
imgResp = imgRequest.GetResponse | |
lastMod = DateTime.Parse(imgResp.Headers("Last-Modified")) | |
imgInfo = New FileInfo(destPath) | |
If DateTime.Compare(imgInfo.CreationTime, lastMod) > 0 Then | |
Console.WriteLine("SKIP - " & destPath) | |
Continue For | |
End If | |
Catch ex As Exception | |
Console.WriteLine("ERROR - " & imgPath) | |
Finally | |
If imgResp IsNot Nothing Then | |
imgResp.Close() | |
End If | |
End Try | |
End If | |
folder = destPath.Substring(0, destPath.LastIndexOf("\")) | |
If Not Directory.Exists(folder) Then | |
Directory.CreateDirectory(folder) | |
End If | |
Try | |
imgClient.DownloadFile(imgPath.TrimStart, destPath) | |
Console.WriteLine(destPath) | |
Catch ex As Exception | |
Console.WriteLine("ERROR 2 - " & imgPath) | |
End Try | |
imgClient.Dispose() | |
Next | |
Console.WriteLine("Completed") | |
Return True | |
Catch ex As Exception | |
MessageBox.Show(ex.ToString) | |
Return False | |
End Try | |
End Function | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment