How to delete all the folder except one folder and its content using command prompt?

Nikunj Parikh

Say for example I have a folder abc containing sub folders 1, 2, 3, 4. Now I want to delete all the folders except folder 2 and its content. I have tried

PUSHD (c:\abc\2) 
rd /s /q "C:\abc" 2>nul

But it deletes the files inside the 2 folder also. I don't want any of the files of folder 2 deleted?

aschipfl

The following code should work:

for /D %%D in ("C:\abc\*.*") do (
    if /I not "%%~nxD"=="2" (
        2> nul rd /S /Q "%%~fD"
    )
)

The for /D loop walks through the directories 1, 2, 3, 4.
The if statement checks the name of the currently iterated directory not to be 2.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete all files and folder except two folders using command prompt

From Dev

Deleting a all files in a folder except one folder in windows command prompt or batch script?

From Dev

how delete All files except one in folder with php?

From Dev

How do I move all files from one folder to a subfolder except .html file using the command line?

From Dev

Gitignore all except one folder and all its content - regardless of the nesting level

From Dev

delete a folder and its content

From Dev

Deny access to all content except content from one folder

From Dev

Windows batch command to delete files from a folder except one file

From Dev

Linux command to delete all files except .git folder?

From Dev

Inno Setup [UninstallDelete] delete all except one folder

From Dev

Delete all folders inside a folder except one with specific name

From Dev

Delete all files in the folder except one extension(say .idf) using batch file

From Dev

Changing owner of all folder except one folder

From Dev

How to Delete all folders inside a folder except one in windows batch script?

From Dev

How to delete all files and directory except one named directory from a specific folder in centos

From Dev

How to rename my folder and any of its subfolders into lowercase using command prompt in Windows?

From Dev

Delete all files in folder except file in list using batch

From Dev

Delete all files in folder except file in list using batch

From Dev

how to delete a folder using linux command line

From Dev

Is it possible to delete all content of a folder?

From Dev

how to I change permissions to all subdirectories except for 1 folder using a single find command?

From Dev

How to copy folder structure and all files except one file type

From Dev

Delete all in folder except the filename in list

From Dev

Delete all in folder except the filename in list

From Dev

How to delete all except for some specific folder under parent-folder

From Dev

how to hide a folder in windows7 using command prompt?

From Dev

Restore all files except one folder

From Dev

How to prompt a folder using cmd?

From Dev

glob for all folders within a folder except one named folder

Related Related

  1. 1

    Delete all files and folder except two folders using command prompt

  2. 2

    Deleting a all files in a folder except one folder in windows command prompt or batch script?

  3. 3

    how delete All files except one in folder with php?

  4. 4

    How do I move all files from one folder to a subfolder except .html file using the command line?

  5. 5

    Gitignore all except one folder and all its content - regardless of the nesting level

  6. 6

    delete a folder and its content

  7. 7

    Deny access to all content except content from one folder

  8. 8

    Windows batch command to delete files from a folder except one file

  9. 9

    Linux command to delete all files except .git folder?

  10. 10

    Inno Setup [UninstallDelete] delete all except one folder

  11. 11

    Delete all folders inside a folder except one with specific name

  12. 12

    Delete all files in the folder except one extension(say .idf) using batch file

  13. 13

    Changing owner of all folder except one folder

  14. 14

    How to Delete all folders inside a folder except one in windows batch script?

  15. 15

    How to delete all files and directory except one named directory from a specific folder in centos

  16. 16

    How to rename my folder and any of its subfolders into lowercase using command prompt in Windows?

  17. 17

    Delete all files in folder except file in list using batch

  18. 18

    Delete all files in folder except file in list using batch

  19. 19

    how to delete a folder using linux command line

  20. 20

    Is it possible to delete all content of a folder?

  21. 21

    how to I change permissions to all subdirectories except for 1 folder using a single find command?

  22. 22

    How to copy folder structure and all files except one file type

  23. 23

    Delete all in folder except the filename in list

  24. 24

    Delete all in folder except the filename in list

  25. 25

    How to delete all except for some specific folder under parent-folder

  26. 26

    how to hide a folder in windows7 using command prompt?

  27. 27

    Restore all files except one folder

  28. 28

    How to prompt a folder using cmd?

  29. 29

    glob for all folders within a folder except one named folder

HotTag

Archive