Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


FTP Question Using Inet

Posted on 2001-08-27
Medium Priority
Last Modified: 2007-11-27
I'm thinking of writing an FTP program in VB using just the inet control that came with VB6.

I need to be able to: 1. check for presence of a particular file type on the internet server(e.g. *.html), 2. copy a file to the server, 3. copy a file from the server and, 4. delete a file in the server.

I need sample code with explanations please.

I am using Win98 and VB6.

Thanks so much!
Question by:limva
  • 6
  • 2

Accepted Solution

JonFish85 earned 400 total points
ID: 6430206
from MSDN:

GET file1 file2 Retrieves the remote file specified in file1, and creates a new local file specified in file2. Execute , _"GET getme.txt C:\gotme.txt"

MKDIR file1 Creates a directory as specified in file1. Success is dependent on user privileges on the remote host. Execute , "MKDIR /myDir"
PUT file1 file2 Copies a local file specified in file1 to the remote host specified in file2. Execute , _"PUT C:\putme.txt /putme.txt"

PWD Print Working Directory. Returns the current directory name. Use the GetChunk method to return the data. Execute , "PWD"
QUIT Terminate current connection Execute , "QUIT"
RECV file1 file2 Same as GET. Execute , _"RECV getme.txt C:\gotme.txt"

RENAME file1 file2 Renames a file. Success is dependent on user privileges on the remote host. Execute , "RENAME old.txt new.txt"

RMDIR file1 Remove directory. Success is dependent on user privileges on the remote host. Execute , "RMDIR oldDir"
SEND file1 Copies a file to the FTP site. (same as PUT.) Execute , _"SEND C:\putme.txt /putme.txt"

SIZE file1 Returns the size of the file specified in file1. Execute "SIZE /largefile.txt"

Expert Comment

ID: 6430214
look in MSDN under FTP I believe it was...

hope this helps!

Author Comment

ID: 6430226
Thanks for your help, JonFish85. I still have a few questions.  Maybe it would be simpler if you would tell me where MSDN is?  Is it on Microsoft's website or is it on the CD that came with VB6?  I think I need to know more of the commands that come after the Execute method (e.g. "QUIT", "DIR", etc.).  I also need to know more on how to interpret data that I receive back from the server.

Thanks again!  Waiting for your reply...
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 6430236
msdn.microsoft.com is the online MSDN... It also comes with VB6 enterprise I believe... Ill look for a MS article on VB & FTP

Expert Comment

ID: 6430252

Expert Comment

ID: 6430253
then there is this:


and if you dont want to buy the PAQ, my code was as follows:

'%%%%%%% This is in a module %%%%%%%%%%%%%%%%

Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String,
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long,
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Public Const MAX_PATH = 260

Public Type FILETIME
       dwLowDateTime As Long
       dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
       dwFileAttributes As Long
       ftCreationTime As FILETIME
       ftLastAccessTime As FILETIME
       ftLastWriteTime As FILETIME
       nFileSizeHigh As Long
       nFileSizeLow As Long
       dwReserved0 As Long
       dwReserved1 As Long
       cFileName As String * MAX_PATH
       cAlternate As String * 14
End Type

Public IsConnected As Boolean

Public Sub ShowFiles(data As String, lst As ListBox)
Dim cnt As Long
Dim temp As String
Dim StartAt As Long
Dim OldAt As Long

 lst.AddItem "Parent Directory"
 StartAt = InStr(data, vbCrLf)
 OldAt = 1
 Do While StartAt <> 0
   temp = Mid$(data, OldAt, StartAt - OldAt)
   temp = Replace(temp, vbCrLf, "")
   If Right$(temp, 1) = "/" Then
     temp = Left$(temp, Len(temp) - 1)
     temp = temp & " (Folder)"
     lst.AddItem temp
   ElseIf UCase(Right(temp, 4)) = ".URT" Then
     lst.AddItem temp
   End If
   OldAt = StartAt
   StartAt = InStr(OldAt + 1, data, vbCrLf)
End Sub

Public Function SearchForFiles(strFilename As String, Ext As String, lst As ListBox) As Boolean
Dim hSearch As Long
Dim success As Long
Dim buffer As String
Dim FindInfo As WIN32_FIND_DATA

If Ext = "" Then Ext = "*"
If Left(Ext, 1) <> "." Then Ext = "." & Ext
hSearch = FindFirstFile(strFilename & "*" & Ext, FindInfo)
If hSearch = -1 Then Exit Function

 buffer = Left(FindInfo.cFileName, InStr(FindInfo.cFileName, vbNullChar) - 1)
 lst.AddItem buffer
 success = FindNextFile(hSearch, FindInfo)
Loop Until success = 0

FindClose hSearch
If lst.ListCount > 0 Then SearchForFiles = True Else SearchForFiles = False
End Function

'%%%%%%%%%%%%%%%% End Module %%%%%%%%%%%%%%%%%%%%

'%%%%%%%%%%%%%%%% This is in the form's code %%%%

Dim CDir As String
Dim lngInterval As Long
Dim strUsername As String
Dim strPassword As String
Dim strFilename As String
Dim strExtension As String
Dim strHost As String

Private Sub cmdLogon_Click()
 If cmdLogon.Caption = "&Log In" Then
   If (txtHost.Text = "") Or (txtUsername.Text = "") Or (txtPassword.Text = "") Or (txtTimer.Text =
"") Then
     MsgBox "Please fill on all the information required!", , "Error"
     Exit Sub
   End If
   If Dir(txtFileDir.Text) = "" Then
     MsgBox "You specified an invalid directory!", , "Error"
     Exit Sub
   End If
   lngInterval = Val(txtTimer.Text) * 1000
   strUsername = txtUsername.Text
   strPassword = txtPassword.Text
   strFilename = txtFileDir.Text
   strHost = txtHost.Text
   strExtension = cboExt.Text
   cmdLogon.Caption = "&Exit"
   tmrCheck.Interval = lngInterval
   Unload Me
 End If
End Sub

Private Sub Command1_Click()
 Inet.UserName = "Username"
 Inet.Password = "Password"
 Inet.Execute LOGON, "PWD"
 Exit Sub
End Sub

Private Sub Inet_StateChanged(ByVal State As Integer)
Dim data As String
 Select Case State
   Case icConnected
     'Connected to FTP
     IsConnected = True
   Case icResponseReceived
     'Got a response
   Case icDisconnected
     IsConnected = False
   Case icError
     MsgBox Inet.ResponseCode & " : " & Inet.ResponseInfo
   Case icResponseCompleted
     data = Inet.GetChunk(2048, icString)
     If data = "/" Then
       lblIndex.Caption = "Index Of '" & data & "'"
       CDir = CDir & data
       Inet.Execute , "DIR"
     ElseIf data = "" Then
       Inet.Execute , "DIR"
       ShowFiles data, lstFiles
     End If
 End Select
End Sub

Private Sub lstFiles_DblClick()
 If Inet.StillExecuting Then Exit Sub
 If Not IsConnected Then Exit Sub
 If InStr(lstFiles.List(lstFiles.ListIndex), "(Folder)") Then
   CDir = CDir & Left(lstFiles.List(lstFiles.ListIndex), Len(lstFiles.List(lstFiles.ListIndex)) - Len("
(Folder)")) & "/"
   CDir = Replace(CDir, "//", "/")
   CDir = Replace(CDir, "///", "/")
   lblIndex.Caption = "Index Of '" & CDir & "'"
   Inet.Execute , "CD " & Left$(lstFiles.List(lstFiles.ListIndex), Len(lstFiles.List(lstFiles.ListIndex))
- Len(" (Folder)")) & "/"
 ElseIf UCase(lstFiles.List(lstFiles.ListIndex)) = "PARENT DIRECTORY" Then
   Dim temp() As String
   Dim cnt As Long
   temp = Split(CDir, "/")
   CDir = ""
   For cnt = LBound(temp) To (UBound(temp) - 2)
     CDir = CDir & temp(cnt) & "/"
   Next cnt
   CDir = Replace(CDir, "//", "/")
   CDir = Replace(CDir, "///", "/")
   'MsgBox CDir
   lblIndex.Caption = "Index Of '" & CDir & "'"
   Inet.Execute , "CDUP"
 End If
End Sub

Private Sub sckNoop_Timer()
 If IsConnected And Not Inet.StillExecuting Then
   Inet.Execute , "NOOP"
 End If
End Sub

Private Sub tmrCheck_Timer()
Dim ConnStr As String

 If Inet.StillExecuting Then Exit Sub
 If IsConnected Then Exit Sub
 If SearchForFiles(strFilename, strExtension, lstDir) = True Then
   'Found at least one file
   ConnStr = "ftp://" & strUsername & ":" & strPassword & "@" & strHost
   Inet.Execute ConnStr, "PWD"
   'Nothing found
 End If
End Sub

'%%%%%%%%%%%%%%%%%% End Form Code %%%%%%%%%%%%%%%%%%%%

hope this helps!

Author Comment

ID: 6430453
Thanks JonFish85, your code certainly helped!

Expert Comment

ID: 6430486
glad I could help! thansk for the points and especially for the A grade!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

571 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