Why is Windows not warning about file in use for certain programs?

5

0

When you open a text file in Notepad on Windows, you can move the source file to the Recycle Bin or even permanently delete it. The same is true for WordPad. Why is it that Windows doesn't give you a warning, complaining about file being open in another program?

However, if the same file is open in Word, and you try to moved the source file to the recycle bin or delete it permanently, Windows does give you a warning. The message itself can be a warning type of message, telling you that some (often unknwon) program has the file open. In case of Word 2013 on Windows 8.1 this is an info message, telling you exactly what program (Word) has the file open.

notepad del file notepad open file

Why is that? Why is it that you can A) delete the source file while it's open in a program, and B) continue working on the document after the file has been deleted? Why is the behavior different in Notepad and WordPad from that of Word?

word del file word open file

The first case behavior exists in Paint too. You can delete a bitmap file while it's open in Paint and continue working. But when you try to save it you get prompted for file location to save it to. With Notepad, it directly saves it to the same location that the original file was stored in and gives it the same name. Notepad doesn't prompt you for that.

I have not tested this on Windows 7, Vista, or XP. However, I would expect the behavior to be exactly the same, and so the question would probably apply to those systems as well.

Samir

Posted 2014-12-16T19:19:50.850

Reputation: 17 919

2It's all about whether the program holds the file open while you edit it or not. Often they don't; they read a copy into memory and drop the file connection while you edit it. When you go to save it, it creates a new connection to the file handle. This is up to the program, not Windows. – Ƭᴇcʜιᴇ007 – 2014-12-16T19:27:11.503

So in this case, Word holds the original file open; Notepad does not? But Word makes a hidden copy of the original file when you open it. As some kind of temporary file. Isn't that the file Word is saving changes to? And then on exit, it merges the changes with the original file and discards the temporary file? If so, it should be possible to delete the original file while open in Word. – Samir – 2014-12-16T19:42:22.197

It's because Word is trying to track changes while you (or anyone) are editing it, and if two people made changes, and then it merged each user files back in, only the changes made by the last editor to save would stick. Notepad on the other hand (for example) doesn't care. :) – Ƭᴇcʜιᴇ007 – 2014-12-16T19:45:36.033

I'm rather surprised that notepad allows you to delete or move the file while it is open, but the application controls that, and in notepads case, its such a simple program that it doesn't attempt any concurrency control, and opens the file in read mode, and then loads it memory, releasing the read lock. For more info on whats going on with other files, see this: http://superuser.com/questions/848782/moving-files-from-one-location-to-another-while-they-are-being-used/848798#848798

– Frank Thomas – 2014-12-16T19:52:40.650

So Notepad applies a read lock, and then releases it right after it has been loaded to primary memory? So this will allow for so called "interceding update"? While Word applies a write lock and releases it only after the file has been closed? @FrankThomas Thanks for the link! Is it true then that Word 2013 uses "transacted" temporary files? In other words, it actually works on the temporary file it makes, and when changes are saved it deletes the original file, and saves the current state of the document in a file with the same name as the original? – Samir – 2014-12-16T20:58:40.147

In simple terms, programs like Notepad and Paint don't care about who makes changes to the file and when? No concurrency and no synchronization control? – Samir – 2014-12-16T21:00:25.760

exactly. notepad and paint are not entirely safe for multi-user/multi-process access by processes that might write to them, because they preseent a race condition (who saves first?). Yes, Word and other office documents make temporary files (there are a few kinds, for a few purposes). I believe that the exact process involves preventing saving of changes if the source file has changed while the file was opened, but yes, it can come down to a race condition to see who saves first. The Office behaviour is complicated, so I recommend studying them in particular if you have need. – Frank Thomas – 2014-12-16T21:06:57.937

I thank you for the explanation! Now, would you please try to jot down a few sentences that summarize this? The previously provided answer was not entirely accurate. Try to contrast the behavior of Notepad to that of Word, in short, simple terms. I was mostly curious why Notepad allowed me to move the source file to recycle bin or even delete it, while it is open. I think I got the gist of it now. – Samir – 2014-12-16T21:15:52.000

Answers

5

Not all programs lock files when opened for editing

  • Windows has two types of file locks, shared locks and exclusive locks. Shared locks allow other programs to read the file but deny write access to other programs. Exclusive locks prevent other programs from accessing the file altogether. A program may use either type of lock as required.

  • Programs can "open" files for editing but not lock them. What really happens here is that the file is loaded into a buffer in memory and the status of the file is not checked until it is operated on again. A file "open" in this fashion is not really in use and can be manipulated freely by other programs or the system itself, which allows you to continue editing the non-existent file and save it into a new file.

  • Notepad does not lock the file opened, which means that the file can be deleted while it is open in Notepad and the program will not notice that the file is deleted until it tries to open it again. Similarly, Paint doesn't lock the file and only notices that the file is deleted when it tries to open or save it.

  • Word, on the other hand, locks documents when it opens them (edit: from my research, it seems the lock is exclusive only when Word is reading from or writing to the file, and is shared otherwise). As a result, you cannot delete files that are currently open in Word.

Opening a file without locking it can be risky

  • Although it is simple to just load the file into memory and not touch it until necessary, doing so without locking it carries the risk of a race condition where one program may edit the file without the changes being reflected in another problem. That other program (which might very well be Notepad or Paint) will not see the changes, and they will be lost when you save the file in that program.

  • This risk of race conditions is the reason Microsoft Word (and other Office programs) lock files when they open them. On the other hand, as an example of an alternative solution, Notepad++ doesn't lock opened files so that other programs can continue to manipulate them. However, it will warn the user if it detects that the file has changed or is deleted, asking the user whether to reload the file from disk or close it, respectively—both of these actions would discard the user's changes.

bwDraco

Posted 2014-12-16T19:19:50.850

Reputation: 41 701

Notepad actually uses memory-mapped file to read a file – phuclv – 2018-09-24T09:41:56.983

0

It's because some programs make a copy of the file and open that file instead of the original one. That way, you can delete the original file without an error, since it's not being used by any program.

Behdad

Posted 2014-12-16T19:19:50.850

Reputation: 271

Yes, so I noticed. Word makes a temporary copy of the original file when you open it. Is that what you mean? But Word doesn't allow me to delete the original file, even when it has created a temporary copy. – Samir – 2014-12-16T19:45:22.650

I didn't say anything about any particular app. If you mean the hidden file Word creates when opening files, I guess that's for auto-backup purposes. It actually opens the original file, since as you mentioned, you can't delete the file when it's open in Word. – Behdad – 2014-12-16T19:50:18.267