By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

How to Delete Files With AutoIt

This is how you Delete or Recycle files with the AutoIt scripting language..

Edited: 2015-02-13 00:26

AutoIt Logo

This is just a short AutoIt Tutorial on how you can delete all the files in a directory, without knowing the names of the files located in the directory.

There are two Functions that you should know about when working with files and directories. These are the FileDelete and FileRecycle functions. The FileRecycle Function will simply move the files to the Recycle Bin for later deletion, while FileDelete will delete the files directly.

Delete all files in Directory with AutoIt

The FileDelete Function can be used to delete all the files contained within the directory.

The FileDelete Function takes the path to the file to be deleted, in this case we only supplied a directory path, which will leave behind a empty directory. The supplied path can either be relative, or absolute. I.e.

FileDelete("ScriptTemp\")

The above relative path assumes that we have a directory called "ScriptTemp", located in the same directory as the running script. This could be used by our script, for temporary files that needs to be stored.

A similar absolute path could look like the below, assuming that the ScriptDirectory is located in c:

FileDelete("C:\ScriptDirectory\*.tmp")

As you can see, we used a wildcard this time. The *.tmp wildcard will only delete files ending in .tmp

Wildcards such as *.tmp will only remove .tmp files, and not .tmping files. This is unlike the behavior of the FileFindFirstFile function, where both will be matched by the wildcard.

How to Recycle files with AutoIt

The FileRecycle Function behaves similar to FileDelete, except that the files will be moved to the recycle bin in the operating system, rather than getting deleted directly. When a directory path is given rather than a file name, the entire directory will be moved to the recycle bin – if you want to only move the files contained within the directory, use the (*.*) wildcard after the path. I.e.

FileRecycle("MyDirectory\*.*")

The FileRecycle Function also accepts absolute paths.

To empty the Recycle Bin you can use FileRecycleEmpty. The Function accepts 1 parameter, which is the rootpath of the drive where the Recycle Bin should be emptied – leaving this out will empty the bins for all the drives. The below will do just that:

FileRecycleEmpty()

The next script will only empty the Recycle Bin of the specified drive.

FileRecycleEmpty("c:\")