Deleting all files in a directory
As you've seen when you used MATCHNAME() to list the contents of an entire directory, your template consisted of the desired directory name, ending with a backslash. It's the same thing when you wish to delete all files in a directory with DELETEFILE. You pass the name of the desired directory which contains the files to delete, ending with a backslash.
If DELETEFILE successfully completes the operation, then an empty string is returned. Otherwise, if there is a problem, an error message is returned.
Note: If the file you wish to delete doesn't exist, this is not considered an error, and DELETEFILE will return an empty string in this case.
Here's an example of deleting all files in the directory C:\MyDir:
/* Delete the files in C:\MyDir directory. */ error = DELETEFILE('C:\MyDir\') /* Display any error. */ IF error \= "" THEN SAY error
Note: You can omit the backslash at the end of the directory name (when you have no wildcard template to append to it). But it's a little more efficient to include the trailing backslash.
Deleting only files with a certain name pattern
Let's say that we wish to delete only those files in C:\TextFiles whose names end with a .TXT extension. For this, we need to append a wildcard template of *.TXT to our directory name. In other words, we'll pass C:\TextFiles\*.TXT.
error = DELETEFILE('C:\TextFiles\*.txt') IF error \= "" THEN SAY error
Note: If the files happen to be in the current directory, then you can eliminate the directory name and simply pass the wildcard template. For example, assume that our current directory is C:\TextFiles (and it obviously already exists), and we wish to delete all text files in it. We can specify only the wildcard template:
error = DELETEFILE('*.txt') IF error \= "" THEN SAY error
Deleting a specific file
If you have a specific file to delete, then you'll pass that specific file's name. It must not end with a backslash. (In the following examples, we'll dispense with error checking).
Here we delete the file C:\Windows\readme.txt.
error = DELETEFILE('C:\Windows\readme.txt')Once again, if the file you're deleting is in the current directory, then you can eliminate the directory name. For example, assume that our current directory is C:\Windows. We can specify only the filename part:
error = DELETEFILE('readme.txt')
Deleting a directory tree (ie, Recursive delete)
those examples where we asked DELETEFILE to delete all the files in a directory, you'll notice that sub-directories and their contents (ie, all of the files and additional sub-directories that may be inside of each sub-directory) were also not deleted. In other words, DELETEFILE doesn't delete an entire directory tree. If you want to delete all sub-directories and their contents, then you'll need to use MATCHNAME to do a recursive search of all the sub-directories in a given directory, and make another DELETEFILE call for each sub-directory (and another DIR() call to delete the sub-directory itself).Here we have a function named DeleteFolder. It is passed the name of a directory to delete, and the MATCHNAME search number. It deletes all files in the directory. And whenever DeleteFolder encounters a sub-directory, it dives into that sub-directory to delete any files inside of that sub-directory too, as well as deleting the sub-directory. DeleteFolder does this by calling itself -- passing a new search number, and the name of the sub-directory. In other words, whenever it encounters a sub-directory, it starts up another, simultaneous MATCHNAME loop to query that new directory and delete its contents. It is also important to use the PROCEDURE keyword.
We'll write this function so that it returns an empty string if it is successful. If there is a problem, it will abort the delete process and return that error message.
/* Deletes the contents of a folder and all sub-folders inside it */ DeleteFolder: PROCEDURE /* Delete all files in this directory. */ error = DELETEFILE(ARG(1)) IF error \== "" THEN RETURN error /* DO UNTIL no more sub-directories. */ DO UNTIL error \== "" /* Get the name of the next sub-directory inside of this directory. */ error = MATCHNAME(ARG(2), 'name', ARG(1), 'D') /* No error? */ IF error == "" THEN DO /* Delete all files in this sub-directory. */ error = DeleteFolder(ARG(1) || '\' || name, ARG(2) + 1) IF error \== "" THEN SIGNAL BadDeleteFolder /* Delete the sub-directory too (now that it's empty). */ error = DIR(ARG(1) || '\' || name, 'D') IF error \== "" THEN DO BadDeleteFolder: MATCHNAME(ARG(2)) RETURN error END END END /* Return an empty string if success, or an error message */ IF error == 'DONE' THEN error = "" RETURN errorAnd here's an example of how you may call DeleteFolder to delete all the files/sub-directories in C:\MyDir. We arbitrarily start with a search number of 1.
result = DeleteFolder('C:\MyDir', 1) /* Did DeleteFolder complete its job? If not, display why. */ IF result \== "" THEN SAY result