Whoa! Here's some syntax that I've never seen before...
command1 > somefile | command2
What is that supposed to do? I tested it and I found that all of command1's output went to the file and nothing went to the pipe. But this means your while loop should run zero times. So I can't explain how you get anything. It looks to me like you should get nothing. And when I try your code, I do get nothing.
After I removed "> dir.txt", it seemed to work for me.
The -depth does no good so I would get rid of that. If a directory has subdirctories, it is not empty and does not need to be checked. You should check only "leaf directories". To do that add "-links 2" to your find statement (think about it). And you have a bug. A directory containing only ".secretfile" is not empty. Do a "ls -a" and check for 3 or more entries. ls sorts its output and a sort can take time. "ls -fa" will bypass the sort and possibly speed stuff up.
But I have no idea why you are getting everything.