File Drop Count?

My app accepts dropped files. Is there a property for the number of files dropped?

Sub HandleDataDropObjects(Obj As DragItem)

//how many can I expect?

Do  

  if Obj.FolderItemAvailable then
    //Do stuff with this file
  end if

Loop until not obj.NextItem




I can rearrange my actual code, but was wondering if there is a count?

Maybe there is, maybe there isn’t, so what I’ve done in the past for this situation is put the handling into a Do Loop (like you have), and add the files to an array property of type FolderItem.

Private mDroppedFiles() As FolderItem

Plus, add a Timer and a method for handling multiple dropped files.

Then my Do Loop looks like the following:

Do
    
  // the DragItem object could contain a collection of one or more files
  // - so add each item to a local property array before calling the Timer logic
  Me.mDroppedFiles.Add(obj.FolderItem)
  
  If Me.mDroppedFileTimer Is Nil Then
    // setup timer and attempt to run
    Me.mDroppedFileTimer = New Timer()
    AddHandler Me.mDroppedFileTimer.Action, AddressOf Me.DroppedFiles_Action
    Me.mDroppedFileTimer.Period = 50
    Me.mDroppedFileTimer.RunMode = Timer.RunModes.Single
    
  Else
    // but if multiple files are being loaded, reset to allow
    // time to get all the files in the array, before letting the timer run
    Me.mDroppedFileTimer.Reset()
  End If
  
Loop Until Not obj.NextItem()

Afterwards, in the DroppedFiles_Action method, I can count the number of files by getting the Count from the array property. I hope that helps.

  var x as integer = 0
  
  If obj.FolderItemAvailable Then
    Do
      x=x+1
    Loop Until Not obj.NextItem
  End If

X has number of files…

That’s almost right @Pawel_Soltysinski, but your logic is a little incorrect. It’s possible that each item can be a file or not so it should be:

var x as integer = 0
  
Do
  If obj.FolderItemAvailable Then
      x=x+1
  End If
Loop Until Not obj.NextItem

true… Forgot about folders… but, on the other hand, the solution should also go into folders, so perhaps this:

//the app.FileCount Integer property was previously added as public property to App

If obj.FolderItemAvailable Then
  app.FileCount=0
  
  do
    CountFiles(obj.FolderItem)
  Loop Until Not obj.NextItem
  
End If

// app.FileCount has number of files

…and the CountFiles method:

Public Sub CountFiles(FolderOrFile as FolderItem)

  if FolderOrFile <>nil then
    if FolderOrFile.IsFolder then
      for i as Integer=1 to FolderOrFile.Count
        if FolderOrFile.Item(i).isfolder then
          ListFiles(FolderOrFile.Item(i)) 
        else
          app.FileCount=app.FileCount+1
        end if
      next
    else
      app.FileCount=app.FileCount+1
    end if 
  end if
  
End Sub

Greg was alluding to the fact that drops can contain anything, including pictures, text, links, raw data, custom data, whatever – not just files and folders.

2 Likes

Right. You have to check every item to see if it’s a folderitem, not just the first one.

wouldn’t this prevent dropping anything else besides files/folders? in Opening event:

Me.AcceptFileDrop(AllFiles.Any)

I was just pointing out that your logic was backwards. If your code is going to check if the object is a folderitem, it needs to check inside the loop, not outside.

I got it, thank You :slight_smile: my code was intended to be directly used in DropObject event, that’s why.

Even then, if you check for FolderItemAvailable once (outside the loop as you did), it should mean you expect there can be drops of something else than folderitems. So either don’t check at all (if you’re sure) or every time. :wink: