Jump to content
Sign in to follow this  

Create table of "Files in use"

Recommended Posts



Had a troll through the forum and looked at Siao's post FileInUse, but no answers yet.

I am trying to launch MyApp.exe from an AI script. When MyApp.exe is launched it extracts 12 files to a (predetermined) folder. Once everything (graphics, etc) is extracted, the GUI is created with the extracted components. Once created there is no longer a need for these extra components and I want to delete them.

I tried using a variety of means to determine when these extras can be deleted but have met without success. One factor is that when all is said and done, only 2 of the extracted files remain in use.

I was thinking about running a While..WEnd to determine when ONLY these 2 known files are in use and then deleting all the others.

Is there a way to create a table of "Files in Use" and update it every 100 msecs (for example) to determine if and when only these two files are in use?

Thanks for any suggestions.


Edited by MadDogDean

Share this post

Link to post
Share on other sites

Well you mentioned a fileinuse function and you mentioned a loop.

The answer seems to be there, check them in a loop, if not inuse then delete.

Hey JohnOne,

It's so close I can almost taste it...

Where I am running into troubles (and soon into the wall :) ) is that I need to figure out a way to see when only the two specific files are in use - no more, no less.

I thought to enumerate the files into a table and then compare them using a fileinuse function to see when only the two files match the "in use" criteria. This is still a little above my pay grade and apparently that part of my brain is "in use" and won't let me in...

I little light through the keyhole might help...



Share this post

Link to post
Share on other sites

While 1
    $x = _FileInUse($file)
    $x2 = _FileInUse($file2)
    If $x = 0 And $x2 = 0 Then ExitLoop

; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Parameter(s):     $sFilename = File name
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
Func _FileInUse($sFilename)
    Local $aRet, $hFile
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ
                                    "dword", 0, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0, _ ;lpSecurityAttributes = NULL
                                    "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        Return 1
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return 0

Edited by rogue5099

Share this post

Link to post
Share on other sites


To my rescue again! :)


A little tweaking and it works fine. One thing I did change was the dword for "dwDesiredAccess" as was 0x80000000 for GENERIC_READ, but this didn't respond as expected. (This particular file, although while "in use" could be moved & renamed but not deleted), so I changed the dword to 0x40000000 GENERIC_WRITE - now it responds as I expect it should (for me anyhow)

This sounds noobish, but I've never had to use a while statement like that before. Usually I only using it in a single linear test. This is great.

One final question, when using FileDelete, is there a way to delete everything in a folder "*.*" except for one single file?



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this