Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

If folder empty...


  • Please log in to reply
4 replies to this topic
  • Guests
  • Last active:
  • Joined: --
Hi, I'm new with AHK

I cant find how to check if a folder is empty

If C:\Documentes is empty

Or something like that

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
If !FileExist("C:\Documents\*.*")
not tested

  • Guests
  • Last active:
  • Joined: --
Thnks, I have tested it and doesnt work.

It always says its empty, even if there are documents in the folder

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
Try this:
Dir = %A_Temp%\ahkTest
FileCreateDir, %Dir%
MsgBox % IsEmpty(Dir)
FileAppend, , %Dir%\a.txt
Sleep 1000
MsgBox % IsEmpty(Dir)

IsEmpty(Dir){
   Loop %Dir%\*.*, 0, 1
      return 1
   return 0
}
I tested this, and it works.

earthbound
  • Validating
  • 4 posts
  • Last active: Dec 30 2016 02:24 PM
  • Joined: 23 Jul 2013

This is exactly what I searched for and want, thx.

I tweak that this way, reversing the 1 and 0 return values as you have them, to:

Dir = %A_ScriptDir%\ahkTest
FileCreateDir, %Dir%
MsgBox % IsEmpty(Dir)
FileAppend, , %Dir%\a.txt
Sleep 1000
MsgBox % IsEmpty(Dir)

IsEmpty(Dir){
   Loop %Dir%\*.*, 0, 1
      return 0
   return 1
}

-- as returning 1 (true) if the directory is empty and 0 (false) if it is not. I also move the temp folder to the script's directory so I can easily delete from the folder it creates to test again.