Solved

FTP Question Using Inet

Posted on 2001-08-27
8
686 Views
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!
0
Comment
Question by:limva
  • 6
  • 2
8 Comments
 
LVL 6

Accepted Solution

by:
JonFish85 earned 100 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"
 
0
 
LVL 6

Expert Comment

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

hope this helps!
0
 

Author Comment

by:limva
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...
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:JonFish85
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
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6430252
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6430253
then there is this:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20100617

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.Clear
 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
   DoEvents
   OldAt = StartAt
   StartAt = InStr(OldAt + 1, data, vbCrLf)
 Loop
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

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

Do
 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
   tmrCheck_Timer
 Else
   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
     'Disconnected
     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"
     Else
       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"
 Else
   'Nothing found
 End If
End Sub

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


hope this helps!
0
 

Author Comment

by:limva
ID: 6430453
Thanks JonFish85, your code certainly helped!
0
 
LVL 6

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

828 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