How do you detect if item is a file or folder.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Happyrever
Posts: 55
Joined: Sat Sep 10, 2011 12:22 pm

How do you detect if item is a file or folder.

Post by Happyrever » Mon Dec 05, 2011 8:20 pm

In the simple process of drag and drop of files into a field, the result is a text string showing the full path and file name.
However, the same result applies if a folder is drag & dropped. This is expected.
However, I need some way of excluding the case where it is a folder being D&D.
Example:-
C:/Users/Alan/Documents/Serial Numbers/Alans drivers/U-values

U-values is a folder and not a file and must be excluded.

I would expect the point to detect the folder rather than file is at the point of starting to drag the item, but cannot think how to detect this.
Any ideas or code?

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How do you detect if item is a file or folder.

Post by Klaus » Mon Dec 05, 2011 8:44 pm

Hi Happy,

unfoertunately you cannot prevent this from being dragged to your app!

I usually "postprocess" the dropped files after the user dropped them like this:

Code: Select all

...
put the dragdata["files"] into tFiles
## This does ALSO contain dropped folders!
repeat for each line i in tFiles:

  ## We don't need no stinking badg... erm. folders here :-)
  if there is folder i then
    next repeat
  end if
  put i & CR after tRealFiles
end repeat
delete char -1 of tRealFiles
...
Now tRealFiles only contains files :D


Best

Klaus

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How do you detect if item is a file or folder.

Post by mwieder » Mon Dec 05, 2011 9:00 pm

Well, you can *sort of* do this by trapping it in dragEnter:

Code: Select all

on dragEnter
    if there is a folder the dragData["files"] then
        set the dragAction to "none"
    else
        set the dragAction to "copy"
    end if
end dragEnter
see an example at http://lessons.runrev.com/s/lessons/m/4 ... -on-Fields

If you can't be sure that the user is dragging only one file/folder at a time then you have to parse through the dragData["files"] one line a a time, eliminating the ones that are folders. It's a bit messy.

Happyrever
Posts: 55
Joined: Sat Sep 10, 2011 12:22 pm

Re: How do you detect if item is a file or folder.

Post by Happyrever » Mon Dec 05, 2011 9:34 pm

Thank you both for your advice.
I had tried the Drag version mwieder suggested, but it did not work. I put that down to me rather than the suggestion. However, it is expected the buser will drag afolder full of files, so that knocks it on the head anyway.
The post processing is fine for me and I can slip that in without any disruption to my code. I am sure this will work and will try it tomorrow.
Thanks Klaus

Post Reply