Get 3.9% APR, Get Instant Credit - Click here!

PC Magazine

  PC Tech

File Management via Windows Scripting

Introduction

File Management Objects

A General-Purpose File-Deletion Script

A Few Details

Figure 1: File deletion script.

Figure 2: VBScript's File-Handling Objects



X10.com - The SuperSite for Home Automation!

Click here for E*TRADE.

 
  Categories
Scripting Languages
Windows 95/NT
Operating Systems

Related Stories:
The Windows Scripting Host
-- Operating Systems 2/10/98

File Management via Windows Scripting
Figure 1: File deletion script.

Continued from A Few Details

This script lets you designate by extension a set of files to delete.

Download this script as part of this issue's archive.

Dim FSO, WSH, TheExtension, TheCount
Dim TheFolder, Message, YesNo
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("WScript.Shell")
Set TheFolder = FSO.GetFolder("C:\")	 'start at the root of C 
  
  TheExtension = InputBox("What extension to delete-don't use a '.', example: tmp")
  TheExtension = UCase(TheExtension)
  Message = "Are you sure you want to delete ALL files in ALL sub-directories with "
  Message = Message & "the extension " & vbCrLf
  Message = Message & vbCrLf & vbTab & vbTab & vbTab & TheExtension
  YesNo = MsgBox(Message, vbYesNo)
  If YesNo = vbYes Then 
       Message = "Press Enter or click on OK to start the process. "
	 Message = Message & "Note, this can take some time. A message "
 Message = Message & "box will appear for one second after each "
 Message = Message & "100 sub-directories are processed. "
 Message = Message & "It will vanish after 1 second automatically"
	 MsgBox Message
	 WorkWithSubFolders TheFolder, TheExtension
  End If
MsgBox "Done! Worked on " & TheCount & " directories. Click on OK or press Enter to end."

Sub WorkWithSubFolders(ByVal AFolder, ByVal TheExtension)
  Dim MoreFolders, TempFolder
  TheCount = TheCount + 1 'VBScript has no static variables, 
                          'so use a global variable
  If TheCount  Mod 100 = 0 Then 
    Message = "Have worked so far with " & TheCount & " folders." 
    WSH.Popup Message, 1,"File deletion at work"  'super message box 1 second delay
  End If
  KillFilesWithExtensionIn AFolder, TheExtension
  Set MoreFolders = AFolder.SubFolders
  For Each TempFolder In MoreFolders
    WorkWithSubFolders TempFolder, TheExtension
  Next
End Sub

Sub KillFilesWithExtensionIn(AFolder,TheExtension)
Dim AFile, TheFiles
  On Error Resume Next
  Set TheFiles = AFolder.Files
  For Each AFile In TheFiles
    If UCase(FSO.GetExtensionName(AFile.Path)) = TheExtension Then
      AFile.Delete
    End If
  Next
End Sub

Next: Figure 2: VBScript's File-Handling Objects

Published as Operating Systems in the 6/30/98 issue of PC Magazine.


 SPONSORED LINKS
@Backup   Your Solid Online Backup Plan. Download Now.
Services   9c/MINUTE LONG DISTANCE, 5c/MINUTE ON SUNDAYS!
STORAGE   Quantum means non-stop business, 24 hours a day
Software   X10.com -- The SuperSite for Home Automation
Books   Bargain Books up to 90% off at barnesandnoble.com
 ZDNET FEATURED LINKS
Downloads   Check out the best new downloads in Reviewer's Raves
Bargains!   Shop the Basement for best buys on computer products
Free Help   Got computing questions? ZDHelp has all the answers!
 MAGAZINE OFFERS
Free Offer   Get a FREE SUBSCRIPTION to Inter@ctive Week

TOP
Copyright (c) 1998 Ziff-Davis Inc.