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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kill Files

Status
Not open for further replies.

theCroat

Technical User
Sep 27, 2006
20
HR
Hello! I'm using Kill App.Path + "\tempfsh\*.*" function, however, when there's no files inside it gives me error and application crashes, and if there are some files, it deletes them [as it should] and application continues to work properly.. help needed!
 
Before executing the kill, check whether that path contains any such files or not. If the file count > 0, then only execute it.

Sharing the best from my side...

--Prashant--
 
theCroat,

Try this;

Sub KillIfExist(Filename$)
If Len(Dir$(Filename$)) Then Kill Filename$
End Sub

to delete C:\MyDir\MyFile.ext call it with;

KillIfExist "C:\MyDir\MyFile.ext"

HTH Hugh
 
<If Len(Dir$(Filename$)) Then (...)

Just to be clear here, there's an implicit conversion from an integer to a boolean here. This statement is basically equivalent to

If Len(Dir$(Filename$)) <> 0 Then

HTH

Bob
 
Check this out:

Private Sub Command1_Click()

Dim retval As String

retval = Dir$("c:\vbfiles\b.txt")

If retval = "b.txt" Then

MsgBox "b.txt exists--no need to copy it..."

Else

FileCopy "c:\vbfiles\a.txt", "c:\vbfiles\b.txt"

End If

End Sub
 
I recently had an issue with dir$
Then checking network path
(\\server\share\file.ext)
- it worked on development machine OK (returned vbNullString if no file exists). However then I install on different machine it thow error on this line...
So I had to wrap it up with OnError, like this:
Code:
CPUNoSaveLoadExists = False
On Error Resume Next
    CPUNoSaveLoadExists = (dir$("\\server\share\file.ext") <> vbNullString)
On Error GoTo 0
If CPUNoSaveLoadExists Then
(... rest of code)
 
Other contenders

Sub KillIfExist(Filename$)
On Error Resume Next
Kill Filename$
End Sub

and

Function IsKilled(FileName$) As Long
On Error Resume Next
Kill Filename$
IsKilled = Err
End Sub

regards Hugh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top