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!

Deleting file if it exists 3

Status
Not open for further replies.

DwayneS

Instructor
Mar 29, 2002
70
0
0
Simple coding error. I'm processing a long list of files sent to a directory as work is completed. I simply need to delete a file if it already exists. Here's the code and it doesn't run. The filename calculation is working. I need coding correction. Can someone who understands vb tell me what's wrong?

GetFile = DataPath & FileIn
NewFile = HistDataDir & DateString & FileIn

If FileExists(NewFile) Then
Kill NewFile
End If


Dwayne Streeter, CPA, CITP
 
I think you want to have a look at the FileSystemObject.

Something like this (not tested)

Code:
'add this to your declaration section
dim objFSO as Object

set objFSO = CreateObject("Scripting.FileSystemObject")

if objFSO.FileExists(NewFile) then
VBA.Kill(NewFile)
End If

Hope it helps



Ignorance of certain subjects is a great part of wisdom
 
I don't think there's a FileExists method (it does exist as a method of the FileSystemObject, though), but you could use Dir?

[tt]If Dir(NewFile) <> "" Then
Kill NewFile
End If[/tt]

Roy-Vidar
 
If you are going to use the FileSystemObject (you'll need a reference to Microsoft Scripting Runtime) then you can do it all there.
Code:
With New FileSystemObject
   If .FileExists(SomeFile) Then .DeleteFile(SomeFile)
End With
 
Thanks, guys. I appreciate it. I struggle with vb, but not that much with your help. I'm learning a lot for a bean counter.

Dwayne Streeter, CPA, CITP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top