Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all files/folders from a folder 'A' which are not present in the folder 'B', using Windows Batch Scripting?

How to delete all files/folders from a folder 'A' which are not present in the folder 'B', using Windows Batch Scripting?

For example, Folder A has following contents:
Dir1 -> File1
Dir2 (no files)
Dir3 -> File2
Dir4 (no files)
File3
File4
Dir5 -> File5

Folder B has following contents:
Dir3 -> File2
Dir4 (no files)
File3
File5

On running the script, following will be deleted from folder A:
Dir1, along with File1
Dir2
File4
Dir5, along with File5

like image 516
raptor Avatar asked Dec 06 '25 23:12

raptor


1 Answers

robocopy folderB folderA /purge /nocopy

This should remove all the elements in folderA not present in folderB without copying anything from folderB to folderA

like image 95
MC ND Avatar answered Dec 09 '25 11:12

MC ND



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!