Create or Delete a Folder and Delete a file in Stata

In this article, we are going to learn how to Delete a File, Create a Folder and Delete a Folder in Stata. We do have to work with files that are in our hard drives.

For deleting file on a hard drive, we have two commands. The “erase” command can be used if we are working on Windows, while the “rm” command would be applicable for Mac. The command for erase would look something like this,

 erase “image.png” 

This command erases the file that we wish to delete from our current working directory. Upon execution, this command would delete the specific file. For Mac users, the command would be,

 rm file.png

Remember, we can delete any file on our hard drive but using the file extension is very important. Extension explains the type of file that requires to be removed. If we try to execute a command without an extension, it would not run. For example, a command for deleting a file without the extension, as following, would show that the file does not exist.

 erase “example”

So if you want to remove the file named example, use its extension, i.e. it’s a .dta file along with the command.

  erase “example.dta”

We can also create a folder, along with creating a subfolder within a current folder. Let us assume that we want to create a folder called “folder2”. Our command would be,

mkdir folder2

If one wants to create a subfolder called “folder3” within the “folder2”, the command to be used should be,

mkdir folder2/folder3

Deleting a folder is also very easy. For that matter, we use the rmdir command, which stands for remove directory

However, you need to remember that this command only deletes empty folders. For example, we have a folder called “full folder” with various files, and we wish to delete it. Our command would be,

rmdir "full folder"

But it would not execute as the folder contains files and data. This command also does not execute when the folder has any kind of subfolder, may it be empty. Happy learning!

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x