Delete old files on the client
Here's a script that deletes files in a template directory that the user computer is not present in one of the file groups is copied. For clarity, the code is at the end of this answer. Here's how to use a script in your current solution that does not use /MIR .
In the code for each group copied, add another method call to "ListFiles" - this keeps track of the files copied from the server:
If User is MemberOf (group3) Then RoboCopy.exe \\fileserver\templates\group3\ c:\templates\workgroup *.* /E /XO ListFiles("\\fileserver\templates\group3\", userTemplates) End if
Do this for each group copied. (This is normal if the same pattern appears in multiple groups.)
After all the groups have been copied, you will add this code block:
ListFiles "c:\templates\workgroup", toDelete removeAllFrom toDelete, userTemplates
All files in the local user templates folder on toDelete . All newly copied files are then deleted from this set, leaving only files that were not copied from the server. Then we can print the files for deletion and then delete them.
echoDictionary "deleting old user templates", toDelete ' deleteFiles c:\templates\workgroup", toDelete
The call to the deleteFiles method is commented out - it might be wise to make a test run first! The first argument to deleteFiles is the user template directory β it should not have a trailing slash.
With these changes, all files in the templates folder on the user's computer that were not copied from the server will be deleted, providing effective multi-directory synchronization.
Now comes the script. The first block can be inserted at the top of your file, and the rest at the bottom to avoid clutter.
// script to remove files not present on one of the group folders on the fileserver Set fs = CreateObject("Scripting.FileSystemObject") Set userTemplates = CreateObject("Scripting.Dictionary") userTemplates.CompareMode = 1 Set toDelete = CreateObject("Scripting.Dictionary") toDelete.CompareMode = 1 -- under here are just procedures so they can go at -- the bottom of your script if desired Sub deleteFiles(basedir, dictionary) for each key in dictionary.Keys fs.DeleteFile(basedir+"\"+key) next End Sub Sub echoDictionary(msg, dictionary) for each key in dictionary.Keys Wscript.Echo msg & ": " & key next End Sub Sub removeAllFrom(target, toRemove) for each key in toRemove.Keys if target.Exists(key) then target.remove key end if next End Sub Sub ListFiles(folderName, dictionary) Set folder = fs.GetFolder(folderName) ListSubFolders folder, "", dictionary End Sub Sub ListSubFolders(folder, prefix, dictionary) Set files = folder.Files For Each file in files qualifiedName = prefix & file.Name dictionary.add qualifiedName, file Next For Each Subfolder in Folder.SubFolders qualifiedName = prefix+Subfolder.Name & "\" ListSubFolders Subfolder, qualifiedName, dictionary dictionary.add qualifiedName, Subfolder Next End Sub