Skip to content

EraseFile

See Also: File I/O Commands, CopyFile, File_Exist, Remove_Directory, Set_Directory

Purpose

Deletes one or more disk files.

Syntax

EraseFile {file-spec}

Where

  • {file-spec}: The specification of a file or set of files (including wildcard characters), optionally including a path.

What It Does

EraseFile deletes the file(s) identified by {file-spec}. It will use the current working folder unless a path is provided as part of the file specification. You may use any valid Windows file specification (including wildcard characters).

Be extremely cautious with EraseFile. DO NOT CALL ERASEFILE WITHOUT ANY ARGUMENTS! Doing so will delete all files in the current working folder without any warning.

Examples

Example 1

The sample deletes the file "hello.txt" in the current working folder (since a path is not specified).

EraseFile "hello.txt"

Example 2

The sample deletes all files with the file extension "txt" in the "C:\My Notes" folder.

EraseFile "C:\My Notes\*.txt"

Example 3

The sample deletes the file "notes.txt" in the "C:\My Notes" folder, then checks whether it was successfully deleted. It notifies the user of the result. The function DeleteFile is written in a generic manner so it can be used with any file.

Function DeleteFile String sFile Returns Boolean
    Boolean bExist
    EraseFile sFile
    File_Exist sFile bExist
    // this function returns whether the deletion was successful, thus:
    // return False (did not delete) if file still exists and True (did delete) if it does not
    Function_Return (not(bExist))
End_Function

Procedure DeleteMyNotes
    String sFile
    Boolean bSuccess
    Move "C:\My Notes\Hello.txt" to sFile
    Get DeleteFile sFile to bSuccess
    If (bSuccess) Begin
        Send Info_Box ("The file '" + sFile + "' was deleted successfully" "Success!")
    End
    Else Begin
        Send Stop_Box ("The file '" + sFile + "' was NOT deleted successfully" "Error!")
    End
End_Procedure

Send DeleteMyNotes

Notes

  • EraseFile deletes files specified in {file-spec} without triggering any error, warning, or notification.

  • If EraseFile is executed on a non-existent file, no error will be triggered. Use the File_Exist command to determine whether a file exists before erasing the file(s).

  • You may use "wild card" (ambiguous) file names for {file-spec}. Be extremely careful when using wildcards in EraseFile command statements. EraseFile provides no verification or protections unless you program them, and erroneous usage could potentially erase important data!

  • Specifying a folder name in {file-spec} with no file name(s) specified will erase all files in that folder. For example, the following two lines of code are functionally equivalent and result in the deletion of all files in the "C:\Temp\Test" folder:

    EraseFile "C:\Temp\Test"
    EraseFile "C:\Temp\Test\*.*"
    
  • EraseFile may behave differently on different operating systems. For example, EraseFile "" will delete all files in the current folder on some versions of Windows, but no files on other versions. Thorough testing in all deployed environments is advised.

  • If you want to erase all data from a database table but leave its structure intact, you can use the Zerofile command.