Mar 31 2008

Delete one File in Current Directory

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


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.


Prompts for confirmation before deleting each file.


Force deleting of read-only files.


Delete specified files from all subdirectories.


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


Selects files to delete based on 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.

Dim outMsg
If RunDosCommand( "DEL test.doc", outMsg ) Then
   Reporter.ReportEvent micPass, "RunDosCommand", "Ended successfully."
   Reporter.ReportEvent micFail, "RunDosCommand", outMsg
End If

the example deletes the test.tmp in the directory that you currently are in, if the file exists.

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
   ' ** retieve the output
   stdout = sb.ToString
   RunDosCommand = True
   Set sb = Nothing : Set exec = Nothing : Set wShell = Nothing
End Function