|
-
Nov 19th, 2015, 07:24 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] .DeleteFile not working
In a vbs, I have this sub to delete some files depending on the extension:
Code:
Sub DoDelete(TheFolder)
Dim TheFile
Dim FSO2
Set FSO2 = CreateObject("Scripting.FileSystemObject")
TheFile= TheFolder & "\*.xyz"
WScript.echo TheFile
On Error Resume next
FSO2.DeleteFile(TheFile), TRUE
Set FSO2 = Nothing
End Sub
WScript.echo TheFile tells me that TheFile is correct, it is something like this:
Q:\_X15\__KILLME Icons\*.xyz
But the FSO2.DeleteFile(TheFile), TRUE does not do anything.
Ideas?
-
Nov 19th, 2015, 07:40 AM
#2
Re: .DeleteFile not working
What error message do you get when you remove or comment out the evil OERN?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 19th, 2015, 07:47 AM
#3
Re: .DeleteFile not working
remove the on error resume next, maybe it will then tell you some error it found.
further: why are the brackets around TheFile? this should also work: FSO2.DeleteFile TheFile, TRUE
-
Nov 19th, 2015, 07:50 AM
#4
Re: .DeleteFile not working
 Originally Posted by Bonnie West
the evil OERN?
oh c'mon don't overreact
-
Nov 19th, 2015, 07:59 AM
#5
Thread Starter
Fanatic Member
Re: .DeleteFile not working
Ok, I tried now:
Code:
WScript.echo TheFile
FSO2.DeleteFile TheFile, TRUE
WScript.echo TheFile & " is deleted"
No error comes up.
And the second echo doesn't say anything.
This is all what comes out:
Code:
Microsoft (R) Windows Script Host, Version 5.812
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
Q:\_X15\__KILLME Icons\*.xyz
***** script completed - exit code: 0 *****
Remark:
The files in question are accessible and deleteable...
-
Nov 19th, 2015, 08:12 AM
#6
Re: .DeleteFile not working
you probably have another on error somewhere in the code that calls the DoDelete Sub.
the effect is as follows: the calling code turns off error handling, jups into the Sub, the Sub stumbles on an error and immediatly leaves the Sub back to the calling code that Comes after the Sub call.
On error resume next CAN be evil if you don't know what it is doing like in your case, but it is not evil per se.
-
Nov 19th, 2015, 08:26 AM
#7
Thread Starter
Fanatic Member
Re: .DeleteFile not working
Right, there WAS an another On Error Resume Next in the calling code.
I eliminated it.
Now I see why DeleteFile fails.
"File not found."
Knowing this, I re-introduced the On Error resume next before the DeleteFile.
And guess what: Now it exactly works as expected.
Thank you very much - solved.
-
Nov 19th, 2015, 08:33 AM
#8
Re: [RESOLVED] .DeleteFile not working
one more Thing to do: when you turn on on error resume next in a Sub to catch an error there, you should make sure that after the error was cought and before the Sub exits you clear the error either by calling err.clear or on error goto 0.
the following should demonstrate the issue that can occur if you don't do this:
Code:
Sub Main()
On Error Resume Next
a = 1
b = 0
c = b / a
If Err.Number <> 0 Then
MsgBox "a is zero"
End If
c = DoSomething(a, b)
d = b / a
If Err.Number <> 0 Then
MsgBox "a is zero!"
End If
On Error GoTo 0
End Sub
Function DoSomething(a, b)
on error resume next
DoSomething = a / b
'Err.Clear '<-- missing!
End Function
it might not be a good example as i quickly put this together. the Thing is: the calling code is trapping for Errors, the Sub is trapping for Errors but an error raised by the Sub is passed back to the calling code and the calling code could then think the error it finds was caused by itselfs code instead of the Sub's code.
-
Nov 20th, 2015, 03:46 AM
#9
Thread Starter
Fanatic Member
Re: [RESOLVED] .DeleteFile not working
Err.Clear is for sure a good practice.
I added it.
Thank you.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|