cURL / Mailing Lists / curl-library / Single Mail

curl-library

Trying to download and remove files from server via vb.net - need help

From: <lists_at_waynesplace.com>
Date: Wed, 02 Jul 2008 00:14:29 -0400

I want to loop through a list of files and download them and, if a flag is set, delete it the file from the server after download...if one other flag is set then delete a slightly different file from the server.

I am able to download the files OK (have to add a small amount of sleep to get it to work without error - strange). When the delete flag is set it starts to fail on every file it tries to download.

Here is what I have (some variable names have been changed for simplicity, but other then that it is the real code):
===helper functions===
Public Function OnProgress(ByVal extraData As Object, _
    ByVal dlTotal As Double, ByVal dlNow As Double, _
    ByVal ulTotal As Double, ByVal ulNow As Double) As Int32
    'Console.WriteLine("Progress: {0} {1} {2} {3} {4}", dlTotal, dlNow, _
    'ulTotal, ulNow, extraData)
    'UpdateStatusBar(extraData)
    Return 0
End Function
' Called by libcURL.NET when it has data for your program
Public Function OnWriteData(ByVal buf() As Byte, _
    ByVal size As Int32, ByVal nmemb As Int32, _
    ByVal extraData As Object) As Int32
    'Console.Write(System.Text.Encoding.UTF8.GetString(buf))
    'Dim fs As New FileStream
    If bSTOP Then
        Return 0
    End If
    FTPfs.Write(buf, 0, nmemb)
    Return size * nmemb
End Function
Public Sub OnDebug(ByVal infoType As CURLINFOTYPE, _
    ByVal msg As String, ByVal extraData As Object)
    'Console.WriteLine(msg)
End Sub
======

===Code===
Curl.GlobalInit(CURLinitFlag.CURL_GLOBAL_ALL)

Dim CURLFTP As Easy
CURLFTP = New Easy
CURLFTP.SetOpt(CURLoption.CURLOPT_USERPWD, UID + ":" + PWD)

Dim pf As Easy.ProgressFunction
pf = New Easy.ProgressFunction(AddressOf OnProgress)
CURLFTP.SetOpt(CURLoption.CURLOPT_PROGRESSFUNCTION, pf)

Dim rf As Easy.WriteFunction
rf = New Easy.WriteFunction(AddressOf OnWriteData)
CURLFTP.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, rf)
CURLFTP.SetOpt(CURLoption.CURLOPT_WRITEDATA, FTPfs)

Dim Slist As New Slist()

**FOR File_List (The start of a loop of 1 to X files)**

FTPfs = New FileStream(Filename, FileMode.Create, FileAccess.Write, FileShare.None)

CURLFTP.SetOpt(CURLoption.CURLOPT_URL, "ftp://" + Server + "/" & Path & "/" & Filename)

CURLFTP.Perform()
FTPfs.Close()
Sleep(150)

If DOWNLOADFILE = ZERO BYTES Then
  Sleep(250)
  FTPfs = New FileStream(LocalSSPathProfile & "\" & Filename, FileMode.Create, FileAccess.Write, FileShare.None)
  CURLFTP.SetOpt(CURLoption.CURLOPT_URL, "ftp://" + Server + "/" & Path & "/" & Filename)
  CURLFTP.Perform()
  FTPfs.Close()
End If

If DELETE_FLAG_SET Then
  'Remove the file from server
   CURLFTP.SetOpt(CURLoption.CURLOPT_URL, "ftp://" + objProfile.Server + "/dummy")
   Slist.Append("DELE " & SSList.Item(icnt).Path & "/" & Filename)
   CURLFTP.SetOpt(CURLoption.CURLOPT_QUOTE, Slist)
   CURLFTP.Perform()
   Slist.FreeAll()
   If SOMEOTHER_DELETE_FLAG_SET Then
      Slist.Append("DELE " & Path & "/" & NameOnly & "." & OtherMask)
      CURLFTP.SetOpt(CURLoption.CURLOPT_QUOTE, Slist)
      CURLFTP.Perform()
      Slist.FreeAll()
   End If
End If

**Next - LOOP THROUGH FILES**

CURLFTP.Cleanup()
Curl.GlobalCleanup()

======

Issues:
1) If I do not put the "Sleep(150)" there every 8-10 files it downloads it gets a hit on a ZERO byte file (all files being download are > 0 size). At 150 it does not happen...is it to fast for vb.net?

2) So downloading is actually working nice and fast with the sleep there, when the DELETE_FLAG_SET is true it DOES remove the files from the server, but after the first download it then fails to download any more files the ZERO check and then fails there too and leaves a ZERO size file and moves on.

So why do I need the sleep to get the downloads to work and why when I add the delete code does it all fall apart?

Any help would be appreciated - thanks!

-Wayne
Received on 2008-07-02