File gets deleted while it is being written to on Windows!

Asked By 270 points N/A Posted on -
qa-featured

We are presently writing an application in Java. This application scans a predefined set of folders and gets a list of files in each of them. Then the application reads the file as a byte array. Next it calls a Web Service to submit the binary image. Lastly it moves the files to a different location. These set of actions work perfectly. The problem we have is that the java application is picking the file when in reality, the file is not complete as yet. How do we prevent this?

SHARE
Best Answer by Ingrid
Answered By 0 points N/A #101615

File gets deleted while it is being written to on Windows!

qa-featured

It is practically impossible to delete a file in Windows, while it is in use! For example you will get the following error message when you attempt to delete a file while it is in a write mode! Microsoft Windows Operating system keeps track of all opened files. Each file can have a setting stating that it is either opened for reading or it is opened for writing. Each process that requires access to the file attempts to open the file in read-only mode. If a process requires to delete the file or write to the file, it needs to gain exclusive access to the file.

The Microsoft Windows operating system checks if the file is in use by any other process, prior to allowing a process to be deleted, renamed or move the file. If the process that is requesting, is the same process that currently has a lock on the file, it is allows to do so. Therefore, it is practically impossible to delete the file when it is in use! Are you sure it is happening on your computer?

Answered By 0 points N/A #101616

File gets deleted while it is being written to on Windows!

qa-featured

You actually cannot delete the file, while it is being written to for applications running on the same machine. It sometimes may happen when the application writes to the file in short bursts without actually locking the entire file. That is the application opens the file, writes and closes it without keeping the file locked for writing until the entire precess is complete.

You also need to know that in event the file is locked for exclusive access then you cannot even open it for reading. I think the application that is writing the file is not locking the entire file. Instead it may only be locking it for wiring for a shot period and then releasing it. What you can do is to check if the file is fully readable AND writable before attempting to read it or do any modifications. This is because most processes allow read-only access to the file, even when the file is actually in use.

Answered By 270 points N/A #101617

File gets deleted while it is being written to on Windows!

qa-featured

Do you mean to say that Windows process, still allows access to reading the file even when it is actually in use? I did not think that was even faintly possible! What can I do to ensure that the file is totally written and done with, prior to calling the web-service? The application is a scanner application that writes a PDF document, to the specified folder. If the scanner application does a multi-page scan, it pauses until the user flips the document. It is during this interval that this tragedy happens!

Answered By 0 points N/A #101618

File gets deleted while it is being written to on Windows!

qa-featured

If a process does not create an exclusive access, then it is possible for other applications to open or read a copy of the file in read-only mode. You can test this out by using Microsoft Excel Spread Sheet or a Microsoft Word Document. You open the Microsoft Word or Microsoft Excel document, and then double click on the file again to open it on another window. When you do this, you will get a prompt in Microsoft Word or Microsoft Excel stating that the destination file is in use and do you wish to open a read-only copy. I have attached a screen-shot of the same.

Answered By 270 points N/A #101619

File gets deleted while it is being written to on Windows!

qa-featured

This is horrible! We have been using this process for digitizing customer invoices and documents in our office! We do not have access to the scanner software source code. Therefore I think we can only do some modifications on the Java application to check if the file is fine for picking. What is the best method to choose? Attempt to rename the file? Attempt to write a byte into the file (horrible thought)? Write a Windows based desktop scheduler? What can I do to ensure the file is ok for pickup to be posted via the web service?

Best Answer
Best Answer
Answered By 0 points N/A #101620

File gets deleted while it is being written to on Windows!

qa-featured

Scheduling the pickup interval or batch processing the file is the best option. For example, if you are scanning a multi-page document, I would schedule the pickup every 20 minutes or so. If it is a single page document, I would schedule a pickup every 2 minutes or so. In order to make it operationally feasibly, I would suggest to use "top of the hour"."hand on the 4", "hand on the 8" technique in eructating the users.

That is, they must start and finish the scanning of a multi-page document after or before the hand of the clock reaches the appropriate digit. Further, in your application, read the list of files at that instant to a collection variable and only operate on this files. This will ensure the new files are picked up in the next cycle, allowing the files to complete. You also can use the following code at Java level to check if the file is readable and writable before adding it to the cache.

File currentFile = files[i];
 if (currentFile.exists() && currentFile.canRead()
               && currentFile.canWrite()) {
                varFiles.add(files[i]);

  }

Answered By 270 points N/A #101621

File gets deleted while it is being written to on Windows!

qa-featured

Checking for read and write at file level before picking the file solved the problem. Furthermore, scheduling or botching the scanning also improved accuracy! I was able to modify the Java program and insert the additional check as given by Ingrid. I am surprised that Microsoft Windows allows different processes read-only access to the file even though the actual requirement was to entirely lock the file until the original application is totally done with it! Thank you experts for the valuable feedback and suggestions. It really was a big learning experience for me!

Answered By 0 points N/A #101622

File gets deleted while it is being written to on Windows!

qa-featured

I've had the same problem for days. I'm glad i found this post. Thanks so much Ingrid! This is like a free crash course 🙂

Related Questions