Search code examples
windowscmdbatch-processingxcopywindows-scripting

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


Solution

  • robocopy folderB folderA /purge /nocopy
    

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