Results 1 to 7 of 7

Thread: ftp folder upload

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    ftp folder upload

    Hi, i know how to upload a single file to the web via FTP, but i don't know how to upload a folder via FTP using inet, can someone help me?

    thanks

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: ftp folder upload

    It's the same principle as uploading a single file, except you would loop through all files in the folder and inside that loop, put the upload code for that current file. Do you know how to list files in a directory?

    To include sub-directories, it might get a little complicated as you would need a recursive function and when you encounter a folder you send a different command (MkDir) to the FTP server.

    Can you show what code you have so far?

  3. #3

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: ftp folder upload

    Hi, well this is my current code.
    What should i do next?

    vb Code:
    1. Option Explicit
    2. Option Compare Text
    3. DefInt A-Z
    4.  
    5. Private WithEvents ftp As CFTPLink
    6.  
    7. Private Sub DirScan(ByVal RootDIR As String, ByRef DirList() As String, Optional ByRef tFolders As Long = 0)
    8.  
    9.     On Error GoTo ErrHandler
    10.    
    11.     ' Purpose: Gathers all sub directories within a given directory
    12.    
    13.     Dim fso As New FileSystemObject ' To use this, make sure you have a references set to "Microsoft Scripting Runtime"
    14.     Dim f As Folder                 ' Create a folder object
    15.     Dim qq As Control
    16.    
    17.     Set f = fso.GetFolder(RootDIR)  ' Set the folder object to the folder to be scanned
    18.    
    19.     If f.SubFolders.Count = 0 Then: Exit Sub    ' If there are no sub folders in this folder, then we can exit
    20.    
    21.     For Each qq In f.SubFolders
    22.         tFolders = tFolders + 1                 ' Increment total folders found
    23.         tProgress.Tag = tFolders                    ' The timer tag is being used to hold the current status to avoid defining a global variable
    24.         ReDim Preserve DirList(1 To tFolders + 1)   ' Redim our array to increase the array size to allow for more directory paths to be stored
    25.         DirList(UBound(DirList())) = qq              ' Assign our array index to the value of the directory that was found
    26.         DirScan qq, DirList(), tFolders              ' Where the recursion takes place.  We recall our same procedure to keep burrowing down in our directory tree.
    27.         DoEvents                                    ' Allow other process to occur during recursion
    28.     Next
    29.    
    30.     Exit Sub
    31.  
    32. ErrHandler:
    33.     MsgBox "Source: " & Err.Source & vbCrLf & _
    34.             "Number: " & Err.Number & vbCrLf & _
    35.             "Description: " & Err.Description
    36. End Sub
    37. Private Sub PopDirList(ByRef DirList() As String)
    38.  
    39.     Dim qqq, tmp
    40.     Dim f As Folder
    41. Dim fpath As String
    42. Dim fsys As FileSystemObject
    43. Set fsys = CreateObject("Scripting.filesystemobject")
    44. Dim a
    45.  
    46.  
    47.     tmp = UBound(DirList()) - 1
    48.     'tProgress.Enabled = True
    49.     'tProgress.Interval = 100
    50.     For qqq = 2 To tmp
    51.         'tProgress.Tag = Round(((i / tmp) * 100), 0)
    52.     Next
    53.     tProgress.Enabled = False
    54.    
    55.      
    56. End Sub
    57. Private Sub Command1_Click()
    58.     Dim tDir As Long, DirList() As String, ScanDIR As String
    59.     Dim f As Folder
    60.     Dim fpath As String
    61.  
    62. Dim fsys As FileSystemObject
    63. Set fsys = CreateObject("Scripting.filesystemobject")
    64.    
    65.    
    66.    
    67.    
    68.  
    69.     ' Set initial values
    70.     tDir = 0
    71.     ScanDIR = "c:\Users"
    72.    
    73.    
    74.     ' Need to set this initially or the "DirScan" procedure will break.
    75.     ReDim DirList(1 To 1) As String
    76.    
    77.     tProgress.Enabled = True            ' Start getting the progress info
    78.     tProgress.Interval = 1000           ' Interval set to One Second
    79.     DirScan ScanDIR, DirList(), tDir    ' Start scanning
    80.     DoEvents                            ' Allow other process to occur during scan
    81.     tProgress.Enabled = False           ' Stop getting the progress info since we are finnished scanning
    82.     DoEvents
    83.     PopDirList DirList()                ' Populate our ComboBox with our results
    84.  
    85.  
    86.     Dim i As Integer
    87.    
    88.     With ftp
    89.         .Server = "corvette.dreamhost.com"
    90.         .Username = "lycee"
    91.         .Password = "lyceea"
    92.        
    93.         For i = 0 To List2.ListCount - 1
    94.             .AddFileToSend List2.List(i), "/secured.ethiowish.com/" & DirList(i)
    95.         Next
    96.         .SendFiles
    97.     End With
    98.    
    99. End Sub
    100.  
    101. Private Sub Command2_Click()
    102.     List1.Clear
    103. End Sub
    104.  
    105. Private Sub Command3_Click()
    106.     Dim i As Integer
    107.    
    108.     For i = 0 To List2.ListCount - 1
    109.         If ftp.IsBinaryFile(List2.List(i)) Then
    110.             MsgBox List2.List(i) & " is a binary file"
    111.         Else
    112.             MsgBox List2.List(i) & " is a text file"
    113.         End If
    114.     Next
    115.    
    116. End Sub
    117.  
    118. Private Sub Form_Load()
    119.     Set ftp = New CFTPLink
    120.  
    121.  
    122.            
    123. End Sub
    124.  
    125.  
    126. Private Sub Form_Unload(Cancel As Integer)
    127.     Set ftp = Nothing
    128. End Sub
    129.  
    130. Private Sub ftp_StatusUpdate(vsText As String, vlEventType As StatusEventType)
    131.     Debug.Print "StatusUpdate [" & vlEventType & "] : " & vsText
    132.     List1.AddItem "StatusUpdate [" & vlEventType & "] : " & vsText
    133. End Sub
    134.  
    135. Private Sub List2_OLEDragDrop(data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    136.     Dim i As Integer
    137.    
    138.     For i = 1 To data.Files.Count
    139.         If (GetAttr(data.Files.Item(i)) And vbNormal) = 0 Then
    140.             List2.AddItem data.Files.Item(i)
    141.         End If
    142.     Next
    143. End Sub
    144.  
    145. Private Function GetFilename(ByVal vsFullPath As String) As String
    146.     Dim v As Variant
    147.    
    148.     If InStr(vsFullPath, "/") <> 0 Then
    149.         v = Split(vsFullPath, "/")
    150.         GetFilename = v(UBound(v))
    151.    
    152.     ElseIf InStr(vsFullPath, "\") <> 0 Then
    153.         v = Split(vsFullPath, "\")
    154.         GetFilename = v(UBound(v))
    155.    
    156.     Else
    157.         GetFilename = Replace(vsFullPath, ":", "_")
    158.        
    159.     End If
    160.    
    161. End Function

    Thanks

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: ftp folder upload

    do a search on recursive directory searching, there are many examples in the forums using fso, dir or api
    this thread http://www.vbforums.com/showthread.p...sive+directory has example for dir and fso, with some time comparisons, there is also an example of using api by vbasicgirl if you search
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: ftp folder upload

    In my code it searches also for folders, but when i try to upload folders it don't that's my problem

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ftp folder upload

    You can't upload a folder, you have to create the folder in the target machine and upload each file to the folder separately.

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: ftp folder upload

    Quote Originally Posted by Doogle
    You can't upload a folder, you have to create the folder in the target machine and upload each file to the folder separately.
    Ya and depending how you send or the way the target is setup you may have to send a Change Dir to the target so it's set to the folder you just created, then start uploading the files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width