Link to home
Start Free TrialLog in
Avatar of simonlai
simonlai

asked on

VBS ftp command download latest uploaded file

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

ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simonlai
simonlai

ASKER

robberbaron, many thanks for our ideas....

i manage to do it...


once again...thanks
*our => ur