(A little late to the answer ...)
How can a file be size zero is a little more complicated than provided by the above answers. The question is tagged Win7, but looking at other "simpler" file systems such as FAT or NTFS, may be useful as the concepts are similar.
The disk does not "know" what is a file and what is a directory; it's all data in little blocks. The OS distinguishes between the meaning of data blocks. The first few a special, but the rest of the blocks hold either information about the data (eg: file name, file length, first data block holding the data), or the data itself.
A directory is a special "file" whose "data" the OS understands is an information block containing information about files, not the content of the files. A good analogy is a physical library and the card catalog. Think of the information blocks as the card catalog and the shelves as the data blocks (card catalog also sits on a shelf-like structure).
When you "create" a file (say with UNIX touch
command), the OS first creates an entry in an information block (directory), with the following:
- Name = My_File.txt
- Length = 0
- Starting Data Block = N/A
- Additional info (owner, permissions, created/updated/modified date), etc
Only if there is some data to "write" does it attempt to find an empty data block to store the data. But the data blocks come in fixed sizes (say 32K) convenient for the disk to get to and the OS to read. If you only write "Hello", most of the block is "empty" (actually may not be zeros, but garbage from what was there before), so the table also now updates the size to the length (say 5 chars + End of File) so you don't get the bad stuff.
When you update the "file" to a length > block size, the OS writes the data to the new block and updates a data block to say the file continues onto next block AFTER the first (and so on) and the length is updated the new length (details differ).
What you end up with is a collection of information data blocks (directories or lists) with information about the chains of data blocks (file contents).
Logically, this also explains why a file move on the same filesystem is blinking fast while a copy takes a long time. The OS only has to edit 2 directory blocks to remove the entry from one directory (information data block) and add to another. Delete a file: just remove the entry in the directory block, freeing up the file data blocks to be reallocated.
ps: Just because the card catalog has entry for a book does not mean it's on the shelve (checked out or lost perhaps); file size 0.
pps: A misplaced book inside library implies search library, or in computer terms: chkdsk or repair disk!
A greater understanding can be gleaned by reading about UNIX inodes or appreciating how version control systems (ClearCase, TFS, Git, etc.) manage not only files and directories, but also versions of files and even versions of directories. In most cases, everything is stored in a database and presented to the user to appear as classical directory structure and files!
82the file name does not count in the file, that how it can be explained. – njzk2 – 2015-09-15T19:23:51.707
124I'm reminded of a friend in college who wrote a piece of software to store text as filenames to get around the disk quota. – slebetman – 2015-09-17T02:38:27.400
3@slebetman But then what happens when the MFT (or FAT or whatever it was) grew too big? Or was it a directory size quota? – Cole Johnson – 2015-09-17T20:27:25.107
16
@ColeJohnson I was an intern back in the 2000's in one of my U's computer lab, and the user quota was calculated as sum of filesizes. So storing data as file names would indeed get around qouta. Heck you could save a program in folders and it would not count against your quota.
– Mindwin – 2015-09-17T21:22:31.0538Also, you might find it interesting that, in some cases, the file can even contain actual data and still be reported as having zero size. See file systems with forks (for example, NTFS ADS). – T. C. – 2015-09-17T22:19:26.180
2@ColeJohnson: The disk would be full so the sysadmin would notice I guess. But he never got caught. We had a 10MB quota at the time on I'm guessing a 500MB shared disk. He had lots of pictures and graphs he needed for his dissertation so that used up a lot of space. So he cheated on the text storage. – slebetman – 2015-09-18T02:07:11.127
@ColeJohnson: Back in 1999 a laptop was still quite expensive so around 80% of students used the lab for work. I guess these days personal computers are more common – slebetman – 2015-09-18T02:09:40.597
1@slebetman out of curiosity, do you remember how he encoded the file names? I'd assume base-64 or something – Cole Johnson – 2015-09-18T02:39:42.500
3@ColeJohnson I think it was just plain text with some encoding for special characters. If I remember correctly, the first 4 digits were the sequence numbers. – slebetman – 2015-09-18T03:10:52.953
21@slebetman This is the point where the line between genius and insanity becomes blurred. – Pharap – 2015-09-18T16:47:22.277
10
A similar technique was famously used in a compression challenge,
– Oddthinking – 2015-09-20T13:12:27.2172
possible duplicate of How are the file metadata stored in Windows?
– Ching Chong – 2015-09-24T09:13:09.420@slebetman, So.. did it work? Does it scale? – Pacerier – 2016-08-19T11:26:51.713
@Pacerier: It worked for him. He stored around 50MB of data I think (not exactly sure of the details). That was way more than the 10MB limit. – slebetman – 2016-08-19T14:37:32.763