Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If a folder is not empty, do something
#1
I was not able to create a function with this condition. I tried the following code:

if(FileExists("$user profile$\01\*.*"))
    ReplaceToolbarButtonText 0 "Not empty" "File Explorer toolbar"

It works if I refer to some specific file, but it does not work if I use asterisks to refer to any kind of file, as I did in the above code.
#2
Try this function

if IsFileExist("$user profile$\01\*.*")
    ReplaceToolbarButtonText 0 "Not empty" "File Explorer toolbar"

Function IsFileExist
Code:
Copy      Help
function# str'file

lpstr s=dir(_s.expandpath(file))
if(s=0) ret 0
ret 1
#3
Let’s suppose that the folder I want to check is C:\

What should be the code for the function?
#4
Macro Macro33
Code:
Copy      Help
if IsFileExist("c:\*.*")
,;do something
#5
Have you tested your code?
#6
I've tested on my part. What about you, did you try it out? What did you notice?
#7
I see a couple issues with this function 
#1. if folder contains subfolders and has files doesn't detect it.
#2. dir is an obsolete function
   

Maybe something like this would be better

Function IsFolderEmpty
Code:
Copy      Help
function# str'folderpath

;checks folder and its subfolders(if they exist) to see if it's empty or not
;
;REMARKS
;;folderpath- must end with a \ and be top level of folder to check(subfolders are automatically checked)
;;folderpath example- $documents$\test folder\   (test folder is the desired(TopLevel) folder to check)
;;Returns: 1 if folder is not empty, 0 if  folder is empty or If folder doesn't exist

;EXAMPLES
;if IsFolderEmpty("$documents$\test folder\")
,;out "not empty"
;if !IsFolderEmpty("$documents$\test folder\")
,;out "empty"
;int ReturnValue=IsFolderEmpty("$documents$\test folder\")
;out ReturnValue

str folder.getpath(folderpath "\*")
Dir d
foreach(d folder FE_Dir 0|4|32)
,if d.dir("*.*" 0)
,,ret 1
ret 0
Macro MacroExample
Code:
Copy      Help
if IsFolderEmpty("$documents$\test folder\")
,out "not empty"
 this is not  the only solution 
Feel free to correct or improve on this function or offer different solutions
#8
Kevin you're right, "dir" is now obsolete. So now I update my "IsFileExist" function to this:
Function IsFileExist
Code:
Copy      Help
function# str'file

Dir d; str s
foreach(d _s.expandpath(file) FE_Dir 0|4|32)
,s = d.FileName
,if s.len
,,ret 1
ret 0

Macro IsFileExist Test
Code:
Copy      Help
out
;Note: folders ("c:\x1" and "d:\z1" are just empty folders for testing only.
str files=
;c:\*
;c:\temp\*.csv
;c:\temp\*.htm
;c:\temp\*.txt
;c:\temp\*.docx
;c:\x1\*
;d:\*
;d:\download\*.apk
;d:\download\*.epub
;d:\download\*.mobi
;d:\download\*.pdf
;d:\download\*.txt
;d:\download\*.rar
;d:\z1\*

str file
foreach file files
,if IsFileExist(file)
,,out F"{file} exist."
,else
,,out F"{file} is NONE!"


Forum Jump:


Users browsing this thread: 1 Guest(s)