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

deleting files with vbs 1

Status
Not open for further replies.

Fletch219839

IS-IT--Management
Jun 24, 2012
3
US
I am using the following code to delete files.

Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("Q:\Company\Forms\*.PTR"), DeleteReadOnly

The code works great as long as their is a PTR file in the folder to delete.
If their is no PTR file in the folder the script generates an error.
Can someone tell me how to make the script stop giving errors if the file is already deleted.
 
Like this:
Code:
Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists("Q:\Company\Forms\*.PTR") Then
   objFSO.DeleteFile "Q:\Company\Forms\*.PTR", DeleteReadOnly
End If
 
Guitarzan,
The code is not deleting the files.
Can you figure out why.
I have to remove your line to delete the file.
Thanks in advance.
 
make the script stop giving errors if the file is already deleted
Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.DeleteFile("Q:\Company\Forms\*.PTR"), DeleteReadOnly
On Error GoTo 0

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Fletch219839:
My bad, I forgot that FileExists does not work with wildcards. PHV's correction is the simplest way to do what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top