FTP Question Using Inet

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!
Who is Participating?
JonFish85Connect With a Mentor Commented:
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"
look in MSDN under FTP I believe it was...

hope this helps!
limvaAuthor Commented:
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...
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

msdn.microsoft.com is the online MSDN... It also comes with VB6 enterprise I believe... Ill look for a MS article on VB & FTP
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!
limvaAuthor Commented:
Thanks JonFish85, your code certainly helped!
glad I could help! thansk for the points and especially for the A grade!
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.

All Courses

From novice to tech pro — start learning today.