«

»

Mar 31 2008

Delete all Temporary Files

Del is a command used to delete files from the computer.

DEL or ERASE is a command used to delete files from the computer.

 

Windows 2000 and Windows XP syntax

 

Deletes one or more files.

 

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

names

 

Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted.

 

/P

 

Prompts for confirmation before deleting each file.

 

/F

 

Force deleting of read-only files.

 

/S

 

Delete specified files from all subdirectories.

 

/Q

 

Quiet mode, do not ask if ok to delete on global wildcard.

 

/A

 

Selects files to delete based on attributes

 

attributes

 

R  Read-only files
S  System files
H  Hidden files
A  Files ready for archiving
  Prefix meaning not

 

IMPORTANT WARNING: This command is easy to use, but it is the most dangerous one you will encounter in DOS (apart form FORMAT). If you aren’t careful, you may delete a file which you, the system, or someone else needs. And, unless you have saved a backup of that file, the erased file is gone for good. For this reason it is good practice to use only complete file specifications with the ERASE/DEL command (and to keep backups of your most valuable files). As a safety precaution, never use the wild-card characters ‘*’ and ‘?’ in ERASE/DEL commands.

When Using wild-cards ( regular expressions ) the default
DOS command does not let you auto-erase all files, but confirm one by one. it can bypassed using the switch /Q
The switch /S removes also files from sub-folders.

Dim outMsg
If RunDosCommand( "DEL /F /S /Q %TEMP%", outMsg ) Then
   Reporter.ReportEvent micPass, "RunDosCommand", "Ended successfully."
Else
   Reporter.ReportEvent micFail, "RunDosCommand", outMsg
End If

A good practice is to clear out TEMP files on a regular basis – this is best done at startup when no applications are running. To delete all files in all sub-folders of C:\temp\ but leave the folder structure intact.When clearing out the TEMP directory it is not generally worthwhile removing the sub-folders too – they don’t use much space and constantly deleting and recreating them can potentially increase fragmentation within the Master File Table.

RunDosCommand function

Public Function RunDosCommand( ByVal cmd, ByRef stdout )
   Dim sb, wShell, exec
   Dim input

   RunDosCommand = False
   Set wShell = CreateObject( "WScript.Shell" )
   ' ** Executing the command
   Set exec = wShell.Exec( "%comspec% /C " & cmd )
   ' ** Checking for errors...
   If Not exec.StdErr.AtEndOfStream Then
      stdout = "STDERR: " & exec.StdErr.ReadAll
      Set exec = Nothing : Set wShell = Nothing
      Exit Function
   End If
   ' ** The stringBuilder is faster than concatenation ( & )
   Set sb = DotNetFactory.CreateInstance( "System.Text.StringBuilder" )
   Do While Not exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      ' ** Appending only not empty lines 
      If Len( input ) > 0 Then
         sb.AppendLine input
      End If
   Loop
   ' ** retieve the output
   stdout = sb.ToString
   RunDosCommand = True
   Set sb = Nothing : Set exec = Nothing : Set wShell = Nothing
End Function