Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I auto-delete specific file types from the My Pictures folder? If an error occurs with my fax machine, .drg and .txt files get added to My Pictures. I then have to delete these files before the fax machine will work again.

-- WW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, WW. You know, this was a case where the Scripting Guys almost outsmarted themselves. (True, the average hamster would have a pretty good chance of outsmarting the Scripting Guys. But that’s not the point.) When we first sat down to try and answer this question we wrote a complicated script that relied on WMI event monitoring, a script that required a WQL query overflowing with single and double quotes, and which called for file paths similar to this:

C:\\\\Documents and Settings\\\\kenmyer\\\\My Documents\\\\My Pictures

Eep! It all worked, but we had no idea how we were going to explain such a complicated script in this little column.

And then it dawned on us: this is a simple task (periodically remove selected files from a single folder). That means we ought to be able to solve the problem using a simple script. (OK, technically the hamster in the office next door came up with this idea. But we still had to write the script; after all, if nothing else we can type a little faster than he can.) Here’s a much simpler way to carry out this chore:

On Error Resume Next

Const MY_PICTURES = &H27&
Const DeleteReadOnly = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_PICTURES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

Do Until i = 1
    strFiles = strPath & "\*.txt"
    objFSO.DeleteFile(strFiles), DeleteReadOnly

    strFiles = strPath & "\*.drg"
    objFSO.DeleteFile(strFiles), DeleteReadOnly
   
    Wscript.Sleep 60000
Loop

Granted, what we’re doing here isn’t really auto-deleting files, at least not if “auto-deleting” means deleting a file the split-second it gets created. In this case, we have a script that connects to the My Pictures folder and periodically deletes any .txt and .drg files it finds there. We’ve configured the script to do its check-and-delete thing every 60 seconds (that is, every 60,000 milliseconds), but you can adjust that value as you see fit.

As for the code itself, we start things off the On Error Resume Next statement. This statement is very important. In order to keep the script simple, we don’t bother verifying whether or not there actually are any .txt or .drg files in the My Pictures folder; we just have code that deletes those files. If it turns out that no such files exist, an error will be generated: without the On Error Resume Next statement that error would cause the script to blow up. With this statement, however, the script simply shrugs its shoulders and keeps going.

Next we define a pair of constants:

Const MY_PICTURES = &H27&
Const DeleteReadOnly = TRUE

MY_PICTURES is assigned the value &H27&; this is the value used by the Shell object to locate the My Pictures folder. Granted, we could simply hardcode in a path like this:

C:\Documents and Settings\kenmyer\My Documents\My Pictures

By using the Shell object and the MY_PICTURES constant, however, we can create a “generic” script that will work on any computer, regardless of the actual path to the My Pictures folder.

Meanwhile, the constant DeleteReadOnly tells the FileSystemObject to go ahead and delete any read-only files found in the My Pictures folder. This is also very important: if we leave this out then the script will crash if it encounters any read-only .txt or .drg files. And we’re guessing you don’t really want your script to crash.

Next we create instances of the Scripting.FileSystemObject and the Shell.Application objects. We use the Shell.Application object to determine the path to the My Pictures folder and store that path in a variable named strPath; that’s what we do here:

Set objFolder = objShell.Namespace(MY_PICTURES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

As you can see, we call the Namespace method, passing this method the constant MY_PICTURES (which tells the Shell object which special folder to bind to). In order to get the path to the folder, we call the Self method; this creates an object reference to the actual folder. Once we have that object reference (objFolderItem) we can then store the value of the Path property in the variable strPath.

Got all that? Good. Finally we have the following Do loop, which loops until i = 1:

Do Until i = 1
    strFiles = strPath & "\*.txt"
    objFSO.DeleteFile(strFiles), DeleteReadOnly

    strFiles = strPath & "\*.drg"
    objFSO.DeleteFile(strFiles), DeleteReadOnly
   
    Wscript.Sleep 60000
Loop

Note. When will i equal 1? Never; there isn’t any code that changes the value of i (and, because i was never assigned a value, it takes on the default value of 0). This allows us to create an endless loop that will run forever. The only way to break out of the loop is to terminate the script process.

Time for a quick review. As you might recall, the variable strPath contains a value similar to this:

C:\Documents and Settings\kenmyer\My Documents\My Pictures

In order to delete all the .txt files in the folder (our first task) we need to construct a path that looks like this, tacking \*.txt to the end of the path:

C:\Documents and Settings\kenmyer\My Documents\My Pictures\*.txt

And that’s what we do with this line of code, storing the new value in a variable named strFiles:

strFiles = strPath & "\*.txt"

We can then use the FileSystemObject’s DeleteFile method to delete all the .txt files in the folder:

objFSO.DeleteFile(strFiles), DeleteReadOnly

Notice that we pass DeleteFile two parameters: the full path to the set of files and the constant DeleteReadOnly. We do the exact same thing for .drg files, then use this line of code to pause the script for 60 seconds:

Wscript.Sleep 60000

When 60 seconds are up we loop around and do the same thing all over again, once more deleting any .txt and .drg files found in the My Pictures folder.

And by the way, thanks: we do believe that, when it comes to scripting, at least, the Scripting Guys are just as good as the average hamster. Maybe not better, but just as good.