How Can I Verify That a Folder Has Files in It and, If So, Delete Those Files?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I verify that a certain folder (C:\TestLog) has files in it and, if so, delete all those files?

— DC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DC. Before we begin we should point out that – when using WMI – it doesn’t matter whether or not a folder has any files in it: you can still tell WMI to delete all the files in that folder. If no such files exist that won’t phase WMI in the least. Why are we telling you this? Just to point out that, if your only concern is deleting files, then you don’t have to worry about verifying that the folder actually has files. Instead, just delete away.

Of course, you might have other reasons for wanting to know whether or not a folder has any files in it. With that in mind, we came up with this script:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFileList = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name=’C:\TestLog’} Where ” _ & “ResultClass = CIM_DataFile”)

If colFileList.Count = 0 Then Wscript.Echo “There were no files in the folder.” Else Wscript.Echo “There were ” & colFileList.Count & ” files in the folder.”

For Each objFile In colFileList objFile.Delete Next End If

The script begins by connecting to the WMI service on the local computer. If you’re wondering why we chose to use WMI to carry out this task (as opposed to, say, the FileSystemObject) here’s one reason: although this script is designed to work against the local computer we can easily modify it to work against any computer where we have local Administrator rights. All we have to do is change the value of the variable strComputer from a dot (WMI-speak for the local computer) to the name of the remote machine. You can’t do that with the FileSystemObject, at least not as easily.

After connecting to the WMI service we then use this line of code to retrieve a collection of all the files in the folder C:\TestLog:

Set colFileList = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name=’C:\TestLog’} Where ” _
        & “ResultClass = CIM_DataFile”)

And, yes, Associators Of queries are kind of funny-looking, but don’t let that bother you. All you really need to know is this: working with any other folder (for example, D:\Logfiles) is as easy as replacing the path C:\TestLog with the path D:\Logfiles:

Set colFileList = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name=’D:\Logfiles’} Where ” _
        & “ResultClass = CIM_DataFile”)

Of course, that’s all well and good, but how do we know whether or not C:\TestLog has any files in it? Well, our query returns a collection consisting, in this case, of all the files (instances of the CIM_DataFile class) found in the folder C:\TestLog. As it turns out, WMI collections have a property named Count which returns the number of items in the collection. We can determine whether or not the folder has any files in it just by checking to see if the Count is equal to 0:

If colFileList.Count = 0 Then

If the Count is equal to 0 that means there were no files in the folder; in that case, we echo a message saying that no files were in the folder. If the Count is not equal to 0 then we do two things. First, we use this line of code to report back the number of files that were in the folder:

Wscript.Echo “There were ” & colFileList.Count & ” files in the folder.”

Second, we set up a For Each loop to walk through the collection of files. For each file in the collection we call the Delete method and delete that file:

For Each objFile In colFileList
    objFile.Delete
Next

When we’ve finished with the For Each loop all the files will be gone, and everyone will live happily ever after. (Except, of course, for those poor files we just wiped out. Hopefully they’ve all gone to a better place.)

0 comments

Discussion is closed.

Feedback usabilla icon