Solved

File I/O

Posted on 2000-03-23
11
811 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
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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now