From: Rick Raisley on
I have a little routine during closing one of my programs that is intended
to delete all temporary files copied to a particular directory during
operation, cleaning up after itself. It works fine, unless someone has one
of the files open, which creates an error. That's fine, and expected, but
what isn't as expected is that I can't just use Resume Next to continue?

Originally, I had this:

On Error Resume Next
a$ = Dir$(Drive$ & "*.tif")
Do While a$ > ""
Kill Drive$ & a$
a$ = Dir$
Loop

Then realized that it would loop indefinitely if a file was open (actually,
it doesn't, it errors out too). Anyhow, so changed it to simply:

On Error Resume Next
Kill Drive$ & "*.tif"

I get a Run-time error '70', Permission denied. The error is expected, but
why won't it Resume Next?

Can anyone propose a neater solution, to delete all not-open TIF images, and
delete all not-open PDF files except one with a specific name?

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net


From: Jeff Johnson on
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:eKmZlkF4IHA.784(a)TK2MSFTNGP04.phx.gbl...

> Then realized that it would loop indefinitely if a file was open
> (actually, it doesn't, it errors out too). Anyhow, so changed it to
> simply:
>
> On Error Resume Next
> Kill Drive$ & "*.tif"
>
> I get a Run-time error '70', Permission denied. The error is expected, but
> why won't it Resume Next?

I have to ask: from a compiled EXE or running in the IDE?


From: Rick Rothstein (MVP - VB) on
You could always count how many files there are...

TotalFiles = TotalFiles + 1
A = Dir$("c:\temp\" & "*.tif")
Do While Len(A)
TotalFiles = TotalFiles + 1
A = Dir$
Loop

and then use a For-Next from 1 to TotalFiles in place of your Do-Loop to
process the files only one time each.

Rick


"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:eKmZlkF4IHA.784(a)TK2MSFTNGP04.phx.gbl...
>I have a little routine during closing one of my programs that is intended
>to delete all temporary files copied to a particular directory during
>operation, cleaning up after itself. It works fine, unless someone has one
>of the files open, which creates an error. That's fine, and expected, but
>what isn't as expected is that I can't just use Resume Next to continue?
>
> Originally, I had this:
>
> On Error Resume Next
> a$ = Dir$(Drive$ & "*.tif")
> Do While a$ > ""
> Kill Drive$ & a$
> a$ = Dir$
> Loop
>
> Then realized that it would loop indefinitely if a file was open
> (actually, it doesn't, it errors out too). Anyhow, so changed it to
> simply:
>
> On Error Resume Next
> Kill Drive$ & "*.tif"
>
> I get a Run-time error '70', Permission denied. The error is expected, but
> why won't it Resume Next?
>
> Can anyone propose a neater solution, to delete all not-open TIF images,
> and delete all not-open PDF files except one with a specific name?
>
> --
> Regards,
>
> Rick Raisley
> heavymetal-A-T-bellsouth-D-O-T-net
>
>

From: Rick Raisley on
"Jeff Johnson" <i.get(a)enough.spam> wrote in message
news:e2$4kyF4IHA.1420(a)TK2MSFTNGP06.phx.gbl...
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
> news:eKmZlkF4IHA.784(a)TK2MSFTNGP04.phx.gbl...
>
>> Then realized that it would loop indefinitely if a file was open
>> (actually, it doesn't, it errors out too). Anyhow, so changed it to
>> simply:
>>
>> On Error Resume Next
>> Kill Drive$ & "*.tif"
>>
>> I get a Run-time error '70', Permission denied. The error is expected,
>> but why won't it Resume Next?
>
> I have to ask: from a compiled EXE or running in the IDE?

From the IDE. It errs and Resumes or GoTos as expected elsewhere within the
IDE, though.

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net


From: Rick Raisley on
"Rick Rothstein (MVP - VB)" <rick.newsNO.SPAM(a)NO.SPAMverizon.net> wrote in
message news:u27SayF4IHA.2332(a)TK2MSFTNGP03.phx.gbl...
> You could always count how many files there are...
>
> TotalFiles = TotalFiles + 1
> A = Dir$("c:\temp\" & "*.tif")
> Do While Len(A)
> TotalFiles = TotalFiles + 1
> A = Dir$
> Loop
>
> and then use a For-Next from 1 to TotalFiles in place of your Do-Loop to
> process the files only one time each.
>
> Rick
>

I considered this, and felt it was overkill, much as alluded to by Bob
O'Bob. I've solved my problem using the following function to check if the
file is available (to edit, rename, delete, whatever):

Public Function FileIsAvailable(ByVal Filename As String) As Integer
Dim fnum As String

On Error GoTo AnError
fnum = FreeFile
Open Filename For Input Lock Read Write As fnum
Close fnum
FileIsAvailable = True
Exit Function

AnError:
Select Case Err.Number
Case 0: FileIsAvailable = True 'File exists, and is available to write
or replace, won't get here, actually
Case 70: FileIsAvailable = 1 'File exists, but is open and locked
Case Else: FileIsAvailable = False 'File does not exist
End Select

End Function

I then changed my deleting code to the following:

On Error Resume Next '(Left it in even though it doesn't seem to do
anything for me)
a$ = Dir$(Drive$ & "*.tif")
Do While a$ > ""
If FileIsAvailable(Drive$ & a$) = True Then Kill Drive$ & a$
a$ = Dir$
Loop

Despite the On Error line, no errors will normally occur here, but will be
caught by the FileIs Available function. Surprisingly, and good for me, the
Do loop does NOT continue while there are TIF files available; it only comes
up with each name once, and that file is either deleted or skipped, so it
works fine that way, and was not the problem I thought it might be.

I am still at a loss why Resume Next would not skip over the Kill statement,
however. If it would, it would be simplest as originally written:

On Error Resume Next
a$ = Dir$(Drive$ & "*.tif")
Do While a$ > ""
Kill Drive$ & a$
a$ = Dir$
Loop

I haven't checked it when compiled, only in the IDE. But most errors Resume
and are Trappable, so don't know why this one would be different.

Thanks for everyones' help. ;-)

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net


>
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
> news:eKmZlkF4IHA.784(a)TK2MSFTNGP04.phx.gbl...
>>I have a little routine during closing one of my programs that is intended
>>to delete all temporary files copied to a particular directory during
>>operation, cleaning up after itself. It works fine, unless someone has one
>>of the files open, which creates an error. That's fine, and expected, but
>>what isn't as expected is that I can't just use Resume Next to continue?
>>
>> Originally, I had this:
>>
>> On Error Resume Next
>> a$ = Dir$(Drive$ & "*.tif")
>> Do While a$ > ""
>> Kill Drive$ & a$
>> a$ = Dir$
>> Loop
>>
>> Then realized that it would loop indefinitely if a file was open
>> (actually, it doesn't, it errors out too). Anyhow, so changed it to
>> simply:
>>
>> On Error Resume Next
>> Kill Drive$ & "*.tif"
>>
>> I get a Run-time error '70', Permission denied. The error is expected,
>> but why won't it Resume Next?
>>
>> Can anyone propose a neater solution, to delete all not-open TIF images,
>> and delete all not-open PDF files except one with a specific name?
>>
>> --
>> Regards,
>>
>> Rick Raisley
>> heavymetal-A-T-bellsouth-D-O-T-net
>>
>>
>