how do I copy a file from my c drive to an FTP site using vbscript

Posted on 2005-03-10
Medium Priority
Last Modified: 2008-02-26
I have tried this but it doesnt work.

Dim fso
set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\test.txt", "ftp:\\ftp.mysite.com\test.bak"
set fso = nothing
msgbox "Done!"
Question by:nabuilder
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
  • 2
LVL 96

Expert Comment

by:Lee W, MVP
ID: 13513762
ftp doesn't work that way.  Not sure how you'd do it in vbscript.  Could write a script for you in batch.  Probably be simpler than vbscript.

Expert Comment

ID: 13514492
I agree with leew. Using ftp.exe included in Windows is an easy way to do this simple task.
You can create a script file to feed it whichever FTP commands you wish.
If you really want to use VBscript, you can create this script file with it, then call ftp.exe from there.
LVL 10

Expert Comment

ID: 13514944
On Error Resume Next

Dim MySite
Dim myPath

Set MySite= CreateObject("CuteFTPPro.TEConnection")

MySite.Host = "ftp://usrname:pwd@ftpsite.com/data/"

If (Cbool(MySite.IsConnected)) Then    
   MySite.LocalFolder = "F:\exports"

   myPath = "F:\exports\data.txt"
   MySite.Upload myPath
   Set MySite = nothing

End If

You'll have to have cuteFTP tough..
LVL 10

Expert Comment

ID: 13514956
or how about this one ?

<%@ Language=VBScript %>
' FTP via ASP without using 3rd-party components
' Ben Meghreblian 15th Jan 2002
' benmeg at benmeg dot com / http://benmeg.com
' This script assumes the file to be FTP'ed is in the same directory as this script.
' It should be obvious how to change this (*hint* change the lcd line).
' You may specify a wildcard in ftp_files_to_put (e.g. *.txt).

' NB: You need to have C:\winnt\system32\wshom.ocx registered to use the WSCRIPT.SHELL object.
' It is registered by default, but is sometimes removed for security reasons (no kidding!).
' You will also need cmd.exe in the path, which again is there, unless the box is locked down.
' Check with your web host/resident sysadmin if in doubt.
Dim objFSO, objTextFile, oScript, oScriptNet, oFileSys, oFile, strCMD, strTempFile, strCommandResult
Dim ftp_address, ftp_username, ftp_password, ftp_physical_path, ftp_files_to_put

' Edit these variables to match your specifications
ftp_address          = "ftp.server.com"
ftp_username         = "username"
ftp_password         = "password"
ftp_remote_directory = "subdirectory" ' Leave blank if uploading to root directory
ftp_files_to_put     = "file.txt"

On Error Resume Next
Set oScript = Server.CreateObject("WSCRIPT.SHELL")
Set oFileSys = Server.CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Build our ftp-commands file
Set objTextFile = objFSO.CreateTextFile(Server.MapPath("test.ftp"))
objTextFile.WriteLine "lcd " & Server.MapPath(".")
objTextFile.WriteLine "open " & ftp_address
objTextFile.WriteLine ftp_username
objTextFile.WriteLine ftp_password

' Check to see if we need to issue a 'cd' command
If ftp_remote_directory <> "" Then
   objTextFile.WriteLine "cd " & ftp_remote_directory
End If

objTextFile.WriteLine "prompt"

' If the file(s) is/are binary (i.e. .jpg, .mdb, etc..), uncomment the following line' objTextFile.WriteLine "binary"
' If there are multiple files to put, we need to use the command 'mput', instead of 'put'
If Instr(1, ftp_files_to_put, "*",1) Then
   objTextFile.WriteLine "mput " & ftp_files_to_put
   objTextFile.WriteLine "put " & ftp_files_to_put
End If
objTextFile.WriteLine "bye"
Set objTextFile = Nothing
' Use cmd.exe to run ftp.exe, parsing our newly created command file
strCMD = "ftp.exe -s:" & Server.MapPath("test.ftp")
strTempFile = "C:\" & oFileSys.GetTempName( )
' Pipe output from cmd.exe to a temporary file (Not :| Steve)
Call oScript.Run ("cmd.exe /c " & strCMD & " > " & strTempFile, 0, True)
Set oFile = oFileSys.OpenTextFile (strTempFile, 1, False, 0)

On Error Resume Next
' Grab output from temporary file
strCommandResult = Server.HTMLEncode( oFile.ReadAll )
' Delete the temporary & ftp-command files
Call oFileSys.DeleteFile( strTempFile, True )
Call objFSO.DeleteFile( Server.MapPath("test.ftp"), True )
Set oFileSys = Nothing
Set objFSO = Nothing
' Print result of FTP session to screen
Response.Write( Replace( strCommandResult, vbCrLf, "<br>", 1, -1, 1) )
LVL 96

Accepted Solution

Lee W, MVP earned 2000 total points
ID: 13515658
In batch, that script would be so much simpler:

Set ftpAddress=ftp.server.com
Set ftpUsername=username
Set ftpPassword=password
REM  Leave following line blank if uploading to root directory
Set ftpRemoteDir=/subdirectory/path
Set ftpFile=file.txt
Set FileIsTextOnly=Y

echo lcd > ftp.scr
echo open %ftpAddress% >>ftp.scr
echo %ftpusername% >>ftp.scr
echo %ftppassword%>>ftp.scr
If "%ftpRemoteDir%" NEQ "" echo cd %ftpRemoteDir% >> ftp.scr
echo prompt >> ftp.scr
If /i "%FileIsTextOnly%" == "Y" (
   echo asc >> ftp.scr
) Else (
   echo bin >> ftp.scr
if "%ftpfile%" == "*" (
   echo mput %filetoput% >> ftp.scr
) Else (
   echo put %filetoput% >> ftp.scr
echo bye >> ftp.scr
ftp -s:ftp.scrftp.scr.log
start notepad ftp.scr.log

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Occasionally Windows/Microsoft Updates will fail to update. We have found a code that will delete all temporary files and re-register all dll's related to Windows/Microsoft Updates! This works 99% of the time to get the updates working again! The…
In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

752 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