Click here to Skip to main content
Click here to Skip to main content

Copy from FTP server to local Server through VB.Net

, 10 May 2007
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Nakul Lande
Web Developer
United States United States
Optimistic....... and Looking forward

Comments and Discussions

 
BugSomething is wrong. PinmemberMshadow1717-Jun-14 17:03 
QuestionGetting error on declaration PinmemberRonny George30-Jan-12 3:13 
GeneralHi Pinmemberprabupep29-Jul-10 1:58 
GeneralMy vote of 1 PinmemberJBDJr28-May-09 11:19 
GeneralWhere is the copy code PinmembernavalArya22-Oct-07 14:16 
QuestionVBS!? PinmemberT4Top10-May-07 3:21 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 10 May 2007
Article Copyright 2007 by Nakul Lande
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid