Solved

FTP Question Using Inet

Posted on 2001-08-27
8
680 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now