Q(uick)BASIC Statement: KILL

Quick View

KILL

A statement that uses DOS to delete a disk file

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • KILL filespec$
Description/Parameter(s)
filespec$ Identifies the file or files to delete. It may include a path and the DOS wildcard characters ? and *.
Example
INPUT "File to delete: "; f$ KILL f$

See also:

Syntax
  • KILL filespec
Description/Parameter(s)

The KILL statement is similar to the DOS ERASE or DEL commands.

KILL is used for all types of disk files: program files, random data files, and sequential data files. The filespec may contain question marks (?) or asterisks (*) used as wild cards. A question mark matches any single character in the file name or extension. An asterisk matches one or more characters starting at its position.

You can use KILL only to delete files. To delete directories, use the RMDIR command. Using KILL to delete a file that is currently open produces an error message that reads "File already open."

Warning

  • Be extremely careful when using wild cards with KILL. You can delete files unintentionally with the wild card characters.
Examples

Here is an example of using wild-card characters with KILL. This example will not run unless the specified files are found:

KILL "DATA1?.DAT" 'Kills any file with a six-character 'base name starting with DATA1 and 'also with the extension .DAT. KILL "DATA1.*" 'Kills any file with the base name 'DATA1 and any extension. KILL "GREG*.DAT" 'Kills any file with the extension '.DAT in a subdirectory called GREG.

Here is an example program that deletes the file specified on the command line:

' Make sure you have selected Full Menus from the Options menu, ' then select Modify COMMAND$ from the Run menu to specify a file. ' Specify any file, but be sure it's one you want to delete. DEFINT A-Z CLS ' Clear screen ON ERROR GOTO Errorhandle 'Set up error handling. FileName$ = COMMAND$ 'Get file name. KILL FileName$ PRINT FileName$ " deleted" END Errorhandle: Number = ERR IF Number = 53 THEN PRINT "Couldn't delete " FileName$ ; PRINT "; file does not exist in current directory" ELSE PRINT "Unrecoverable error:";Number ON ERROR GOTO 0 'ON ERROR GOTO zero aborts program. END IF RESUME NEXT
Syntax
  • KILL filespec$
Description/Parameter(s)
  • The argument filespec$ is a string expression that may contain a path and the DOS wildcard characters question marks (?) or asterisks (*). A question mark matches any single character in the filename or extension. An asterisk matches one or more characters starting at its position.

Usage Notes

  • The KILL statement is similar to the DOS ERASE or DEL command.
  • KILL is used for all types of disk files: program files, random access data files, and sequential data files.
  • KILL deletes files only. To delete directories, use the DOS RMDIR command or the BASIC RMDIR statement.
  • Using KILL to delete a file that is currently open generates an error message that reads, "File already open."

Important

  • Be extremely careful when using wildcard characters with KILL. You can delete files unintentionally.
Examples

This example uses the KILL statement to delete files.
Note: This example will not run unless the specified files are found.

KILL "DATA1?.DAT" 'Deletes files with a six-character base name 'starting with DATA1 and having the extension .DAT. KILL "DATA1.*" 'Deletes files with the base name DATA1 and any 'extension. KILL "\GREG\*.DAT" 'Deletes files with the extension .DAT in a 'directory called GREG.

The following example deletes the file specified on the command line.
Note: To run this example, select Full Menus from the Options menu, then select Modify COMMAND$ from the Run menu to specify a file to delete.

DEFINT A-Z ON ERROR GOTO Errorhandle 'Set up error handling. FileName$ = COMMAND$ 'Get file name. KILL FileName$ PRINT FileName$; " deleted" END Errorhandle: Number = ERR IF Number = 53 THEN PRINT "Couldn't delete "; FileName$; PRINT "; file does not exist in current directory" ELSE PRINT "Unrecoverable error:"; Number ON ERROR GOTO 0 'Aborts program. END IF RESUME NEXT

See also: