Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone know how to FTP with VBS?

Status
Not open for further replies.

sshafe

Programmer
Oct 18, 2002
71
0
0
US
I am trying to FTP error messages within a script instead of setting up the email(which is tooo complicated).
Does anyone know how to do this?


Thanks :)
 
Compliments to codetoad.com and benmeg.com



<%@ Language=VBScript %>
<%
' FTP via ASP without using 3rd-party components
' Ben Meghreblian 15th Jan 2002
' benmeg at benmeg dot 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)
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 = &quot;ftp.server.com&quot;
ftp_username = &quot;username&quot;
ftp_password = &quot;password&quot;
ftp_remote_directory = &quot;subdirectory&quot; ' Leave blank if uploading to root directory
ftp_files_to_put = &quot;file.txt&quot;

On Error Resume Next
Set oScript = Server.CreateObject(&quot;WSCRIPT.SHELL&quot;)
Set oFileSys = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
' Build our ftp-commands file
Set objTextFile = objFSO.CreateTextFile(Server.MapPath(&quot;test.ftp&quot;))
objTextFile.WriteLine &quot;lcd &quot; & Server.MapPath(&quot;.&quot;)
objTextFile.WriteLine &quot;open &quot; & 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 <> &quot;&quot; Then
objTextFile.WriteLine &quot;cd &quot; & ftp_remote_directory
End If

objTextFile.WriteLine &quot;prompt&quot;

' If the file(s) is/are binary (i.e. .jpg, .mdb, etc..), uncomment the following line' objTextFile.WriteLine &quot;binary&quot;
' If there are multiple files to put, we need to use the command 'mput', instead of 'put'
If Instr(1, ftp_files_to_put, &quot;*&quot;,1) Then
objTextFile.WriteLine &quot;mput &quot; & ftp_files_to_put
Else
objTextFile.WriteLine &quot;put &quot; & ftp_files_to_put
End If
objTextFile.WriteLine &quot;bye&quot;
objTextFile.Close
Set objTextFile = Nothing
' Use cmd.exe to run ftp.exe, parsing our newly created command file
strCMD = &quot;ftp.exe -s:&quot; & Server.MapPath(&quot;test.ftp&quot;)
strTempFile = &quot;C:\&quot; & oFileSys.GetTempName( )
' Pipe output from cmd.exe to a temporary file (Not :| Steve)
Call oScript.Run (&quot;cmd.exe /c &quot; & strCMD & &quot; > &quot; & 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 )
oFile.Close
' Delete the temporary & ftp-command files
Call oFileSys.DeleteFile( strTempFile, True )
Call objFSO.DeleteFile( Server.MapPath(&quot;test.ftp&quot;), True )
Set oFileSys = Nothing
Set objFSO = Nothing
' Print result of FTP session to screen
Response.Write( Replace( strCommandResult, vbCrLf, &quot;<br>&quot;, 1, -1, 1) )
%>



Digga

Sharing Knowledge Saves Valuable Time!
 
Not sure why there are two FSO objects in that ASP page, but in general Digga has covered it.

The easiest way to FTP in a script is the same as in a batch file: run
Code:
FTP.EXE
with the -S:{command file} switch.

Or were you after something different?
 
I think that covers all of it. Thanks for all your help. :)
 
I am trying to run an FTP script in a batch file as described above, whenever I do an FTP -s:\temp\myscript from the DOS prompt, it just blinks and does nothing. I'm not sure I know what I'm doing wrong. Is there an error log somewhere. Here is my script:

opeN myfile
USER MYSELF
PASSWORD MYPASSWORD
PUT C:\TEMP\FFNGL012.dat mylib/FFNGL012
quit
 
Replace the IP address with the actual address. Replace UPPERCASE words with your equivalant.

open 10.0.0.1
MYUSERNAME
MYPASSWORD
lcd LOCALDIR
cd REMOTEDIR
put LOCALFILENAME
quit


In an FTP script, you don't need &quot;USER MyUsername&quot;, just &quot;MyUsername&quot; will suffice, having &quot;USER&quot; in front of the username might make it fail, I'm not sure. Your PUT command looks like it would work, but I'm not sure on that either so use LCD to change to the correct path on the local system and CD to change to the path of the remote system and then PUT Filename.ext w/o any paths for testing.
 
Thanks DANP129. it worked like a champ. One other thing, Is there a way I can get it to prompt for USER and PASSWORD so I don't have to hard code them. I tried taking USER and PASSWORD out of the script. However, when I run the FTP.exe command, it skips the USER prompt and prompts for PASSWORD

C:\>FTP -s:\TEMP\FTPSCRIPT.TXT
ftp> opeN 10.1.1.137
Connected to 10.1.1.137.
220-QTCP at HEALY400.
220 Connection will close if idle more than 5 minutes.
User (10.1.1.137:(none)):
331 Enter password.

530 Log on attempt by user USER rejected.
Login failed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top