5

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.

  • 2
    It'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 Dec 16 '14 at 19:27
  • 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 Dec 16 '14 at 19:42
  • 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 Dec 16 '14 at 19:45
  • 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: superuser.com/questions/848782/… – Frank Thomas Dec 16 '14 at 19:52
  • 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 Dec 16 '14 at 20:58
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.

| improve this answer | |
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.

| improve this answer | |
  • 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 Dec 16 '14 at 19:45
  • 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 Dec 16 '14 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.