Viral Soni
Viral Soni

Reputation: 1

Batch file to delete sub folder(s) from parent with exception(s)

This is my Batch File Code:

@ECHO ON
cd D:\Parent_Folder
del /s /q D:\Parent_Folder\
rmdir /s /q D:\Parent_Folder\

Parent_Folder:

I want to delete only Abc folder from my Parent Folder but i want to keep xyz.zip file with this code its getting deleted all the data from parent folder.

Upvotes: 0

Views: 451

Answers (1)

lit
lit

Reputation: 16266

If you are willing to do this in PowerShell, you could use the following. When you are satisfied that the correct files and directories would be removed, remove the -WhatIf switch from Remove-Item. Of course, change the directory path and filename to keep to yours.

Get-ChildItem -Path 'C:/src/t/' -Exclude 't.py' |
    Remove-Item -Recurse -Force -WhatIf

If you are desperate to do this from cmd.exe, you could do something like this.

powershell -NoProfile -Command "Get-ChildItem -Path 'C:\src\t' -Exclude 't.py' | Remove-Item -Recurse -Force -WhatIf"

Upvotes: 1

Related Questions