How to check for copy file completion

B

booksnore

Riddle me this... I have an app which listens to a windows folder for
files to appear and then processes the file based on the file format. My
question is if a really big file appears in the folder (say over 20 G)
then it takes more than 20 minutes for the file to be copied there. As
my listener job looks in the folder every 30 seconds I don't want to
process the file until the whole file is there - if you get what I mean.
The only way I think I have found to ensure that a file is a whole file
and not in the process of loading into the folder is to compare the
fileInfo LastAccessTime to the fileInfo CreationTime (below). I'm not
sure if this is really the correct way to go. Any one else come across
this problem or any ideas?

Hashtable fileList = new Hashtable();
DirectoryInfo di = new DirectoryInfo(dir);
FileInfo[] rgFiles = di.GetFiles(type);

foreach(FileInfo fi in rgFiles)
{
if (fi.LastAccessTime != fi.CreationTime)
{
string key = fi.Name.ToUpper() +
fi.Length.ToString().ToUpper()+fi.CreationTime.ToString("yyyyMMdd").ToUp
per();
fileList[key] = fi;
}
}
 
W

Winista

While the file is being written to disk, there would be a lock on it by the
writer. So you would not be able to access it and you will get IOException
thrown at you. You can check for this exception.
 
W

William Stacey [MVP]

I think maybe you need to make FileShare.None, FileMode.Write for this to
work as expected. But that would seem to be a good way to go. Another way
used in the past is to download to a temp file, then when done, rename the
file and the poll loop will then "see" the file.

--
William Stacey [MVP]

| While the file is being written to disk, there would be a lock on it by
the
| writer. So you would not be able to access it and you will get IOException
| thrown at you. You can check for this exception.
|
| | > Riddle me this... I have an app which listens to a windows folder for
| > files to appear and then processes the file based on the file format. My
| > question is if a really big file appears in the folder (say over 20 G)
| > then it takes more than 20 minutes for the file to be copied there. As
| > my listener job looks in the folder every 30 seconds I don't want to
| > process the file until the whole file is there - if you get what I mean.
| > The only way I think I have found to ensure that a file is a whole file
| > and not in the process of loading into the folder is to compare the
| > fileInfo LastAccessTime to the fileInfo CreationTime (below). I'm not
| > sure if this is really the correct way to go. Any one else come across
| > this problem or any ideas?
| >
| > Hashtable fileList = new Hashtable();
| > DirectoryInfo di = new DirectoryInfo(dir);
| > FileInfo[] rgFiles = di.GetFiles(type);
| >
| > foreach(FileInfo fi in rgFiles)
| > {
| > if (fi.LastAccessTime != fi.CreationTime)
| > {
| > string key = fi.Name.ToUpper() +
| > fi.Length.ToString().ToUpper()+fi.CreationTime.ToString("yyyyMMdd").ToUp
| > per();
| > fileList[key] = fi;
| > }
| > }
| >
| >
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top