Solved

File I/O

Posted on 2000-03-23
11
823 Views
Last Modified: 2008-02-20
I have a NT service which scans a particular folder every 15 seconds for any files in that folder. The files in this folder are being moved from another folder by another service. Here's the sequence of events that happens:

1. Every 15 seconds, a function gets called which :

- Makes a call to findfirst().
- For the file found (excluding subdirectories) a row is inserted into the database.
- The file is moved to a different folder.
- An email is sent out.
- Call findnext to work on the next file.

Randomly, the function works on the same file twice. What seems to be happening is that findfirst() or findnext() finds the file in the folder while it is still being moved into the folder or being moved out of the folder. Since windows has still not completed the move, findfirst()/findnext() combo finds the file again and processes it again. I use the MoveFileEx function to move the file with the MOVEFILE_WRITE_THROUGH flag which should guarantee that the function does not return till the file is moved. I have also tried to call CreateFile() to get an exclusive lock on the file before starting any processing on the file.

Somehow, randomly, I still get a file which is processed twice. Any ideas/suggestions?

Thanks & Cheers
0
Comment
Question by:kkokal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 2651360
Use the following technique instead of checking it periodically.

Monitoring Changes in a Directory or Directory Tree
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/psdk/winbase/filesio_1545.htm
0
 
LVL 5

Expert Comment

by:pitonyak
ID: 2651506

If you have to, maintain a list of files (with time stamps) that you processed last time. If you see them again, skip them and add them to the list of things that you did "last time".
0
 

Expert Comment

by:sunsetyang
ID: 2651675
You can first use the findfirst and findnext to build a filelist,then to do the moving operation.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kkokal
ID: 2651743
Thank you all for your input. Chensu, I like your solution, but its a rehaul of the program. Maybe, when I have the time, I'll get it done.

I don't think the problem is with findfirst()..findnext(). I think that either CreateFile is not returning me an exclusive handle to the file or MoveFileEx sometimes doesnot complete the move before returning. My inclination is on the 2nd one.

Does anyone know what is the series of steps that the NT OS does while moving files from one folder to another? ie does the OS have exclusive access of the file while moving or is it shared? also windows creates a file in the destination folder and then appends to the file till the file is moved.

Thanks

Cheers
0
 

Author Comment

by:kkokal
ID: 2651786
Thank you all for your input. Chensu, I like your solution, but its a rehaul of the program. Maybe, when I have the time, I'll get it done.

I don't think the problem is with findfirst()..findnext(). I think that either CreateFile is not returning me an exclusive handle to the file or MoveFileEx sometimes doesnot complete the move before returning. My inclination is on the 2nd one.

Does anyone know what is the series of steps that the NT OS does while moving files from one folder to another? ie does the OS have exclusive access of the file while moving or is it shared? also windows creates a file in the destination folder and then appends to the file till the file is moved.

Thanks

Cheers
0
 

Expert Comment

by:maxd128
ID: 2651990
I think that while system copyes new file to your folder it locks it, so you couldn't move it.
Try something like
if (MoveFile(name, newlocation))
{
// adding to database and so on
}
0
 

Author Comment

by:kkokal
ID: 2654172
Adjusted points from 100 to 200
0
 

Author Comment

by:kkokal
ID: 2654173
Hi,

I don't think that reorganising the steps in the function will solve the problem. What I have also found is that MoveFileEx returns error # 32 [ERROR_SHARING_VIOLATION] which means the system has locked the file and hence MoveFileEx cannot move it. Since I used CreateFile() to give me an exclusive access to the file initially, the system is doing something with the file.

Any ideas. I am also raising the points to 200.

Cheers & Thanx.

Kumud
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2657812
Hi why can not try changing it al little like this.

- Makes a call to findfirst().
- For the file found (excluding subdirectories) a row is inserted into the database.
- The file is moved to a different folder.
- An email is sent out.

//- Call findnext to work on the next file.
Call findclose on the existing handle.
Call findfirst again ( same pattern).

As you have modified the folder on which findfirst() has returned you a handle,
It does not seem logical to carry on with same handle.
0
 

Expert Comment

by:simmvar
ID: 2734225
I u stand u r problem before even u come out the function the next instance of the service i.e it takes atlease 15 secs to move a file and that is where u overlap.  the only way to avoid this is to synchronize u r calls.  have some external mechanism something like a file created to maintain the status of u r function.  Until one of u r functions or service is completed make sure that no other instance of u r program will access any files i.e it checks for a particular temp file in a directory if it exists it exits immediately it enters the process of moving etc etc only when that file is not existing and this file is created if it doesnt exist and make sure that it is a global variable to u r appln and hence locked until u exit the appln.  I think this should solve u r problem

simmvar
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 200 total points
ID: 2751144
May be problem in 15 sec timer(as was in my apps and I sure in you case too). Now getting WM_TIMER with my ID  I make next:
1.Kill timer
2. Make my actions
3. Start timer (with same ID)

This quarantieres, that my action
NOT will be maked 2-nd time, during
1-st not ended

Alex
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question