CIS Logo SVC Logo

   Computing & Information Systems


Schoology Facebook        Search CIS Site      Tutorials

WSH File Removal Example

This example is a bit more involved than the previous ones. It is also a more userful script. (At least, a variant of it found a useful spot on one of our servers.) This script is used to delete large, unneeded files in order to reclaim disk space on certain drives. Of course, if you do not have the permissions to delete certain files, then you cannot delete them with such a script.

Warning: When trying out any script that deletes files, always proceed with caution. One mistake could wipe out a large number of files that you did not want to lose! When first trying such a script, comment off the line(s) that delete files and insert commands that simply print out the names of the files that would have been deleted. Look through this list carefully to be sure that these are precisely the files to delete before actually having the script delete them.

' Filename:	vcremove.vbs
' Author:	Br. David Carlson
' Reference:	Based partially on nimbdascan.vbs by Brian J. LaSitis
'		and Linux vcremove by Br. David Carlson
' Date:		01/25/04
' Revised:      02/13/04 to keep a separate log file for each of the drives scanned. - Br. David
' Description:	This script scans the R, S, and T drives for junk files in Debug and Release directories,
'               removing these files in order to reclaim disk space. The files removed have the form:
'                *.obj *.ilk *.pch *.pdb *.idb  These files are created by Visual Studio, but do not need
'               to be kept on a long-term basis.  This script could be scheduled to be run on a periodic basis.

Option Explicit

Dim cTarget

Sub ClearDirectory(cTarget)
   Const cScriptPath = "c:\MyScripts"   ' Adjust this location as desired.
   Const DeleteReadOnly = True

   Dim oShell, fso, tsInFile, tsOutFile, tsFileName, cDir

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

   ' Produce the list of files:

   cDir = Left(cTarget, 1)
   oShell.Run "%comspec% /c dir " & cTarget & "*.obj " & cTarget & "*.ilk " & cTarget & "*.pch " & cTarget & _
      "*.pdb " & cTarget & "*.idb /s /b > " & cScriptPath & "\vc.tmp", 0, True

   Set fso = WScript.CreateObject("Scripting.FileSystemObject")
   Set tsInFile = fso.OpenTextFile("vc.tmp")
   Set tsOutFile = fso.OpenTextFile("vclog" & cDir & ".txt", 2, True)   ' Put the drive letter into the filename.

   ' Remove the files:

   Do While Not tsInFile.AtEndOfStream
      tsFileName = tsInFile.ReadLine
      ' Let's not record too much unless we really need it:  tsOutFile.WriteLine("Processing: " & tsFileName)
      if (instr(1, tsFileName, "\Debug\", vbTextCompare) > 0) or _
         (instr(1, tsFileName, "\Release\", vbTextCompare) > 0) then
         tsOutFile.WriteLine("Deleting: " & tsFileName)
         fso.DeleteFile(tsFileName), DeleteReadOnly
      end if


   Set tsOutFile = Nothing
   Set tsInFile = Nothing
   fso.DeleteFile("vc.tmp"), DeleteReadOnly

   Set oShell = Nothing
   Set fso = Nothing
End Sub

' Clean out whatever drives you wish:
ClearDirectory "R:\"
ClearDirectory "S:\"
ClearDirectory "T:\"

This script uses a "sub" procedure to delete the desired files on a particular drive. That way the procedure can be called for whatever drives you wish to process. Here it is shown processing 3 drives: R, S, and T.

The oShell.Run line contains a lot. This is the line that generates the list of potential files to be deleted. It uses %comspec% /c to run the dir directory command in the computer's command interpreter. Note that the comspec system variable automatically contains the path for your command interpreter. For example, on many systems this variable contains the string "C:\WINNT\system32\cmd.exe", which you can see by entering the following command in a DOS box:

echo %comspec%

The dir command is used to look for files of the desired form (such as *.obj). Note the use of the /s and /b switches for the dir command. The /s tells it to search through subdirectories as well, thus giving a search through the whole directory tree rooted at the starting location (something such as R:\). The /b switch tells the command to use "bare" output, which simply consists of the filename for each file that matches what we are looking for. You can use "help dir" at the command prompt for more information. You can also try out a command like the one used in our script by entering the following in a DOS box:

dir c:\windows\*.exe /s /b

The output of the dir command is redirected by the > sign to the text file c:\MyScripts\vc.tmp, which is created if it does not already exist. In fact, anything already in this file would be wiped out when this command writes to the file. Finally, this long line of code ends with 2 parameters for the Run method. The 0 parameter has to do with the appearance of the window and the true indicates that the script should wait until the running of this command is completed before going on. You can read the details on the Run method by going to the MSDN page on the Run method.

The script next opens the vc.tmp file for input so that we can begin to read through the list of files that are candidates for deletion. It also opens a text file for output. This file has a name that contains the drive letter. For example, if we are cleaning out the R drive, the filename is vclogr.txt. This log file will contain the name of each file that the script deletes from this drive.

The loop obviously reads one line of the input file, vc.tmp, at a time. Each line is a filename from vc.tmp, and is placed in the variable tsFileName. Commented off is a line of code that would write this filename to the log file. This is turned off since it can generate a lot of output. However, the first time you try a script like this, you would want to see these filenames (and have the DeleteFile line commented off until you are sure that the list of filenames is correct).

The IF section is used to test each filename (really a complete pathname) to see if it contains Debug or Release as a directory in the path. (Note the \'s on each side of these so that Debug and Release have to be directories, not merely substrings of some longer directory name.) This is used because Visual Studio puts the *.obj and other files that we wish to delete under directories with these names. Files with these names that are in other locations might be used by a different software package and should not be deleted. Note that the instr function returns the index of the starting location of one string inside of another. The parameter 1 specifies to start searching at index 1. The second parameter is the string to search, while the third is the substring for which to search. The final parameter specifies the type of comparison to use. Here, vbTextCompare indicates text comparison. The return value for instr is typically 0 if the search fails to find the desired string.

Anytime a filename passes the above IF test, we use the DeleteFile method to remove the file. Note that the DeleteReadOnly constant has value true. It is used here to tell DeleteFile to even delete the file if it is marked as read only. Note that the name of the file being deleted is written to our log file.

The procedure ends by closing both the input and output files, deleting the temporary file vc.tmp, and clearing out all of the objects that we created. (The latter is done by assigning Nothing into them.)

You have now seen a fairly practical use of scripting. See what useful scripts you can devise. Just be careful if the script deletes things!

Back to main Windows Script Host page

Author: Br. David Carlson
Last updated: February 11, 2007