Sign in to follow this  
Followers 0
RogerL

FileFindFirstFile in empty root dir returns @error = 0?

14 posts in this topic

I wish to test if a USB drive is empty. (My first post and I'm fairly new to AutoIt so please be gentle)

I have found that DriveSpaceFree and DriveSpaceTotal return the same value if it is empty so I can use this in my code but I tried first to use FileFindFirstFile and found that for empty subdirs it returns @error = 1, but 0 for root. Is there something wrong in my code below or is it a bug?

; before running this - formated drive L:, checked Windows hadn't created any hidden files
;
#include <file.au3>
$logFile = @TempDir & "\test.log"
_FileWriteLog ($logFile, DriveSpaceFree("L:") & ", " & DriveSpaceTotal("L:\")); same = empty?!

$file = FileFindFirstFile("L:\*.*")
_FileWriteLog ($logFile, "$file = " & $file & ", @error = " & @error)

DirCreate("L:\Temp")
$file = FileFindFirstFile("L:\Temp\*.*")
_FileWriteLog ($logFile, "$file = " & $file & ", @error = " & @error)

_FileWriteLog ($logFile, DriveSpaceFree("L:") & ", " & DriveSpaceTotal("L:\")); differ by size of dir
#cs
contents of $logFile:
2008-02-23 17:51:56 : 1936.84375, 1936.84375
2008-02-23 17:21:00 : $file = -1, @error = 0  <------------------ shouldn't this be 1?
2008-02-23 17:21:00 : $file = -1, @error = 1
2008-02-23 18:06:19 : 1936.8125, 1936.84375
#ce

Share this post


Link to post
Share on other sites



I cannot say yes as being a bug. "Returns -1 if error occurs. If the Folder is empty the @error is set to 1." Technically the root is not a folder. And MSDN are unclear to the error response other then invalid handle which the -1 return basically covers. So should it be @error = 1, I will pass on that one :) .

Share this post


Link to post
Share on other sites

Technically the root is not a folder.

Did Microsoft change the rules when they renamed directories as folders (to help make computing more friendly for Joe Public)?

When did the root directory become not a directory? Isn't it just a special directory in that it can't be 'RMDIR'ed or 'MKDIR'ed and doesn't have a parent.

Anyway I would be grateful for the recommended way to test for the root directory to be empty.

Share this post


Link to post
Share on other sites

Did Microsoft change the rules when they renamed directories as folders (to help make computing more friendly for Joe Public)?

Not sure what set of rules you are referring to.

A directory is a index/listing. A folder is an virtual container that can have files or folders within. They are not the same though some times they seem to be the same.

Share this post


Link to post
Share on other sites

A folder is an virtual container that can have files or folders within.

So why isn't the root directory a folder as it can have files and folders within it?

Share this post


Link to post
Share on other sites

So why isn't the root directory a folder as it can have files and folders within it?

Because it is a root of a drive that has a directory but it is not a folder. Simply stating that both can contain files or folders does not make them the same so quit trying to make them the same.

There are 2 registry keys that exist that allow you to add items to your context menu for this example. If I wanted to add an item into the registry for right click on a folder. Would I choose the "folder" key or the "directory" key. If you can tell me the answer and why the choice, then you may realize the difference. There is also a key named "drive". Those keys exist because there is a difference between them.

A telephone directory is not a telephone folder. Get the point? :)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Because it is a root of a drive that has a directory but it is not a folder. Simply stating that both can contain files or folders does not make them the same so quit trying to make them the same.

There are 2 registry keys that exist that allow you to add items to your context menu for this example. If I wanted to add an item into the registry for right click on a folder. Would I choose the "folder" key or the "directory" key. If you can tell me the answer and why the choice, then you may realize the difference. There is also a key named "drive". Those keys exist because there is a difference between them.

A telephone directory is not a telephone folder. Get the point? :)

I aplogise for the delay in replying. I'll be honest I didn't understand your post and have been agonizing about how to reply. I'm not good at writing English. So often forum posts get misinterpreted.

Not sure what context menu you are refering to. I am not a windows programmer and using the registry to explain the difference between folders and directories is a bit 'back to front'

There were no differences between directories and folders in MS-DOS. There were none in WIndows 3.1. Did MS introduce folders in Win95? I don't remember.

I would appreciate you pointing me to material on the web tha can help me undertand how MS use the two terms internally.

As a layman I understand that a telephone directory is a list of telephone numbers and I would guess that in this context a telephone folder would be the cover.

I am surprised, given your explanation, that AutoIt has no Folder Functions. At the very least one might expect DirCreate to be FolderCreate.

Finally I would apprecitate it if thought could be given to amending the FileFindFirstFile help to say that - "it does not return @error = 1 if the root directory is empty as it is not strictly a 'folder' " (or amending the code if it is possible as users might expect it to)

Kind Regards,

Roger

Edited by RogerL

Share this post


Link to post
Share on other sites

I guess you could actually iterate through FileFindNextFile and just count how many loops you do until it stops. If it's 0, then tadaa, empty folder.

$handle = FileFindFirstFile('u:\*.*')
$count = 0
While 1
    FileFindNextFile($handle)
    If @error Then ExitLoop
    $count += 1
WEnd
FileClose($handle)

ConsoleWrite($count & ' files in folder')
Thank you Saunders very much. That does it for me. (I apologise for the delay in replying, been waiting till I replied to MHz)

Share this post


Link to post
Share on other sites

Look here

Thanks for pointing me to that Zedna. (I apologise for the delay in replying, been waiting till I replied to MHz)

If the root dir is empty the array doesn't display but I couldn't find return values that allowed me to test this condition in the code. Anyway I'm happy to go with Saunders solution so no need for more help on this unless you want to.

Share this post


Link to post
Share on other sites

I would appreciate you pointing me to material on the web tha can help me undertand how MS use the two terms internally.

Honestly Roger, I think you are smart enough to research on your own. You are correct about the telephone directory though I never considered a cover to be a folder. And FolderCreate maybe more correct but using Dir* for everything folder or directory related saves dilution of function prefixes and confusion like trying to explain the difference between those two items in this thread. By the sounds of your reply, you do know the difference so I do not understand your objective reply of using sarcasm.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

By the sounds of your reply, you do know the difference so I do not understand your objective reply of using sarcasm.

No sarcasm was intended. Would appreciate you quoting the piece you interpret as sarcasm so I can avoid it in the future. I started my last post saying "So often forum posts get misinterpreted"... Edited by RogerL

Share this post


Link to post
Share on other sites

No sarcasm was intended. Would appreciate you quoting the piece you interpret as sarcasm so I can avoid it in the future. I started my last post saying "So often forum posts get misinterpreted"...

The Windows 3.1 seemed usual to have mentioned for it's primitive design so perhaps that invoked my feeling of sarcasm in use. I will retract the remark and put it as a misunderstanding. I am glad the others have shown you a solution to your issue so I wish you luck with the fix.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0