Delete all Files in a Folder

Article Tools

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

Dim outMsg
If RunDosCommand( "DEL /Q c:\logs\temp\*.*", outMsg ) Then
   Reporter.ReportEvent micPass, "RunDosCommand", "Ended successfully."
Else
   Reporter.ReportEvent micFail, "RunDosCommand", outMsg
End If

the example d indicates that you would like to delete all files in the c:\logs\temp directory, otherwise an error message will be displayed in the Reporter.

This command will not delete files with “Read Only” attribute. to delete also “Read Only” files add the switch /F

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
Previous postDelete all Fles in a Folder Matching a Pattern Next postDelete one File in any Directory

Related Posts

Post Your Comment

You must be logged in to post a comment.