-
Can anyone tell me the correct syntax for this piece of code, I want to 'KILL' a file if it exists:
If Command() + "nng_list.DOC" = True Then
Kill Command() + "nng_list.DOC"
End If
At the moment, the compiler reads the first line, then passes straight to the 'End If', even if the file in question exists. [The Command() funtion holds the directory]
Cheers 'n' Beers
Skeen
-
May be you forgot you "\" between command() and nng_list.DOC
Code:
If Command() + "\nng_list.DOC" = True Then
Kill Command() + "\nng_list.DOC"
End If
You can use dir("yourpath\yourfile.doc")
It return "" if not exist else it return the path.
-
Still not workin'
Thanx 4 the reply,
It gives me a type mismatch error if I use:
dir("yourpath\yourfile.doc")
Cheers though
-
Here is a litle function to see if a file exist or not.
Code:
Public Function Existfile(ByVal sFile As String) As Boolean
Dim sResults As String
On Error GoTo Err_ExistFile
sResults = Dir$(sFile)
If sResults = Empty Then
Existfile = False
Else
Existfile = True
End If
Exit Function
Err_ExistFile:
Existfile = False
End Function
And you can use it like this.
Code:
If Existfile("C:\nng_list.DOC") = True Then
Kill "C:\nng_list.DOC"
End If
-
Sorry for the Dir,it's because it return a value
isThere=dir("....")
-
Cheers Chaps
Nice 1. I was trying the old 'Open-Close-KILL' gag. Wasn't up ta much either.
Thax again
Skeen
-
Here's the function I prefer:
Code:
' Check if file exists without disturbing Dir() function
' If file does not exist, but folder with same name does, return False
' If drive not ready, file exists and is corrupted, or network unavailable, return false
Public Function FileExists(pstrFile As String) As Boolean
On Error GoTo FileExistsErr
FileExists = True
If GetAttr(pstrFile) And vbDirectory Then FileExists = False
FileExistsExit:
Exit Function
FileExistsErr:
Select Case Err.Number
Case 52, 53, 75
FileExists = False
Case Else
MsgBox Err.Description, vbInformation, "Notice #" & Err.Number
End Select
Resume FileExistsExit
End Function
-
And to delete a file, I use:
Code:
Public Function DeleteFile(pstrFile As String)
On Error GoTo DeleteFileErr
If InStr(pstrFile, "*") = 0 And InStr(pstrFile, "?") = 0 Then SetAttr pstrFile, vbNormal
Kill pstrFile
DeleteFileExit:
Exit Function
DeleteFileErr:
MsgBox Err.Description, vbInformation, "Notice"
Resume DeleteFileExit
End Function
Also, while I'm at it, these are good too:
Code:
Public Function FolderExists(pstrFolder As String) As Boolean
On Error GoTo FolderExistsErr
If GetAttr(pstrFolder) And vbDirectory Then FolderExists = True
FolderExistsExit:
Exit Function
FolderExistsErr:
If Err.Number = 53 Then
FolderExists = False
Else
MsgBox Err.Description, vbInformation, "Notice"
End If
Resume FolderExistsExit
End Function
Public Function IsLocked(pstrFile As String) As Boolean
On Error GoTo IsLockedErr
IsLocked = False
Open pstrFile For Binary As 1
Close 1
IsLockedExit:
Exit Function
IsLockedErr:
Select Case Err.Number
Case 70 ' Access Denied error
IsLocked = True
Case 75 ' Skip error if it's a folder name - folders can't be opened
Case Else
MsgBox Err.Description, vbInformation, "Error #" & Err.Number
End Select
Resume IsLockedExit
End Function