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
Solved

File I/O

Posted on 2000-03-23
11
821 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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