Home > Mobile >  How to delete a non-empty folder in Batch script?
How to delete a non-empty folder in Batch script?

Time:05-29

I have tried numerous methods of doing this including Remove-Item, rmdir, del, and rd. I have seen similar questions, but none of the answers have helped me. I'm sorry if I'm doing something wrong; I'm pretty new here.

CodePudding user response:

For the previous answer, I checked the current Microsoft documention about commands it has, and it does NOT say clearly that the del command will delete only files. See: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/del

But what you need, then, is the rmdir command. Its use is (ironically?) with the same arguments the del command has. I did not expect this. In better operating systems, we only have ONE command to delete everything stored in a disk or partition, whatever it is: files, folders, special files, hidden files, hard links, symbolic links, ... and I think this is all. Microsoft, to start, has 2 names for the del command: del and erase. Talk about standardization! Further, there is a separate command to delete directories or folders. Microsoft also does not know which name to use for them: there are places where they are called folders, and other places where they are called directories. Go figure. Anyway, let's conclude this story!

The command you need to recursively delete a folder, and all files OR folders it contains is:

rmdir [name of the folder] /s /q

Please note the "/s" and "/q" arguments, which have the same meaning as for the del command, as I explained before, but now they come AFTER the name of the folder! This is what the command documentation shows, as you may read in:

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/rmdir

But there is more possible reasons for the recursive directory deletion failing! If you try to delete a directory that has system files or hidden files, the rmdir command will fail. To solve this problem, you need to do more work. In the remarks written in the documentation pointed above, we read:

You can't delete a directory that contains files, including hidden or system files. If you attempt to do so, the following message appears:

The directory is not empty

Use the dir /a command to list all files (including hidden and system files). Then use the attrib command with -h to remove hidden file attributes, -s to remove system file attributes, or -h -s to remove both hidden and system file attributes. After the hidden and file attributes have been removed, you can delete the files.

CodePudding user response:

To delete a non empty folder, you need something called recursion. What we usually read is that we want to "delete a folder recursively", which means to delete a folder and all its contents, including other folder, with their respective contents and folders, and so on.

The command del does it for you. Have you read its help or its documentation? To see that, the command you use in the cmd program is:

del /?

But this does not directly answer your question. What you need to put in your batch file is:

del /s /q [non empty folder name]
  1. "/s" is to delete it recursively
  2. "/q" is to delete it without asking for confirmation of each file or folder being deleted. You may want to remove this item, if you want to choose what will be deleted inside the folder.
  3. You must not write the square brackets I wrote in the command example. Just the folder name, as it is.
  • Related