Copy from FTP server to local Server through VB.Net






2.56/5 (10 votes)
May 10, 2007

86202
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 TryYou can use the desire desination folder instead of harc-coded C:\ drive