Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ftp_upld.vbs

(view source code of ftp_upld.vbs as plain text)

  1. Option Explicit
  2.  
  3. ' Upload the file "test.txt" from the current (local) directory to
  4. ' "ftp.myServer.myTld/www/testdir/test.txt"; create the directory
  5. ' "testdir" if it doesn't exist; login as "myid" with password "secret".
  6. WScript.Echo FTPUpload( "test.txt", "/www/testdir", _
  7.                         "ftp.myServer.myTld", _
  8.                         "myid", "secret", True )
  9.  
  10.  
  11. Function FTPUpload( locFile, targetDir, host, user, password, blnMkDir )
  12. ' This function uses the free ChilkatFTP ActiveX component
  13. ' to upload a single file.
  14. ' The remote directory can be specified, but the remote
  15. ' file name will be the same as the local file name.
  16. ' The function is based on Chilkat's own sample for the ChilkatFTP2 component
  17. ' (which is not free): http://www.example-code.com/vbscript/ftpPutFile.asp
  18. '
  19. ' Arguments:
  20. ' locFile    [string]   the (path and) file name of the file to be uploaded
  21. ' targetDir  [string]   the (relative) path of the remote target directory;
  22. '                       if empty, the current remote directory will be used
  23. ' host       [string]   the remote host name (e.g. "ftp.mydomain.org")
  24. ' user       [string]   the login name for the remote host
  25. ' password   [string]   the password for the login account
  26. ' blnMkDir   [boolean]  if True, the remote directory will be created if it
  27. '                       doesn't exist, otherwise the function will fail if
  28. '                       the remote directory doesn't exist
  29. '
  30. ' The ChilkatFTP ActiveX component can be downloaded from:
  31. ' http://www.chilkatsoft.com/download/FtpActiveX.msi
  32. ' Documentation can be found at:
  33. ' http://www.chilkatsoft.com/refdoc/xChilkatFtpRef.html
  34. '
  35. ' Written by Rob van der Woude
  36. ' http://www.robvanderwoude.com
  37.  
  38. 	' Standard housekeeping
  39. 	Dim objFSO, objFTP, ok, strRemFile
  40.  
  41. 	' Extract the local file name and extension only from its path
  42. 	Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  43. 	With objFSO
  44. 		strRemFile = .BuildPath( targetDir, .GetFileName( locFile ) )
  45. 	End With
  46. 	Set objFSO = Nothing
  47.  
  48. 	' Create a ChilkatFTP object
  49. 	Set objFTP = CreateObject( "ChilkatFTP.ChilkatFTP.1" )
  50.  
  51. 	' pass the connection properties to the object
  52. 	objFTP.Username = user
  53. 	objFTP.Password = password
  54. 	objFTP.Hostname = host
  55.  
  56. 	' Connect, abort and return error message on failure
  57. 	ok = objFTP.Connect( )
  58. 	If ( ok <> 1 ) Then
  59. 		FTPUpload = objFTP.LastErrorText
  60. 		Set objFTP = Nothing
  61. 		Exit Function
  62. 	End If
  63.  
  64. 	If targetDir <> "" Then
  65. 		' If specified, create target directory
  66. 		If blnMkDir = True Then
  67. 			objFTP.CreateRemoteDir targetDir
  68. 		End If
  69. 		' Change directory remotely, abort and return error message on failure
  70. 		ok = objFTP.ChangeRemoteDir( targetDir )
  71. 		If ( ok <> 1 ) Then
  72. 			FTPUpload = objFTP.LastErrorText
  73. 			objFTP.Disconnect()
  74. 			Set objFTP = Nothing
  75. 			Exit Function
  76. 		End If
  77. 	End If
  78.  
  79. 	' Upload the file, abort and return error message on failure
  80. 	ok = objFTP.PutFile( locFile, strRemFile )
  81. 	If ( ok <> 1 ) Then
  82. 		FTPUpload = objFTP.LastErrorText
  83. 	End If
  84.  
  85. 	' Disconnect,and release the object
  86. 	objFTP.Disconnect( )
  87. 	Set objFTP = Nothing
  88.  
  89. 	' Return result
  90. 	FTPUpload = "Upload succeeded"
  91. End Function
  92.  

page last modified: 2024-04-16; loaded in 0.0088 seconds