Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Using FTP with VB

I want to ftp to a Unix machine in VB and was looking at the Internet Transfer Control. The problem with the control is that it appears to be intended for anonymous sites.
I need to pass a login and password. Is there an API that I can use to invoke the standard ftp tool in Windows?

1. Is this the best way to accomplish what I'm trying to do?
2. Does anyone have any sample code they could share?

Thanks..
0
GeekMan
Asked:
GeekMan
1 Solution
 
DennisLCommented:
www.vbcode.com

do a search for FTP and you'll get a bunch of sample code
0
 
ebosscherCommented:
GeekMan,

the Inet control will allow to log in using a username and password.

the code looks like this:

With itcFTP
   .URL = strIP
   .Protocol = icFTP
   .RemotePort = 21
   .UserName = strUserId
   .Password = strPassword
End With


after you do that you can execute commands

itcFTP.Execute , "PUT " & strFileName & " " & strRemoteFileName
zsWait


the zsWait function looks like this:

Private Sub zsWait()
    ' keep going until we can stop
    While itcFTP.StillExecuting = True
        DoEvents
    Wend
End Sub


and in the same form you can use the function itcFTP_StateChanged(ByVal State As Integer) as a callback function allowing you to do things while the control is executing (like check the state variable to check what it's doing.

If you still don't like the Inet control look into the wininet.dll file, it contains function to allow you to do FTP (I believe it is a wrapper for the FTP functions of winsock)

Hope this helps,

Evan
0
 
ebosscherCommented:
Oh, in my prevous comment.. the itcFTP_StateChanged function (Sub) is defined for you as the callback function.. all you need to do is code the function
Cheers
0
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.

 
MikeRenzCommented:
you can connect to an ftp site like this:

ftp://username:password@ftp.myftp.com/

that way it won't be anonymous.
0
 
Richie_SimonettiIT OperationsCommented:
from msdn:
' there is a little trick to get to work...
HOWTO: Internet Transfer Control Using Username and Password
Last reviewed: October 16, 1997
Article ID: Q173264  
The information in this article applies to:
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0


SUMMARY
When setting the Internet Transfer Control URL, Username, and Password properties programmatically, the order in which you do it is very important. If you don't set the URL property before the Username and Password properties, you will get the following error:


   Run-time error '35754'
   Unable to connect to remote host


IMPORTANT: In the control shipped with Visual Basic 5.0 (not SP2) the Username is sent as Anonymous if the URL property is not set first (regardless of the UserName and Password properties). So when connecting to an FTP server, you would be accessing the URL with the Anonymous credentials, not with the credentials you intended (according Username and Password Inet properties).
NOTE: The Internet Transfer Control included with Visual Studio 97 Service Pack 2 is unable to make an authenticated connection using the Username and Password properties. For more information, see the following article in the Microsoft Knowledge Base:


   ARTICLE-ID: Q173265
   TITLE     : BUG: Internet Transfer Control (SP2) Username and Password


Workaround
This is sample code for setting the URL property before the Username and Password properties so you won't get the error described above:


   Inet1.URL = "ftp://YourFTPServer"
   Inet1.UserName = "Username"
   Inet1.Password = "Password"


Notice the URL property is set before the Username and Password properties.
This information is also documented in the Visual Basic 5.0 Readme file.



MORE INFORMATION


Steps to Reproduce Behavior

Start a new Standard EXE project. Form1 is added by default.

From the Project menu, click Components and select the Internet Transfer Control 5.0 into the project if it is not already loaded.

Place an Internet Transfer Control 5.0 (Inet1) on Form1 and add the following code:


      Private Sub Inet1_StateChanged(ByVal State As Integer)
       Dim vtData As Variant ' Data variable.



       Select Case State
       ' ... Other cases not shown.
       Case icError ' 11
           ' In case of error, return ResponseCode and

      ' ResponseInfo.
           vtData = Inet1.ResponseCode & ":" & _
           Inet1.ResponseInfo

       Case icResponseCompleted  ' 12
           Dim strData As String: strData = ""
           Dim bDone As Boolean: bDone = False

           ' Get first chunk.
           vtData = Inet1.GetChunk(1024, icString)
           DoEvents

           Do While Not bDone

               strData = strData & vtData
               ' Get next chunk.
               vtData = Inet1.GetChunk(1024, icString)
               DoEvents

               If Len(vtData) = 0 Then
                   bDone = True
               End If
           Loop

          MsgBox strData
       End Select
      End Sub



Place a CommandButton (Command1) on Form1 and add the following code:


      Private Sub Command1_Click()
          Inet1.UserName = "Username"
          Inet1.Password = "Password"
          Inet1.URL = "ftp://YourFTPServer"

          Inet1.Execute , "DIR"
     End Sub



From the Run menu, click Start (ALT, R, S) or press the F5 key to run the program. Click the Command1 button, and you will see error described above.  
0
 
GeekManAuthor Commented:
All good answers with useful tips I can really use. Will gladly post additional points for others that responded upon request.
Thanks.
0
 
Richie_SimonettiIT OperationsCommented:
Thanks for "A" grade!
I didn't try it but i like MikeRenz's comment.
cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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