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

VB6 detele files in a directory ? 2

Status
Not open for further replies.

tdong

Programmer
Mar 12, 2004
112
CA
Hi guys

I create a few pdf from CR and put them in C:\email\ now after sending it I need to delete all file with .pdf or all file in the C:\email\. thanks
 
look up the KILL command.

Ex.
Kill C:\Email\File.ext



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I need to delete zip files after they have been unzipped. I've tried the Kill command but it doesn't delete the files and no error message either. I have a reference to the MS Scripting Object. Am I missing something else?

Thank you.
 
That is probably happening because there is still an open file handle to it somewhere.

Make sure that the file is closed before attempting to delete it.

If you are already referencing the File System Object, then you can delete files as shown below.

Code:
Public Sub DeleteFile(ByVal FileName As String)
    
    Dim FSO As Scripting.FileSystemObject
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(FileName) Then
        Call FSO.DeleteFile(FileName, True)
    End If
    
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
tdong, you can also use wildcards with Kill. To delete all .pdf files you can use:
[tt]Kill "C:\email\*.pdf"[/tt]

George, using FSO, you did not kill the file, you overkilled it.;-)
 
Hypetia said:
George, using FSO, you did not kill the file, you overkilled it.

True...

I would have used:
Code:
Public Function DeleteFile(ByVal FileName As String) As Boolean
  If Len(Dir(FileName)) Then
    Kill FileName
  End If
  DeleteFile = (Dir(FileName) = "")
End Sub

It will also return False if the was NOT deleted.

;-)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top