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!

Delete PDF'S in a directory 1

Status
Not open for further replies.

LFC8

Programmer
Nov 18, 2004
45
0
0
GB
Hi

I'm looking to delete PDF's in a directory, I'm using a FileListBox which displays all the files.

Dim iCnt As Integer

For iCnt = 0 To Me.File1.ListCount - 1
If Me.File1.Selected(iCnt) Then
Kill Me.File1.FileName
MsgBox Me.File1.List(iCnt)
End If
Next

Can anyone see where i'm going wrong, it displays a Run Time error 53 File not found

Thanks in advance
 
I suspect you are trying to Kill a file without specifying the folder in which the file resides. I could be wrong though. I suggest you comment the 'kill' line and post the first MSGBOX output.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
When i have selected the 2 files to be deleted from the folder it deletes the first one fine but then tries to delete it again hence why i'm getting file not found.
 
here is what i have so far!!!

Private Sub Form_Load()
File1.Path = "F:\TEST\C2C\"
End Sub

Private Sub cmdDelete_Click()

MsgBox "Are you sure you wish to DELETE these file(s)!!", vbYesNo, "Deletion"

Dim iCnt As Integer
Dim tmpPdf As String

For iCnt = 0 To File1.ListCount - 1
If File1.Selected(iCnt) Then
tmpPdf = File1.FileName
Kill File1.Path & "\" & tmpPdf
End If
Next

End Sub

Private Sub cmdExit_Click()
Unload Me
End
End Sub

Thanks
 
The problem as I see it is the File1 list is refreshing itself as you delete the file from the drive.

I would suggest that you store the filenames in an array, then step thru the array to kill the files. That way as the File1 updates it will not effect your array data.
 
Example of what I am thinking is below. I have not tested it as I am not at a computer that has VB installed.
(there may be errors in my code)


Dim iCnt As Integer
Dim tmpPdf As String
Dim AList() as string

For iCnt = 0 To File1.ListCount - 1
If File1.Selected(iCnt) Then
Redim Preserve AList(ubound(AList)+1)
AList(Ubound(AList))= File1.FileName
End If
Next

For iCnt = 0 To Ubound(AList)
Kill File1.Path & "\" & Ubound(AList)
Next
 
Thanks for posting up some code

I'm getting Subscript out of range on the Redim line??

 
Or... Run through the list backwards.

Code:
Dim iCnt As Integer

[!]For iCnt = (Me.File1.ListCount - 1) To 0 Step -1[/!]
   If Me.File1.Selected(iCnt) Then
        Kill Me.File1.FileName
        MsgBox Me.File1.List(iCnt)
   End If
Next

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I think I see my error. Try this

Dim iCnt As Integer
Dim tmpPdf As String
ReDim AList(0) As String
Stop
For iCnt = 0 To File1.ListCount - 1
If File1.Selected(iCnt) Then
ReDim Preserve AList(UBound(AList) + 1)
AList(UBound(AList)) = File1.FileName
End If
Next

For iCnt = 1 To UBound(AList)
Kill File1.Path & "\" & AList(iCnt)
Next
 
Hi waytech2003

Thanks again for the help, however i've step through the code and its only picking up the first file even though i've selected 4 files from the directory!

any ideas??

 
Try this
Code:
    Do Until File1.ListCount = 0
        Kill File1.Path & "\" & File1.List(0)
        File1.Refresh
    Loop
 
LFC8, if you are still working on this, I tested the code below and it seems to work...

Dim iCnt As Integer
Dim tmpPdf As String
ReDim AList(0) As String

For iCnt = 0 To File1.ListCount - 1
If File1.Selected(iCnt) Then
ReDim Preserve AList(UBound(AList) + 1)
AList(UBound(AList)) = File1.List(iCnt)
End If
Next

For iCnt = 1 To UBound(AList)
Kill File1.Path & "\" & AList(iCnt)
Next
 
Thanks waytech2003

That worked a dream thanks for all your help

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top