Solved

VBS ftp command download latest uploaded file

Posted on 2010-09-17
3
1,059 Views
Last Modified: 2012-05-10
Dear All:

Can any one provide some ideas how can i develop a vbs scripts with download the new file from ftp server?

i have developed some scripts but i only can download all file instead of download only latest file in ftp server.

Many thanks
Function FTPDownload(sSite, sUsername, sPassword, sLocalPath, sRemotePath, sRemoteFile)

	Const OpenAsDefault = -2
	Const FailIfNotExist = 0
	Const ForReading = 1
	Const ForWriting = 2
 
	Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
	Set oFTPScriptShell = CreateObject("WScript.Shell")

	sRemotePath = Trim(sRemotePath)
	sLocalPath = Trim(sLocalPath)
 
	'----------Path Checks---------
	'Here we will check the remote path, if it contains
	'spaces then we need to add quotes to ensure
	'it parses correctly.
	If InStr(sRemotePath, " ") > 0 Then
		If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
			sRemotePath = """" & sRemotePath & """"
		End If
	End If
 
	'Check to ensure that a remote path was
	'passed. If it's blank then pass a "\"
	If Len(sRemotePath) = 0 Then
		'Please note that no premptive checking of the
		'remote path is done. If it does not exist for some
		'reason. Unexpected results may occur.
		sRemotePath = "\"
	End If
 
	'If the local path was blank. Pass the current
	'working direcory.
	If Len(sLocalPath) = 0 Then
		sLocalpath = oFTPScriptShell.CurrentDirectory
	End If
 
	If Not oFTPScriptFSO.FolderExists(sLocalPath) Then
		'destination not found
		FTPDownload = "Error: Local Folder Not Found."
		Exit Function
	End If
 
	sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
	oFTPScriptShell.CurrentDirectory = sLocalPath
	'--------END Path Checks---------
 
	'build input file for ftp command
	sFTPScript = sFTPScript & "USER " & sUsername & vbCRLF
	sFTPScript = sFTPScript & sPassword & vbCRLF
	sFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLF
	sFTPScript = sFTPScript & "ascii" & vbCRLF
	sFTPScript = sFTPScript & "prompt n" & vbCRLF
	sFTPScript = sFTPScript & "mget " & sRemoteFile & vbCRLF
	sFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF


	sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
	sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
	sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName

	'Write the input file for the ftp command
	'to a temporary file.
	Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
	fFTPScript.WriteLine(sFTPScript)
	fFTPScript.Close
	Set fFTPScript = Nothing 

	oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & sSite & " > " & sFTPResults, 0, TRUE
 	
	Wscript.Sleep 1000
 
	'Check results of transfer.
	Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, FailIfNotExist, OpenAsDefault)
	sResults = fFTPResults.ReadAll
	fFTPResults.Close
 
	'oFTPScriptFSO.DeleteFile(sFTPTempFile)
	'oFTPScriptFSO.DeleteFile (sFTPResults)
 	
	'Wscript.Echo sResults

	If InStr(sResults, "226 Transfer complete.") > 0 Then
		FTPDownload = True
	ElseIf InStr(sResults, "File not found") > 0 Then
		FTPDownload = "Error: File Not Found"
	ElseIf InStr(sResults, "cannot log in.") > 0 Then
		FTPDownload = "Error: Login Failed."
	Else
		FTPDownload = "Error: Unknown."
	End If
 
	Set oFTPScriptFSO = Nothing
	Set oFTPScriptShell = Nothing
End Function

Open in new window

0
Comment
Question by:simonlai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 33709387
the builtin FTP client is hard to script. It has no interactive options; there are some freeware one that do but cant find the one I used a few years ago.

Using the builtin FTP client:

i think you will have to capture a ftp DIR command to a file, parse that to determine which files to retrieve based upon displayed date/time, and then do the retrival.
this will require 2 connections but that is probably not a big problem.  You have done the second conenction

Conn1:
user ; pwd ; dir "source folder" "c:\localtempfile.txt" ; quit

Conn2:
user; pwd ; cd "source folder" ; mget file1 file2 file3 file4

(or use multiple mget to make it easier to script many files)
quit;


http://www.nsftools.com/tips/MSFTP.htm
dir  
Displays a list of a remote directory's files and subdirectories. See also mdir, which can list multiple directories.
 Syntax: dir [remote-directory] [local-file]   Parameter(s):
 remote-directory -  Specifies the directory for which you want to see a listing. If no  directory is specified, the current working directory on the remote  computer is used.
 local-file -  Specifies a local file to store the listing. If not specified, output is displayed on the screen.  
0
 
LVL 1

Author Comment

by:simonlai
ID: 33713568
robberbaron, many thanks for our ideas....

i manage to do it...


once again...thanks
0
 
LVL 1

Author Comment

by:simonlai
ID: 33713569
*our => ur
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Clear Filter 8 73
Use GPO script to report finding a certain file 16 17
CREATE DATABASE 3 36
Modify file details 1 23
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question