Click here to Skip to main content
15,894,740 members
Articles / Desktop Programming / Windows Forms
Article

Copy from FTP server to local Server through VB.Net

Rate me:
Please Sign up or sign in to vote.
2.56/5 (10 votes)
10 May 2007 85.3K   26   7
This code helps you to explain how to copy/downloads files from FTP server to local server

Introduction

Just go through the code and you will come to know that how to copy or downloads the files from FTP server to your local server/machines by using VB.Net Code

Background

Manuallyu copying the code from FTP server to local server is always a fun but the same is cumbersum job if gone through the code.........

Using the code

This is my one of the most beautiful work to copy multiple files from any FTP server to your desire folder or server........ Just go through the code and experience the beauty of it...

//
// Any source code blocks look like this
//
Dim oScript, oFileSys, objFSO, fileSearchObj, fileNameList, fileName, fileNameArray
Dim tempFileName As String
Try

    '' set the FTP path and username/password fields

Dim ftp_address As String = "ftp.XYZ.com" '---> ftp site address
Dim ftp_username As String = "username" '---> user name 
Dim ftp_password As String = "passwd" '---> password

fileSearchObj = CreateObject("Scripting.FileSystemObject")
' Get all the files down loded to C:\ftpDownload folder
fileNameArray = fileSearchObj.GetFolder("C:\ftpDownload\")
fileNameList = fileNameArray.files

For Each fileName In fileNameList
    tempFileName = fileName.name

    '1. Crate the temperory .ftp file (ftpCommand.ftp) which contain the FTP code 
    '2. Write the code for copying the file from FTP to Local folder
    '3. Write the code to delete the file from FTP after copy
    '4. Execute the file through command prompt

    Dim objTextFile
    Dim strFTPFileName As String = ""
    '-----------------------------------------------------------
    ' Step 1
    '-----------------------------------------------------------
    ' this file will be saved on the root folder (bydefault)
    strFTPFileName = "C:\ftpCommand.ftp"
    ' create the temporary on root folder 
    objTextFile = objFSO.CreateTextFile(strFTPFileName, True)
    
    '-----------------------------------------------------------
    ' Step 2
    '-----------------------------------------------------------
    ' write the ftp command line code to fetch files in the file test.ftp
    objTextFile.WriteLine("open " & ftp_address) '-------> open FTP site/location
    objTextFile.WriteLine(ftp_username) '--------------> ftp user name
    objTextFile.WriteLine(ftp_password) '--------------> ftp user password
    objTextFile.WriteLine("cd tempFolder") '-----------> cd tempFolder 
    ' tempFolder is the source folder on FTP site from where application will down load files
    ' Example- ftp:\\ftp.XYZ.com\tempFolder 
    objTextFile.WriteLine("lcd C:\ftpDownload") '--------> This is the working directory
    
    '-----------------------------------------------------------
    ' Step 3
    '-----------------------------------------------------------
    objTextFile.WriteLine("delete " & tempFileName) '----> delete 'filename' // to delete the same copied files from the FTP server
    objTextFile.WriteLine("y") '--------------> y //propmt message 'yes' to commit the copy operation
    objTextFile.WriteLine("bye") '--------------> bye //ftp command to come out of the ftp command promt
    
    ' close the stream for creating the new ftp file
    objTextFile.Close()
    objTextFile = Nothing
    
    '-----------------------------------------------------------
    ' Step 4
    '----------------------------------------------------------- ' create the FTP command promt syntax to execute the files
    Dim strCMD As String
    strCMD = "ftp.exe -i -s: C:\ftpCommand.ftp"
    ' create the temperory file to store the output generated after executing the FTP command 
    Dim strTempFile As String
    strTempFile = "C:\ftpOutput.txt"
    ' call the command promt control to execute the delete operation
    Call oScript.Run("cmd.exe /c " & strCMD & " > " & strTempFile, 0, True)
    
Next

Catch ex As Exception
    ' Some thing goes wrong
End Try 



<pre>You can use the desire desination folder instead of harc-coded C:\ drive

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Optimistic....... and Looking forward

Comments and Discussions

 
QuestionVB.net really? Total waste of time Pin
Sabarinathan Ramachandran21-Jul-15 20:57
Sabarinathan Ramachandran21-Jul-15 20:57 
BugSomething is wrong. Pin
Mshadow1717-Jun-14 16:03
Mshadow1717-Jun-14 16:03 
QuestionGetting error on declaration Pin
Ronny George30-Jan-12 2:13
Ronny George30-Jan-12 2:13 
GeneralHi Pin
prabupep29-Jul-10 0:58
prabupep29-Jul-10 0:58 
GeneralMy vote of 1 Pin
JBDJr28-May-09 10:19
JBDJr28-May-09 10:19 
GeneralWhere is the copy code Pin
navalArya22-Oct-07 13:16
navalArya22-Oct-07 13:16 
also.. where is the ftp command to copy file. u shud have a get command somewhere and i am not able to find it.

And i agree with person above.. why are u calling it a vb.net app?OMG | :OMG:
QuestionVBS!? Pin
T4Top10-May-07 2:21
T4Top10-May-07 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.