Solved

Using FTP with VB

Posted on 2002-03-13
7
426 Views
Last Modified: 2007-12-19
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
Comment
Question by:GeekMan
7 Comments
 
LVL 3

Expert Comment

by:DennisL
ID: 6861523
www.vbcode.com

do a search for FTP and you'll get a bunch of sample code
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 6861740
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
 
LVL 6

Expert Comment

by:ebosscher
ID: 6861743
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:MikeRenz
ID: 6861927
you can connect to an ftp site like this:

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

that way it won't be anonymous.
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6862418
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
 

Author Comment

by:GeekMan
ID: 6862488
All good answers with useful tips I can really use. Will gladly post additional points for others that responded upon request.
Thanks.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6864431
Thanks for "A" grade!
I didn't try it but i like MikeRenz's comment.
cheers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now