• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 662
  • Last Modified:

automated ftp files upload and download with a message box

Hello,

I am looking for a vbscript that can prompt the user for a file to download and once entered download that file.  Also, the same thing except put a file on a server through ftp. I have this batch file but it does not pop a message that the file was transferred.

@echo off
setlocal

REM Prompt user for file name
set /P File=Please enter filename to transfer:
if "%File%" EQU "" (
  echo ERROR - No filename specified.
  exit /b
)

REM FTP file
(
  echo open 10.10.10.21
  echo user user
  echo lcd c:\temp
  echo cd /home/user
  echo get %File%
  echo quit
) | ftp -n -i

With this ftp file I can store the username and password and have it prompt for a file name or store the file name.  I am looking for the same thing in vbscript except when the file transfers have a message box say "Transfer complete" or if the file fails say "Transfer Failed".

Thank you.
0
cbones
Asked:
cbones
  • 2
1 Solution
 
RobSampsonCommented:
Hi, I pieced this together from some code I have.  Try it out for downloading and uploading.  I haven't tested it yet.

Regards,

Rob,

strFTPServer = "ftp.server.com"
strFTPUsername = "ftpusername"
strFTPPassword = "ftppassword"

' Path for log files
strTempFolder = "C:\Temp"

strFileToDownload = "FileOnFTPServer.zip"
strDownloadTo = "C:\Temp"

strValidPath = GetFileViaFTPServer(strZipPackage, strDownloadTo)
If Left(strValidPath, 5) = "ERROR" Then
	strMessage = strValidPath
	WScript.Echo strMessage
Else
	WScript.Echo "Downloaded to " & strValidPath
End If

strRemoteDirToUploadTo = "."
strFileToUpload = "C:\Temp\FileToUpload.zip"

blnReturn = UploadFiles(strRemoteDirToUploadTo, strFTPServer, strFTPUsername, strFTPPassword, Left(strFileToUpload, InStrRev(strFileToUpload, "\")), Mid(strFileToUpload, InStrRev(strFileToUpload, "\") + 1), Null)
If blnReturn = True Then
	strMessage = strFileToUpload & " upload successful."
	WScript.Echo strMessage
Else
	strMessage = strFileToUpload & " upload failed."
	WScript.Echo strMessage
End If

Function GetFileViaFTPServer(strRemotePath, strDestination)
	' TITLE: GetFileViaFTPServer
	' DESCRIPTION: This function will verify that FTP credentials exist, as defined
	'	by global variables strFTPServer, strFTPUsername, and strFTPPassword.
	'	It will pass these to the DownloadFiles sub procedure to download the required file.
	' DEPENDENCIES: This function requires that a DownloadFiles sub procedure also
	'		exists elsewhere in the script.
	' INPUT:
	'	strRemotePath specifies the source file
	'	strDestinationFolder specifies the destination folder to copy the file to.
	'		The name of the file should not be specified. It will retain the same name.
	' OUTPUT:
	'	A string value that will be either the destination file path, if the copy was successful,
	'	or "ERROR: " followed by the error number and description, or "Invalid FTP parameters" if
	'	the global variables do not exist.
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	If strFTPServer <> "" And strFTPUsername <> "" And strFTPPassword <> "" Then
		' Change the UNC path to a local path and download the file of the same name from the FTP source
		If Right(strDestinationFolder, 1) = "\" Then strDestinationFolder = Left(strDestinationFolder, Len(strDestinationFolder) - 1)
		If InStr(strRemotePath, "\") > 0 Then
			strRemoteFileName = Mid(strRemotePath, InStrRev(strRemotePath, "\") + 1)
		Else
			strRemoteFileName = strRemotePath
		End If
		' Call the DownloadFiles function
		blnReturn = DownloadFiles(".", strFTPServer, strFTPUsername, strFTPPassword, strDestination, strRemoteFileName, Null)
		If blnReturn = True Then
			strMessage = strRemoteFileName & " download successful."
			WScript.Echo strMessage
		Else
			strMessage = strRemoteFileName & " download failed."
			WScript.Echo strMessage
		End If
		strLocalCopy = strDestination & "\" & strRemoteFileName
		If objFSO.FileExists(strLocalCopy) = True Then
			strResult = strLocalCopy
		Else
			strResult = "ERROR: Unable to find file via FTP Transfer."
		End If
	Else
		strMessage = "Invalid FTP parameters were specified. Not all three FTP parameters were passed. Could not obtain file via FTP transfer."
		WScript.Echo strMessage									
		strResult = "ERROR: Unable to find file " & strRemoteFileName
	End If
	GetFileViaFTPServer = strResult
End Function

Function DownloadFiles(strRDir, strFTPHost, strUser, strPass, strLDir, strFileList, objVerboseLogObject)
	' TITLE: DownloadFiles
	' DESCRIPTION: This function will use the Microsoft FTP client to download the specified
	'	file from the specified FTP host, and optionally log the FTP output to a log file object,
	'	if one was passed.
	' INPUT:
	'	strRDir specifies the remote directory to download the file from.
	'	strFTPHost specifies the name of the FTP host
	'	strUser specifies the username for authentication to the FTP host
	'	strPass specifies the password for authentication to the FTP host
	'	strLDir specifies the local directory to download the file to.
	'	strFileList specifies the file (or files separated by a colon (;)) to download
	'	objVerboseLogObject specifies a file object bound to a text stream
	'		eg. Set objLog = objFSO.CreateTextFile("VerboseLog.txt", True) which is then passed
	'		This parameter can be passed as Null
	' RETURN VALUE:
	'	The return value will be True of False depending on whether ther download succeeded or failed
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If Right(strLDir, 1) = "\" Then strLDir = Left(strLDir, Len(strLDir) - 1)
	If Right(strRDir, 1) = "\" Then strRDir = Left(strRDir, Len(strRDir) - 1)
	strFTPCommands = strUser & vbCrLf & _
	                              strPass & vbCrLf & _
	                              "debug" & VbCrLf & _
	                              "binary" & vbCrLf & _
	                              "prompt" & VbCrLf & _
	                              "cd """ & strRDir & """" & vbCrLf & _
	                              "lcd """ & strLDir & """" & vbCrLf
	For Each strFile In Split(strFileList, ";")
		strFTPCommands = strFTPCommands & "mget """ & strFile & """" & vbCrLf
	Next
	strFTPCommands = strFTPCommands & "quit"
	
	strFTPScript = strTempFolder & "\FTPCommands.txt"
	Set objFTPFile = objFSO.CreateTextFile(strFTPScript, True)
	objFTPFile.Write strFTPCommands
	objFTPFile.Close
	      
	' You can add -d after the word "ftp" to enable debugging output
	strFTPOutput = strTempFolder & "\ConfigurationFileFTPDownload.txt"
	strCommand = "cmd /c ftp -s:""" & strFTPScript & """ " & strFTPHost & " > " & strFTPOutput
	
	objShell.Run strCommand, 1, True
	objFSO.DeleteFile strFTPScript, True

	blnSuccess = False
	If objFSO.FileExists(strFTPOutput) = True Then
		Set objFTPOutput = objFSO.OpenTextFile(strFTPOutput, 1, False)
		strOutput = objFTPOutput.ReadAll
		objFTPOutput.Close
		objFSO.DeleteFile strFTPOutput, True
		WScript.Echo vbCrLf & strOutput & vbCrLf
		If InStr(LCase(strOutput), "226 transfer complete") > 0 Or InStr(LCase(strOutput), "226 transfer ok") > 0 Then blnSuccess = True

		If IsNull(objVerboseLogObject) = False Then objVerboseLogObject.WriteLine strOutput
	End If

	DownloadFiles = blnSuccess
End Function

Function UploadFiles(strRDir, strFTPHost, strUser, strPass, strLDir, strFileList, objVerboseLogObject)
	' TITLE: UploadFiles
	' DESCRIPTION: This function will use the Microsoft FTP client to upload the specified
	'	file to the specified FTP host, and report success or failure of the upload.
	' INPUT:
	'	strRDir specifies the remote directory to upload the file to.
	'	strFTPHost specifies the name of the FTP host
	'	strUser specifies the username for authentication to the FTP host
	'	strPass specifies the password for authentication to the FTP host
	'	strLDir specifies the local directory to upload the file from
	'	strFileList specifies the file (or files separated by a colon (;)) to upload
	'	objVerboseLogObject specifies a file object bound to a text stream
	'		eg. Set objLog = objFSO.CreateTextFile("VerboseLog.txt", True) which is then passed
	'		This parameter can be passed as Null
	' RETURN VALUE:
	'	The return value will be True of False depending on whether ther download succeeded or failed
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If Right(strLDir, 1) = "\" Then strLDir = Left(strLDir, Len(strLDir) - 1)
	If Right(strRDir, 1) = "\" Then strRDir = Left(strRDir, Len(strRDir) - 1)
	strFTPCommands = strUser & vbCrLf & _
	                              strPass & vbCrLf & _
	                              "debug" & VbCrLf & _
	                              "binary" & vbCrLf & _
	                              "prompt" & vbCrLf & _
	                              "cd """ & strRDir & """" & vbCrLf & _
	                              "lcd """ & strLDir & """" & vbCrLf
	For Each strFile In Split(strFileList, ";")
		strFTPCommands = strFTPCommands & "mput """ & strFile & """" & vbCrLf
	Next
	strFTPCommands = strFTPCommands & "quit"
	
	strFTPScript = strTempFolder & "\FTPCommands.txt"
	Set objFTPFile = objFSO.CreateTextFile(strFTPScript, 1)
	objFTPFile.Write strFTPCommands
	objFTPFile.Close
	      
	strFTPOutput = strTempFolder & "\FTPUploadLog.txt"
	If objFSO.FileExists(strFTPOutput) = True Then objFSO.DeleteFile strFTPOutput, True
	strCommand = "cmd /c ftp -s:" & strFTPScript & " " & strFTPHost & " > " & strFTPOutput	
	
	objShell.Run strCommand, 1, True
	objFSO.DeleteFile strFTPScript, True
	
	blnSuccess = False
	If objFSO.FileExists(strFTPOutput) = True Then
		Set objFTPOutput = objFSO.OpenTextFile(strFTPOutput, 1, False)
		strOutput = objFTPOutput.ReadAll
		objFTPOutput.Close
		objFSO.DeleteFile strFTPOutput, True
		WScript.Echo vbCrLf & strOutput & vbCrLf
		If InStr(LCase(strOutput), "226 transfer complete") > 0 Or InStr(LCase(strOutput), "226 transfer ok") > 0 Then blnSuccess = True
		
		If IsNull(objVerboseLogObject) = False Then objVerboseLogObject.WriteLine strOutput
	End If

	UploadFiles = blnSuccess
End Function

Open in new window

0
 
cbonesAuthor Commented:
Is there any way to have them type in the file name that they need?  It will not also be the same file. And have these two files separated?

Thank you.
0
 
pnclickCommented:
You can use CommonDialog control to prompt for a filename.

such as:

Dim sFileName as String

CommonDialog1.InitDir = App.path
   
CommonDialog1.FileName = ""
CommonDialog1.CancelError = True  
CommonDialog1.Filter = "TXT Format (*.txt)|*.txt"
CommonDialog1.ShowOpen
   
sFileName  = CommonDialog1.FileName
0
 
RobSampsonCommented:
Hi, in it's simplest form, this will prompt with a text box for a file to download:
' Download a file via FTP

strFTPServer = "ftp.server.com"
strFTPUsername = "ftpusername"
strFTPPassword = "ftppassword"

' Path for log files
strTempFolder = "C:\Temp"

strFileToDownload = InputBox("Please enter the file name to download:", "File to Download", "FileOnFTPServer.zip")
strDownloadTo = InputBox("Please enter the folder to download the file to:", "Save As", "C:\Temp")

strValidPath = GetFileViaFTPServer(strZipPackage, strDownloadTo)
If Left(strValidPath, 5) = "ERROR" Then
	strMessage = strValidPath
	WScript.Echo strMessage
Else
	WScript.Echo "Downloaded to " & strValidPath
End If

Function GetFileViaFTPServer(strRemotePath, strDestination)
	' TITLE: GetFileViaFTPServer
	' DESCRIPTION: This function will verify that FTP credentials exist, as defined
	'	by global variables strFTPServer, strFTPUsername, and strFTPPassword.
	'	It will pass these to the DownloadFiles sub procedure to download the required file.
	' DEPENDENCIES: This function requires that a DownloadFiles sub procedure also
	'		exists elsewhere in the script.
	' INPUT:
	'	strRemotePath specifies the source file
	'	strDestinationFolder specifies the destination folder to copy the file to.
	'		The name of the file should not be specified. It will retain the same name.
	' OUTPUT:
	'	A string value that will be either the destination file path, if the copy was successful,
	'	or "ERROR: " followed by the error number and description, or "Invalid FTP parameters" if
	'	the global variables do not exist.
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	If strFTPServer <> "" And strFTPUsername <> "" And strFTPPassword <> "" Then
		' Change the UNC path to a local path and download the file of the same name from the FTP source
		If Right(strDestinationFolder, 1) = "\" Then strDestinationFolder = Left(strDestinationFolder, Len(strDestinationFolder) - 1)
		If InStr(strRemotePath, "\") > 0 Then
			strRemoteFileName = Mid(strRemotePath, InStrRev(strRemotePath, "\") + 1)
		Else
			strRemoteFileName = strRemotePath
		End If
		' Call the DownloadFiles function
		blnReturn = DownloadFiles(".", strFTPServer, strFTPUsername, strFTPPassword, strDestination, strRemoteFileName, Null)
		If blnReturn = True Then
			strMessage = strRemoteFileName & " download successful."
			WScript.Echo strMessage
		Else
			strMessage = strRemoteFileName & " download failed."
			WScript.Echo strMessage
		End If
		strLocalCopy = strDestination & "\" & strRemoteFileName
		If objFSO.FileExists(strLocalCopy) = True Then
			strResult = strLocalCopy
		Else
			strResult = "ERROR: Unable to find file via FTP Transfer."
		End If
	Else
		strMessage = "Invalid FTP parameters were specified. Not all three FTP parameters were passed. Could not obtain file via FTP transfer."
		WScript.Echo strMessage									
		strResult = "ERROR: Unable to find file " & strRemoteFileName
	End If
	GetFileViaFTPServer = strResult
End Function

Function DownloadFiles(strRDir, strFTPHost, strUser, strPass, strLDir, strFileList, objVerboseLogObject)
	' TITLE: DownloadFiles
	' DESCRIPTION: This function will use the Microsoft FTP client to download the specified
	'	file from the specified FTP host, and optionally log the FTP output to a log file object,
	'	if one was passed.
	' INPUT:
	'	strRDir specifies the remote directory to download the file from.
	'	strFTPHost specifies the name of the FTP host
	'	strUser specifies the username for authentication to the FTP host
	'	strPass specifies the password for authentication to the FTP host
	'	strLDir specifies the local directory to download the file to.
	'	strFileList specifies the file (or files separated by a colon (;)) to download
	'	objVerboseLogObject specifies a file object bound to a text stream
	'		eg. Set objLog = objFSO.CreateTextFile("VerboseLog.txt", True) which is then passed
	'		This parameter can be passed as Null
	' RETURN VALUE:
	'	The return value will be True of False depending on whether ther download succeeded or failed
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If Right(strLDir, 1) = "\" Then strLDir = Left(strLDir, Len(strLDir) - 1)
	If Right(strRDir, 1) = "\" Then strRDir = Left(strRDir, Len(strRDir) - 1)
	strFTPCommands = strUser & vbCrLf & _
	                              strPass & vbCrLf & _
	                              "debug" & VbCrLf & _
	                              "binary" & vbCrLf & _
	                              "prompt" & VbCrLf & _
	                              "cd """ & strRDir & """" & vbCrLf & _
	                              "lcd """ & strLDir & """" & vbCrLf
	For Each strFile In Split(strFileList, ";")
		strFTPCommands = strFTPCommands & "mget """ & strFile & """" & vbCrLf
	Next
	strFTPCommands = strFTPCommands & "quit"
	
	strFTPScript = strTempFolder & "\FTPCommands.txt"
	Set objFTPFile = objFSO.CreateTextFile(strFTPScript, True)
	objFTPFile.Write strFTPCommands
	objFTPFile.Close
	      
	' You can add -d after the word "ftp" to enable debugging output
	strFTPOutput = strTempFolder & "\ConfigurationFileFTPDownload.txt"
	strCommand = "cmd /c ftp -s:""" & strFTPScript & """ " & strFTPHost & " > " & strFTPOutput
	
	objShell.Run strCommand, 1, True
	objFSO.DeleteFile strFTPScript, True

	blnSuccess = False
	If objFSO.FileExists(strFTPOutput) = True Then
		Set objFTPOutput = objFSO.OpenTextFile(strFTPOutput, 1, False)
		strOutput = objFTPOutput.ReadAll
		objFTPOutput.Close
		objFSO.DeleteFile strFTPOutput, True
		WScript.Echo vbCrLf & strOutput & vbCrLf
		If InStr(LCase(strOutput), "226 transfer complete") > 0 Or InStr(LCase(strOutput), "226 transfer ok") > 0 Then blnSuccess = True

		If IsNull(objVerboseLogObject) = False Then objVerboseLogObject.WriteLine strOutput
	End If

	DownloadFiles = blnSuccess
End Function

Open in new window


and this will prompt with a text box for a file to upload:
' Upload a file via FTP

strFTPServer = "ftp.server.com"
strFTPUsername = "ftpusername"
strFTPPassword = "ftppassword"

' Path for log files
strTempFolder = "C:\Temp"

strFileToUpload = InputBox("Please enter the file to upload:", "File to Upload", "C:\Temp\FileToUpload.zip")
strRemoteDirToUploadTo = InputBox("Please enter the remote folder to upload the file to:", "Destination", ".")

blnReturn = UploadFiles(strRemoteDirToUploadTo, strFTPServer, strFTPUsername, strFTPPassword, Left(strFileToUpload, InStrRev(strFileToUpload, "\")), Mid(strFileToUpload, InStrRev(strFileToUpload, "\") + 1), Null)
If blnReturn = True Then
	strMessage = strFileToUpload & " upload successful."
	WScript.Echo strMessage
Else
	strMessage = strFileToUpload & " upload failed."
	WScript.Echo strMessage
End If

Function UploadFiles(strRDir, strFTPHost, strUser, strPass, strLDir, strFileList, objVerboseLogObject)
	' TITLE: UploadFiles
	' DESCRIPTION: This function will use the Microsoft FTP client to upload the specified
	'	file to the specified FTP host, and report success or failure of the upload.
	' INPUT:
	'	strRDir specifies the remote directory to upload the file to.
	'	strFTPHost specifies the name of the FTP host
	'	strUser specifies the username for authentication to the FTP host
	'	strPass specifies the password for authentication to the FTP host
	'	strLDir specifies the local directory to upload the file from
	'	strFileList specifies the file (or files separated by a colon (;)) to upload
	'	objVerboseLogObject specifies a file object bound to a text stream
	'		eg. Set objLog = objFSO.CreateTextFile("VerboseLog.txt", True) which is then passed
	'		This parameter can be passed as Null
	' RETURN VALUE:
	'	The return value will be True of False depending on whether ther download succeeded or failed
	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If Right(strLDir, 1) = "\" Then strLDir = Left(strLDir, Len(strLDir) - 1)
	If Right(strRDir, 1) = "\" Then strRDir = Left(strRDir, Len(strRDir) - 1)
	strFTPCommands = strUser & vbCrLf & _
	                              strPass & vbCrLf & _
	                              "debug" & VbCrLf & _
	                              "binary" & vbCrLf & _
	                              "prompt" & vbCrLf & _
	                              "cd """ & strRDir & """" & vbCrLf & _
	                              "lcd """ & strLDir & """" & vbCrLf
	For Each strFile In Split(strFileList, ";")
		strFTPCommands = strFTPCommands & "mput """ & strFile & """" & vbCrLf
	Next
	strFTPCommands = strFTPCommands & "quit"
	
	strFTPScript = strTempFolder & "\FTPCommands.txt"
	Set objFTPFile = objFSO.CreateTextFile(strFTPScript, 1)
	objFTPFile.Write strFTPCommands
	objFTPFile.Close
	      
	strFTPOutput = strTempFolder & "\FTPUploadLog.txt"
	If objFSO.FileExists(strFTPOutput) = True Then objFSO.DeleteFile strFTPOutput, True
	strCommand = "cmd /c ftp -s:" & strFTPScript & " " & strFTPHost & " > " & strFTPOutput	
	
	objShell.Run strCommand, 1, True
	objFSO.DeleteFile strFTPScript, True
	
	blnSuccess = False
	If objFSO.FileExists(strFTPOutput) = True Then
		Set objFTPOutput = objFSO.OpenTextFile(strFTPOutput, 1, False)
		strOutput = objFTPOutput.ReadAll
		objFTPOutput.Close
		objFSO.DeleteFile strFTPOutput, True
		WScript.Echo vbCrLf & strOutput & vbCrLf
		If InStr(LCase(strOutput), "226 transfer complete") > 0 Or InStr(LCase(strOutput), "226 transfer ok") > 0 Then blnSuccess = True
		
		If IsNull(objVerboseLogObject) = False Then objVerboseLogObject.WriteLine strOutput
	End If

	UploadFiles = blnSuccess
End Function

Open in new window


Regards,

Rob.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now