cancel
Showing results for 
Search instead for 
Did you mean: 

is there a method to detect whether a file is in use?

jlui
Occasional Contributor

is there a method to detect whether a file is in use?

I open use the files comparison method to see if two files matches.  However, sometimes one of the files are in use by my testing application, and I get thrown an error by testcomplete?  I have a workaround which i just introduce an arbitrary delay, but i would like to know if there is a way to detect whether a file is in use or not.



Thanks,

Justin
2 REPLIES 2
tristaanogre
Community Hero

RE: is there a method to detect whether a file is in use?

I don't know of a particular method to determine if a file is in use or not.  However, what you could do is try and open the file and trap an exception using aqFile.OpenTextFile such as the following DelphiScript code.



function FileInUse(FilePath): Boolean;



    {

    If the file is in use, an exception is raised resulting in the function returning

    true.  If not, the file is attempted to be closed and the result of the function

    is set to the opposite of whether or not the file was able to be closed

    }



    var

        MyFileObject;

        

    begin

    try    

        MyFileObject := aqFile.OpenTextFile(FilePath, aqFile.faReadWrite, aqFile.ctAnsi, FALSE);

        Result := not MyFileObject.Close;

    except

        Result := TRUE;

        end;

    end;



Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
jlui
Occasional Contributor

RE: is there a method to detect whether a file is in use?

Would I be able to get this in VBScript?



Thanks, Justin
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors