Visual Basic Script - Deleting Files
Author: jason
Date: 2004-05-31
Category: Technical

Quick change in lesson order. I'm going to show file deletion for the third lesson. We'll go back to file listings in a little bit. I know you're anxious to see what I'm going to create with the file listing script.

What we will do:
1. Get information about the logged on user
2. Delete files in the logged on user profile

Here's the full code (delete.vbs)
Code

Option Explicit
Dim WshShell, WshEnv, UserPath, FileSys, oFile
On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("Process")
UserPath = WshEnv("USERPROFILE") & "\"
Set FileSys = CreateObject("Scripting.FileSystemObject")
For Each oFile In FileSys.GetFolder(UserPath & "Recent").Files
oFile.Delete 1
Next


Not too bad. Only ten lines. Should be a little familiar, if you've been following along. Lets step through the code

Practice good coding!
Code

Option Explicit


Create some variables, which we'll discuss in a moment
Code

Dim WshShell, WshEnv, UserPath, FileSys, oFile


We don't want to stop on errors, now do we? Continue with the next line, even with an error
Code

On Error Resume Next


We need to create the windows script automation shell object. We'll use this in the next line.
Code

Set WshShell = WScript.CreateObject("WScript.Shell")


Set our environment to the process value. This lets us get some neat stuff. Have you ever run "set" from the command prompt?
Code

Set WshEnv = WshShell.Environment("Process")


Now lets get what I'm looking for, the USERPROFILE. This is the logged on user profile. If your username is JoeDirt, then this references: c:\documents and settings\JoeDirt. Pretty cool, eh? The " & "\" " adds a slash at the end of the folder. So it would be: c:\documents and settings\JoeDirt\. But without the period, haha...
Code

UserPath = WshEnv("USERPROFILE") & "\"


You're familiar with this now, aren't you? This is our file system object. Now we can do things with the file system, like...
Code

Set FileSys = CreateObject("Scripting.FileSystemObject")


Look at each item in the file object of the Recent folder of JoeDirt and...
Code

For Each oFile In FileSys.GetFolder(UserPath & "Recent").Files


Force a delete!!
Code

oFile.Delete 1


Keep doing it until there aren't anymore files.
Code

Next


We're done. Now, what's the point of this exercise? Well, we sometimes use roaming profiles where I work. Users sometimes open webpages with extremely long filenames. These names go into the Recent folder. When the user logs off, the system cannot copy them to the server because the name is too long. The user then gets an error, and their profile isn't entirely saved up onto the server. I think this might have something to do with the differences between XP and W2k as well, but that's another story. Before they logoff we encourage them to run this script to clean up their recent folder. It could also be modified to easily delete their Temp and Temp Internet files.

Cheers!

Reference: http://www.devguru.com/Technologies/wsh/quickref/wshshell_Environment.html

Reference: http://www.scit.wlv.ac.uk/appdocs/chili-asp/html/vbscript_language_reference.htm



jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009