Re: How do I check if a file is in use?




Mark wrote:
Hi all,

This is something which has been bugging me for ages. How can I check
if a file is already in use by a different program?


There's a very basic problem here-- nobody can predict the future.

Even if there was a standard C function named FileIsFree( char * Path
), there's no way it, or any other function, could ensure that file
would still exist, be free, or contain the same data, even one CPU
clock cycle later.

So any attempt to do this is bound to fail, some of the time. I assume
that's not good enough for a production program.

What you need to do is find some operation that is "atomic", i.e.
indivisible, irrevocable, and can only happen to a unbusy file.

For example, change the program or batch file that is writing the file
to write the file to /tmp/foo.bar. When it's done, have it close the
file, then rename it to the proper destination file name. On every OS
I can forsee, "rename" is an atomic operation, i.e. you'll never see
the file in both places, or no place (well, maybe this) at any one
time.

If you can't change the source program, maybe you can wrap it in a
batch file that does the renaming.

-----

Another possibility, very non-portable, but in Windows you can make API
requests that cause a function in your program to be called every time
a file or directory changes. That's a very good low-overhead way to
do what you need, but specific to Windows.

I don't think the Unix flavors have this kind of hook but of course it
may be a recent addition or a Linux kernel addon.

.



Relevant Pages

  • Re: simple question
    ... > I'm trying to write a batch file to rename a bunch of files, ... > windows xp to transfer files from a camera to the computer and put the ... Why do you need a batch file when there are ... before some DOS batch solution. ...
    (comp.os.msdos.programmer)
  • Rename Lable of Mapped Drive
    ... I am using net use to mapped shared drives, but I want to be able to rename ... the labels giving to them using a batch file or vbs file. ... I am using Windows 2000 and Windows XP Pro editions. ...
    (microsoft.public.scripting.vbscript)
  • Re: system(), renaming files and long filenames
    ... Assuming that you are running under Windows or some version of DOS ... batch file one line at a time and finally call systemjust once to ... rename all the files. ...
    (comp.lang.c)
  • Re: How to copy several txt files into one huge file?
    ... If they all have the same name, make a batch file ... rename name.txx name.txt ... all have the same format, but I don't know how to do this in windows? ...
    (microsoft.public.excel.misc)
  • Re: Error Message when trying to open Word 2000
    ... "John B." ... - I am using Windows ME so I do not have Compatibility ... :>However if you want to rename the old key back then I ... :>add-ins, Normal.dot, Registry, etc. ...
    (microsoft.public.word.application.errors)