Nataraj
Nataraj

Reputation: 942

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

I am looking for a way to delete all files in folder but except one folder through windows batch script..can you please help me on this.

Upvotes: 2

Views: 9515

Answers (3)

sudhakaranR87
sudhakaranR87

Reputation: 210

Test this - it should work.

For EX:To remove all files and folder except cd folder inside abc

pushd "d:\abc\cd" && rd /s /q "d:\abc" 2>nul

Upvotes: 5

sudhakaranR87
sudhakaranR87

Reputation: 210

In windows powershell, Use the below command to remove all files and folder except one

Remove-Item c:\<Path to Folder>\* -exclude *<folder you don't want to remove>*

Upvotes: 2

Ashish Negi
Ashish Negi

Reputation: 5301

This lists all the files (and only the files) in the current directory:

for /r %%i in (*) do echo %%i

to delete a folder you can do:

RMDIR \"FOLDERNAME" /S /Q

you can compare two values in windows batch file as:

 if NOT "%SubFolderName%" == "folder-name-you-do-not-want-to-delete"
         // delete the folder.

I hope you can connect the dots...

Upvotes: 1

Related Questions