batch-filedirectorydelrmdir

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


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. To be clear, I am not asking for a specific "book, tool, or software library"; I just want a Batchfile command to recursively delete a folder. I'm sorry if I'm doing something wrong; I'm pretty new here.


Solution

  • For the previous answer, I checked the current Microsoft documentation about commands it has, and it does NOT say clearly that the del command will delete only files.

    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, but they come AFTER the name of the folder! This is what the command documentation shows, as you may read here.

    But there are 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. To quote the documentation pointed above:

    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.