Solved

Need VBScript code to Delete Multiple Files at once.

Posted on 2007-04-05
6
652 Views
Last Modified: 2013-11-25
Through VBScript I need to delete multiple files using wildcards as the files that need to be deleted are numbered or called something different dependent upon the type of data contained in the file.  The one common value is the Id.  I can delete a single file no problem - but have not found a way within VBScript to delete files based on a wildcard.  I could have a file, RID123.xml and a file CID123Drill.xml and a file called RID123Drill1.xml, RID123Drill2.xml..  What I don't know is the RID123Drill1 or Drill2 as these # are random - I don't know them.  So what I am hoping to do is DELETE(RID123Drill*.XML) or something similar.
0
Comment
Question by:DougR_MS
  • 5
6 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18861692
Since this is data destructive - please test in a test folder first...

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolder: Set objFolder=objFSO.GetFolder("C:\YourFolder\")
For Each File in objFolder.Files
  If lCase(objFSO.GetExtensionName(file))="xml" Then
    If lCase(Left(file.name,11))="rid123drill" Then objFSO.DeleteFile file
  End If
Next
Set objFolder=Nothing
Set objFSO=Nothing
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18861702
Actually fso does support wildcards as well...

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolder: Set objFolder=objFSO.GetFolder("C:\YourFolder\")
objFSO.DeleteFile objFolder.Path & "\RID123Drill*.xml"
Set objFolder=Nothing
Set objFSO=Nothing
0
 

Author Comment

by:DougR_MS
ID: 18865283
I am getting this error returned.

Object doesn't support this property or method
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 67

Expert Comment

by:sirbounty
ID: 18865293
What line number?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18865302
Which version?  The 2nd one is faster and should work for what you want...
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18867626
Happy to help - thanx.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now