1

Getting an error trying to write to USB. CreateFile() returns 0x82 (The directory or file cannot be created). File being copied to the root folder. The problem ended up being that the USB drive is FAT formatted. I found this out when on a hunch I manually dragged and dropped the file onto the USB, and got a Windows dialog that said "The root drive I:\ is formatted with the FAT fiel system and can only store a limited number of items. Store the items in a subfolder, or reformat the drive using the NTFS file system".

Working on Win7 with VS2010 C++. I need to be able to give the user feedback about what the problem is, rather than just the generic "The directory or file cannot be created".

Is there a way that I can query the USB drive to find out if the error came up because of having passed the maximum number of files allowed in the root folder? A call to GetVolumeInformation() doesn't seem to give me this information, and a search of the net doesn't show anyone talking about how to do this (or I am not finding the right question to ask).

Since there are FAT8, FAT12, FAT16 and FAT32 out there, I'm assuming these each have different maximums on the number of folders/files in the root folder. I did find that FAT32 allows the number of files in the root folder to grow, which seems to say that there is no specific maximum number of folders/files for root in FAT32. But for FAT16 there seems to be, and so the other formats, may have different maximums.

Any idea where I could look next for this type of information. Thanks ahead of time for any help.

bmahf
  • 965
  • 2
  • 11
  • 27
  • 1
    You may have to do the grunt work yourself, and get a count of items in the root and compare to known constants. – Mikel F Feb 01 '17 at 20:33
  • I recommend that you don't bother. Check for that error code and give the user a message that includes something along the lines of "this might be because there are too many items in the root folder". You don't need to tell them that this definitely IS or definitely ISN'T the problem, just let them know that it is one possible cause and let them figure it out. (I very much doubt that Windows is checking that the root folder is full when it displays that dialog, it just sees error 0x82 on a FAT file system and assumes that must be the cause.) – Harry Johnston Feb 01 '17 at 21:39
  • FAT16 was a hard 512 in the root. FAT32 allowed about 64K name blocks in all folders, but long names take more blocks so the maximum number of files in a folder changes. Not worth dredging the entire directory and computing the number just to generate an error message. – user4581301 Feb 01 '17 at 21:51
  • "CreateFile() returns 0x82" I assume you mean GetLastError returned 0x82 after you attempted to create the file. Right? – Adrian McCarthy Feb 01 '17 at 22:50
  • Yes, GetLastError(). Would be nice to be more exact,but if you think Windows is taking the shortcut, then I probably should too. Thanks for the information. I was afraid this would be the case from what I saw looking around. – bmahf Feb 02 '17 at 20:24
  • Actually, let me try one more question (2 parts). Would it then be correct for me to say that for FAT16 I can actually count folders/files in the root folder and know for sure it's a FAT problem for that format? And would it be correct to say that for FAT32, if I can't copy a file to a given folder, I could then add the file/folder name sizes together for all in the given folder, and if that plus the new name I'm trying to create exceeds 64k, then I can know for sure it's a FAT problem? Or is that thinking too simplistic? – bmahf Feb 02 '17 at 20:33

0 Answers0