You can use AutoIt to move, delete and create folders quickly and automatically – like with most other tasks, this is accomplished with build-in functions.
Working with folders in AutoIt is very easy, as it only takes a few lines to perform tasks such as moving or deleting directories. This tutorial will teach you how to master the functions related to directory handling.
Creating a Directory with DirCreate
To create a directory, we will be using the DirCreate function, this function takes just one parameter, which is the path of the directory to be created. So if you wanted to create a folder called New Folder located in your c:/ drive, coding the script as demonstrated below should accomplish what you want. I.e.
Checking for Directory existence with DirGetSize
Checking for the existence of a directory, before attempting to create a sub-folder, can be necessary to avoid failure in the folder creation process. We will therefor make use of a simple if statement to check if the folder exists, before attempting to create a sub-folder – this we'll do using DirGetSize on the parent folder. I.e.
If DirGetSize("c:/Parent Folder/") = -1 Then MsgBox(48, "My Message", "Directory does not exists!") Else DirCreate("c:/Parent Folder/New Sub Folder/") EndIf
You can also reverse this if check if you want to only create the folders, when they do not exist. Attempting to create a folder that already exists, will not however, result in loosing the contents of the folder, or any errors – so if you care about whether a folder exists, you must code the checks on your own.
Moving a Directory with DirMove
If you need to move a directory, including all its contents and sub folders, you need to use DirMove. This function accepts 3 arguments, the 2 first are the source and destination paths, and the third is just a flag controlling whether the destination should be overwritten in case it already exists.
DirMove("C:\Source folder\", "C:\Parent\Destination Folder Name")
The third argument controls what is to happen if the directory in the destination path already exists, when included, it accepts a value of either 1 or 0 – where the first will overwrite the files, and the latter won't. I.e.
; move directory, and overwrite the files. DirMove("C:\Source folder\", "C:\Parent\Destination Folder Name", 1)
If no flag was provided, the function will use its default value of 0 – which won't overwrite the files – in which case, if the destination folder already exists, nothing will be moved.
Deleting a Directory with DirRemove
To remove a folder from AutoIt scripts, we'll use DirRemove. This function accepts 2 parameters, the first is the path of the folder that we want deleted, the second parameter controls whether sub-folders should be deleted as well. You will not be able to delete a folder that still has sub-folders, unless you set the flag.
; Remove files and subdirectories of specified folder DirRemove("C:\Parent\Destination Folder Name", 1)