Vbscript - create a script that displays multiple sets of folders

Ok, this is my problem. I am doing logonscript, which basically copies Microsoft Word templates from the server path to the local path of each computer. This is done by checking group membership.

If MemberOf(ObjGroupDict, "g_group1") Then oShell.Run "%comspec% /c %LOGONSERVER%\SYSVOL\mydomain.com\scripts\ROBOCOPY \\server\Templates\Group1\OFFICE2003\ " & TemplateFolder & "\" & " * /E /XO", 0, True End If 

I used to use the / MIR robocopy switch, which is great. But if the user is in more than one group, the / MIR switch deletes the content from the first group, because it reflects the content from the second group. Meaning, I cannot have both contents.

This is "allowed" by not using the / MIR switch and just letting the content be copied anyway. BUT the whole idea of ​​having templates on the server is that I can control the content that users get through the script. Therefore, if I delete a file or folder from the server path, this will not be replicated on the local computer. Since I no longer use the / MIR switch. Comprende?

So what should I do? I made a small script that basically checks folders and files and then deletes them accordingly, but actually it turned out to be the same functionality as the / MIR switch. How to solve this problem?

Edit: I found that I really need a program that scans my local template folder for files and folders and checks if the same structure exists in any folder of the original template.

Server template folders are configured as follows:

 \\fileserver\templates\group1\ \\fileserver\templates\group2\ \\fileserver\templates\group3\ \\fileserver\templates\group4\ \\fileserver\templates\group5\ \\fileserver\templates\group6\ 

And the script that does the copying is such structures (pseudo):

 If User is MemberOf (group1) Then RoboCopy.exe \\fileserver\templates\group1\ c:\templates\workgroup *.* /E /XO End if If User is MemberOf (group2) Then RoboCopy.exe \\fileserver\templates\group2\ c:\templates\workgroup *.* /E /XO End if If User is MemberOf (group3) Then RoboCopy.exe \\fileserver\templates\group3\ c:\templates\workgroup *.* /E /XO End if 

Etc etc. With the / E switch, I'm sure it also copies the subfolders. And the / XO switch only copies files and folders that are newer than those that are in my local path. But it does not take into account whether the local path contains files or folders that do not exist on the server template path.

So, after copying is complete, I would like to check if any of the files or folders on my c: \ templates \ workgroup really exist in any of the sources. And if they do not, remove them from my local path. Something that can be combined in these elements, perhaps?

+4
source share
2 answers

Using the lookup table

I would suggest an approach that puts all the templates in one common file server directory and uses a lookup table to assign templates to groups.

The benefit will be that your templates are guaranteed to be in sync; that is, you don’t have to worry about the fact that the template for groups A, B and C is actually the same in all group folders on your file server.

Another bonus is the supported configuration table, which allows you to assign templates to groups without having to make changes to your script login.

The lookup table configuration file would look like

 group1;\templateA.dot;\templateA.dot group2;\B\templateB.dot;\B\templateB.dot group3;\B\C\templateC.dot;\templateC.dot 

Column 1 listing your AD group names column 2 is the source path and column 3 is the target path. This will also smooth out the client-side templates folder.

In any case, you can avoid the need to store several copies of all your templates on your file server, and adding more groups or templates does not require touching your script login, but only the configuration file.

In your logon script you can iterate over all lines and copy them with the appropriate groups

Input script code

 open lookup table config file For Each line In lookup table If MemberOf(ObjGroupDict, groupname_column_value) Then execute Robocopy templatename_column_value local_target End If Next 
+2
source

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 
+1
source

Source: https://habr.com/ru/post/1310465/


All Articles