3

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

raptor
  • 33
  • 1
  • 4

1 Answers1

3
robocopy folderB folderA /purge /nocopy

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

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thankyou! Did the job! :) If I don't add [/nocopy], will it make the two folders identical, without disturbing anything in folderB? – raptor Jun 18 '14 at 12:21
  • @user2494926 to make the two folders identical use `/mir` to mirror source. Anyway, source is never disturbed. All the changes are done in target. – MC ND Jun 18 '14 at 12:23
  • Will "robocopy folderB folderA /purge" and "robocopy folderB folderA /mir" do exactly same things? I ran both commands, didn't notice any difference. Sorry, I'm new to windows batch commands. – raptor Jun 18 '14 at 12:30
  • @raptor, `/mir` = `/purge` + `/e` , that is, `/mir` include recursion over subfolders. – MC ND Jun 18 '14 at 12:37
  • Alright, got it! Should have read the robocopy help earlier. Thanks! – raptor Jun 18 '14 at 12:44