Check for empty folder problem.

Hi,
I am using the following code to check if a folder has content:

[code]// CHECK IF FOLDER IS EMPTY, IF NOT, DISPLAY THE CONTENTS
if f.count>=1 then

// OPEN FOLDER
f.Launch

else
MsgBox(“Folder Empty”)
End if[/code]

The problem is - if it has content, and then I delete it - the next time the code is run, the MsgBox does not show because there must be some kind of hidden file still inside the folder.

I am pretty sure there is some kind of .blahblah file created whenever a file has content, but I cannot remember the name of it?

Any Ideas how I exclude hidden files?

The file you are remembering is .DS_Store

.DS_Store stores information about window settings (position, size, display style)

THATS THE ONE!!!
Any ideas how I can ignore that file?

If you don’t need it, you can delete it before you do your if statement. If it’s a folder your user is supposed to use I wouldn’t do that though.

This may not be the most efficient way, but I’m pretty sure it’d work.

  if f.count > 0 then
    dim showFolder as boolean = false

    // Items in folder, display the folder.
    showFolder = true
    
    if f.count = 1 and f.item(1).name = ".DS_Store" then
      // Turns out, the only thing there is .DS_Store;
      // nevermind on that opening thing.
      showFolder = false
    end
    
    if showFolder then
      // Launch the folder!
      f.Launch
    end
    
  end

Annnnd edit button isn’t there.
Code amendments:

[code] if f.count > 0 then
// Items in folder, display the folder.
dim showFolder as boolean = true

if f.count = 1 and f.item(1).name = ".DS_Store" then
  // Turns out, the only thing there is .DS_Store;
  // nevermind on that opening thing.
  showFolder = false
end

if showFolder then
  // Launch the folder!
  f.Launch
end

end
[/code]

Thanks matey :slight_smile:

Here’s a function that tells you if there’s any visible files in a directory:

Function HasVisibleFiles(dir As FolderItem) As Boolean Dim count As Integer = dir.Count For i As Integer = 1 To Count Dim child As FolderItem = dir.TrueItem(i) If child.Visible Then Return True Next End Function

There can be a lot of hidden stuff other than .DS_Store. For example, on some non-HFS+ volumes, files with extended attributes will have a secondary “shadow” file that contains the extended attributes.

I’m a bit lost now as to how I use it.
Do I have to put that in a method, or do I put that code inline, and then underneath add the code:

If HasVisibleFiles(f) then f.Launch else MsgBox("Nuttin here") end if

I hate being a novice :frowning:

Yes, put it in a method, maybe in a module. Depending on your style, you may also change the parameter to Extends dir As FolderItem. That will allow

Dim fi As FolderItem = SpecialFolder.Desktop
If fi.HasVisibleFiles Then
    // Do something
End If

The other way, you would use it as:

Dim fi As FolderItem = SpecialFolder.Desktop
If HasVisibleFiles(fi) Then
    // Do something
End If

Thanks Jeremy!

Just tried that and I get the following error message:

[quote=105719:@Richard Summers]Just tried that and I get the following error message:

[/quote]

Sorry, think-o on my part. It should be “TrueItem” and not “TrueChild”.

That did the trick - thanks Joe :slight_smile: