Batch command to delete all subfolders with a specific name

前端 未结 3 1234
一个人的身影
一个人的身影 2021-01-30 09:00

I have a directory as such:

D:\\Movies
D:\\Movies\\MovieTitle1\\backdrops\\
D:\\Movies\\MovieTitle2\\backdrops\\
D:\\Movies\\MovieTitle3\\backdrops\\
D:\\Movies\         


        
3条回答
  •  无人共我
    2021-01-30 09:33

    I will open a different answer, because it would be too cramped in the comments. It was asked what to do, if you want to execute from/to a different folder and I want to give an example for non-recursive deletion.

    First of all, when you use the command in cmd, you have to use %d, but when you use it in a .bat, you have to use %%d.

    You can use a wildcard to just process folders that for example start with "backdrops": "backdrops*".

    Recursive deletion of folders starting in the folder the .bat is in:

    FOR /d /r . %d IN ("backdrops") DO @IF EXIST "%d" rd /s /q "%d"

    Non-recursive deletion of folders in the folder the .bat is in (used with wildcard, as you cannot have more than one folder with the same name anyway):

    FOR /d %d IN ("backdrops*") DO @IF EXIST "%d" rd /s /q "%d"


    Recursive deletion of folders starting in the folder of your choice:

    FOR /d /r "PATH_TO_FOLDER" %d IN ("backdrops") DO @IF EXIST "%d" rd /s /q "%d"

    Non-recursive deletion of folders in the folder of your choice (used with wildcard, as you cannot have more than one folder with the same name anyway):

    FOR /d %d IN ("PATH_TO_FOLDER/backdrops*") DO @IF EXIST "%d" rd /s /q "%d"

提交回复
热议问题