How Can I Use a Wildcard Character to Delete Folders?

How Can I Use a Wildcard Character to Delete Folders?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I delete folders based on a wildcard character? For example, how can I delete all the folders whose name starts with December?

-- RR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RR. Well, that depends. If you’re running Windows XP or Windows Server 2003, you can actually use a wildcard character to identify and then delete all the folders whose name starts with December. If you’re running Windows 2000, you can achieve the same net result: you can identify and delete all the folders whose name starts with December. It’s just that you’ll have to do it in a more clumsy, brute-force fashion.

Let’s start by taking a look at an XP/2003 script that identifies (but doesn’t yet delete) all the folders whose name starts with December:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Directory WHERE FileName LIKE 'December%'")

For Each objFolder in colFolders
    Wscript.Echo objFolder.Name
Next

The key to this script lies in the WQL query:

SELECT * FROM Win32_Directory WHERE FileName LIKE 'December%'

What we’re doing here is selecting all the folders that have a FileName like December. FileName is the WMI property corresponding to what we would call the folder name. (And, yes, there’s sometimes a difference between the name we use for something and the name WMI uses for that same thing.) For example, if we have a folder with the path C:\Scripts\Reports\December, the FileName is December.

LIKE ‘December%’ simply means, “Where the FileName starts with December.” The percent sign (%) is the wildcard character (equivalent to the * used most other places as the wildcard character). In this case, the wildcard indicates that we want folder names that start with December, but we don’t care what - if anything - comes after the word December. What if wanted to look for folders whose names ended with the word December? In that case, we’d put the wildcard at the beginning of the string, like so:

SELECT * FROM Win32_Directory WHERE FileName LIKE '%December'

And what if we wanted folders whose name simply contained the word December (for example, Reports_December_2005)? In that case, we’d put a wildcard before and after the string:

SELECT * FROM Win32_Directory WHERE FileName LIKE '%December%'

The LIKE operator is available only on Windows XP and Windows 2003. If you want to perform this task on a Windows 2000 computer, you’ll have to retrieve the list of all the folders on the machine and then check to see if any of them have a folder name (FileName) starting with December. That’s what we do with this script:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery("SELECT * FROM Win32_Directory")

For Each objFolder in colFolders
    If Left(objFolder.FileName, 8) = "december" Then
        Wscript.Echo objFolder.Name
    End If
Next

With this script we get back a collection of all the folders on a computer, and then check each one to see if the first (leftmost) 8 characters happen to be december. If they are, we echo the folder name; if not, we move on to the next folder in the collection.

This works just fine, although it does take a bit longer to complete. On a test computer with a couple thousand folders, the XP/2003 version took around 15 seconds to complete; the Windows 2000 version took over a minute. But at least it does the job.

Now, what about deleting all the folders that you find? Well, to do that, just substitute the Delete method for the line of code where we echo the folder name. For example, here’s the XP/2003 version of a script that both finds and deletes all folders whose name starts with December:

On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Directory WHERE FileName LIKE 'December%'")

For Each objFolder in colFolders
    objFolder.Delete
Next

Needless to say, be careful about running this particular script; after all, it will delete all the folders it finds whose name starts with December. In some cases, it might be a good idea to first retrieve a list of all these folders and make sure there aren’t any surprises in there (that is, folders you don’t really want to delete that just happen to have a name starting with December) before you start deleting things willy-nilly.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • How do I change the location of the system32 directory, maybe to Program Files?

    Thanks