in reply to readdir inconsistent!
If you are using ext3 there is a limit of 32k inodes per directory, however with Reiser and XFS it should not be an issue. Have 32k inodes in one directory is certainly crazy, and 500k is insanity. I would suggest organizing your data better (if my understanding of your system is correct).
If you want to debug this script, run it over a directory with 10 files and see what your results are. I think you are running into file system wierdness.