Forum Discussion

jlui's avatar
jlui
Occasional Contributor
15 years ago

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

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    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;


  • jlui's avatar
    jlui
    Occasional Contributor
    Would I be able to get this in VBScript?



    Thanks, Justin