Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine quickly if a file can be deleted

Status
Not open for further replies.

HughLerwill

Programmer
Nov 22, 2004
1,818
GB
Dear all,

I have a series of files which I would like to delete if they are not in use (Open). I can do this by trying to delete (Kill) them and catching the error, but this is not very quick. Is there a faster way, a Windows API perhaps?
Would prefer to avoid the fso if possible but give me your best.

TIA

regards Hugh,
 
Not sure if this will speed things up or not.

Code:
Option Explicit

Private Sub Command1_Click()
    Const strFileToOpen As String = "C:\Test.txt"
    If IsFileOpen(strFileToOpen) Then
        MsgBox strFileToOpen & " is already Open. File in Use"
    Else
        MsgBox strFileToOpen & " is not open"
        Kill strFileToOpen
    End If
End Sub

Function IsFileOpen(strFullPathFileName As String) As Boolean
    Dim hdlFile As Long
    On Error GoTo FileIsOpen:
    hdlFile = FreeFile
    Open strFullPathFileName For Random Lock Read Write As hdlFile
    IsFileOpen = False
    Close hdlFile
    Exit Function
FileIsOpen:
    IsFileOpen = True
    Close hdlFile
End Function

Swi
 
Swi,

Thanks for coming back. I have done something similar;

'check menus for data folders which are off LAN/ invalid paths or have open dat files in them
'favorite data folder names are in MenuRecent
Dim mnu As Menu, a$, f

For Each mnu In MenuRecent
With mnu
If Gpini.TickBusy Then 'option to check offending folders is set
.Enabled = Not .Enabled

If ValidDir(.Caption) Then
a$ = Dir$(.Caption & "\*.dat")
On Error Resume Next
Do While Len(a$)
f = FreeFile
Open .Caption & "\" & a$ For Random Access Write Lock Write As f
If Err Then Exit Do 'open failed probably because a .dat file is already open, or unopenable for some other reason
Close f
a$ = Dir$()
Loop
.Checked = (Err > 0)
On Error GoTo 0
Else
'the folder is invalid or off line
.Checked = True
End If

.Enabled = Not .Enabled
Else
.Enabled = Not .Enabled
.Checked = False
.Enabled = Not .Enabled
End If
End With
Next

This all goes on in a File menu click which drops to show MenuRecent so you can appreciate why I need the speed.

regards Hugh
 
Swi,

It would help if there was some way to determine if a folder contained any open files in a single shot rather than having to go through the files in the folders one by one. I guess I'm hard up against disk/ LAN access speed here though and that is the main limiting factor.

regards Hugh,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top