How to Upload Last Modified File **with the Name of the File** with FTP - VB - Still Missing Code-

StackOverflow https://stackoverflow.com/questions/20802055

Question

There has been an EDIT to this title 1:17:45 AM EST 12/29/13

Good Morning,

I'm currently working on a solution to have a program upload the most recently modified file in a specific directory to an online storage site, but I'm running into an issue specifying the most recently modified file in the directory.

I'm by no means a programmer, but I have recently been inspired to learn VB Script. All related articles I have reviewed include additional processes, but I can not seem to chop up the codes to use them successfully so I am hoping that someone could help me.

EDIT* There is no error message but I simply just do not know how to tell the program to upload the most recently modified file.

This is my first time posting on this site and the community here is great! Below is the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("FTP://ftp.DRIVEHQ.COM/information.zip"), System.Net.FtpWebRequest)
    request.Credentials = New System.Net.NetworkCredential("username", "password")
    request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

    Dim file() As Byte = System.IO.File.ReadAllBytes("C:\Users\Public\Documents\LAST MODIFIED FILE")

    Dim Strz As System.IO.Stream = request.GetRequestStream()
    Strz.Write(file, 0, file.Length)
    Strz.Close()
    Strz.Dispose()

Also the extension of the file I am looking for will be a .zip. Any contributions are greatly appreciated!

Was it helpful?

Solution

I recently learned File navigation on a project that sends modified files to Azure.

Try this:

Imports System.IO

Dim Folder As String = "C:\User\Public\Documents\"
Dim Files() As String

'Find all zip files in folder
Files = Directory.GetFiles(Folder, "*.zip", SearchOption.AllDirectories)

Dim fi As FileInfo

Dim FileToUpload As String = ""
Dim LastModifiedTime As DateTime = "1/1/1700"
For Each sFile In Files
    fi = New FileInfo(sFile)
    'Find the last mofified zip file
    If fi.LastWriteTimeUTC > LastModifiedTime Then
        LastModifiedTime = fi.LastWriteTimeUTC
        FileToUpload = sFile
    End If
Next

Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("FTP://ftp.DRIVEHQ.COM/" & fi.Name), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential("username", "password")
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

Dim file() As Byte = System.IO.File.ReadAllBytes(FileToUpload)

Dim Strz As System.IO.Stream = request.GetRequestStream()
Strz.Write(file, 0, file.Length)
Strz.Close()
Strz.Dispose()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top