home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Demo / PCDUO / data1.cab / Script_Samples / FLUPDATE.SCP < prev    next >
Encoding:
Text File  |  2003-11-28  |  10.6 KB  |  263 lines

  1. // FLUPDATE.SCP - Sample Script which udates specified file(s) on Client(s).
  2. // Copyright (c) 2000, Vector Networks Limited
  3. // All Rights Reserved
  4. //
  5. // Revision History:
  6. // 5.3 22-Aug-00 AB - Created.
  7. // 6.0 12-Oct-00 DB  - Trim predefined variables before use.
  8. //                   - Change predefined ClientName for Lookup
  9. //                     from " " to "*".
  10.  
  11. // This Script can be used to ensure that all Clients (or Clients in a Group) 
  12. // have an up to date version of one or more specified files. The filename(s) 
  13. // of the file(s) that need to be checked must be predefined. To pass multiple 
  14. // filenames to the Script, use a vertical bar | to separate the names. Specific 
  15. // Clients or Groups to be checked can also be predefined.
  16.  
  17. $INCLUDE "GTCTLDIR.SCP"
  18. $INCLUDE "PARSE.SCP"
  19. $INCLUDE "RINSTR.SCP"
  20. $INCLUDE "TOKENS.SCP"
  21.  
  22. //  Predefined variables:
  23. //    ClientName = "" Template name for Lookup (use "*" for any)
  24. //    GroupName = "" Selects all Clients from this Group
  25. //    Username = "SCRIPTING/GBBLLYDGKK" Username and password for Connect
  26. //    FileNames = "NSM.LIC|CLIENT32.INI|CONTROLS.NSM" List of files to be checked
  27. //    by the Script.  The names must be separated by a vertical bar (as this cannot be used
  28. //    in filenames).
  29.  
  30. Function Main ()
  31.     Dim ClientName as String, GroupName as String, Username as String
  32.     Dim FileNames as String, FileNameList as List, TempList as List
  33.     Dim Clients as List, ClientCount, FileCopyCount, FileUpdateCount
  34.     Dim CtrlFile as String,, CLFile as String
  35.     Dim Cmp as Integer, Atr as Integer, CtrlDir as String
  36.     Dim x, FilePath as String
  37.  
  38.     SetTransport (T_TCPIP)
  39.  
  40.     // Initialise counter variables
  41.  
  42.     ClientCount = 0
  43.     FileCopyCount = 0
  44.     FileUpdateCount = 0
  45.     x = 0
  46.  
  47.     // Trim predefined variables before use
  48.  
  49.     ClientName = Trim (ClientName)
  50.     GroupName = Trim (GroupName)
  51.     UserName = Trim (UserName)
  52.     FileNames = Trim (FileNames)
  53.  
  54.     // Check the FileNames string to see if it contains a value, 
  55.     // if it does then put the filenames into a list.
  56.  
  57.     If FileNames = "" then 
  58.         Print "No filenames supplied!"
  59.     else
  60.  
  61.         FileNameList = TokenList (FileNames, "|")
  62.         CtrlDir = GetControlDir ()
  63.  
  64.         // Check that the files in the list exist. If one is not there, remove
  65.         // it from the list. x is used to count the number of items in the list
  66.         // so that when all the files in the original list have been searched for,
  67.         // the Script can be sure that at least one valid file name has been
  68.         // supplied. The Items function could be used to do this, but if there
  69.         // were no valid filenames supplied, the list is empty and Items would 
  70.         // cause an error.
  71.  
  72.         For Each File in FileNameList
  73.             FilePath = CtrlDir + "\" + File
  74.             If FileExists (FilePath) Then
  75.                 AddItem (TempList, File)
  76.                 x = x + 1
  77.             Else
  78.                 Print "File ", FilePath, " not found!"
  79.             Endif
  80.         Next
  81.         
  82.         If x = 0 Then
  83.             Print "Specified files not found!"
  84.         Else
  85.             FileNameList = TempList
  86.  
  87.             // If GroupName is defined, get all the Clients in the Group
  88.  
  89.                 If GroupName != "" Then
  90.                 Print "Selecting all Clients from Group: ", GroupName
  91.                 GetClientsInGroup (GroupName, Clients)
  92.             Else
  93.  
  94.                 // If ClientName is defined, see if there are any 
  95.                 // Clients running which have matching names
  96.                 // Use ClientName = "*" to get all Available Clients
  97.                 // Otherwise, get all of the Known Clients
  98.  
  99.                 If ClientName != "" Then
  100.                     If ClientName != "*" Then
  101.                         Print "Browsing for Clients matching name: ", ClientName
  102.                     Else
  103.                         ClientName = ""
  104.                         Print "Browsing for Available Clients"
  105.                     Endif
  106.                     Lookup (ClientName, Clients)
  107.                 Else
  108.                     Print "Selecting all Known Clients"
  109.                     GetAllClients (Clients)
  110.                 Endif
  111.             Endif
  112.  
  113.             // Make sure that at least one Client has been found.
  114.  
  115.             If Items (Clients) > 0 then
  116.  
  117.                 // Execute the following loop on all Clients found.
  118.  
  119.                 For each CClient in Clients
  120.                     Trim (CClient)
  121.  
  122.                     Print "----------------------------------------------------"
  123.                     Print "Trying Client ", GetClientName (CClient), "..."
  124.  
  125.                     // Attempt to connect to the Client.
  126.  
  127.                     If Connect (CClient, UserName) then
  128.                         ClientCount = ClientCount + 1
  129.  
  130.                         Print "Connected to Client ", ClientCount, " ", GetClientName (CClient)
  131.  
  132.                         // Go through the FilenameList list, 
  133.                         // comparing all the files in the list.
  134.  
  135.                         For Each CtrlFile in FilenameList
  136.  
  137.                             // Use GetInstallDir to discover the location of the
  138.                             // required file and to create the path for the temp
  139.                             // file that the Client's file will be renamed to if it is
  140.                             // out of date.
  141.  
  142.                             ClFile = GetInstallDir ("") + "\" + CtrlFile
  143.                             TmpFile = ParseStrPath ("*.OLD", ClFile)
  144.                             ClFile = ">" + ClFile
  145.                             CtrlFile = CtrlDir + "\" + CtrlFile
  146.  
  147.                             // Compare the files
  148.  
  149.                             Cmp = FileCompare (ClFile, CtrlFile)
  150.  
  151.                             // If 1 is returned then the Client's file has the 
  152.                             // wrong attributes. They will be corrected.
  153.  
  154.                             If Cmp = 1 Then
  155.                                 Print "Changing Client file's attributes."
  156.                                 Atr = GetFileInfo (CtrlFile, FI_ATTRIB)
  157.                                 SetAttrib (ClFile, Atr)
  158.                             Endif
  159.  
  160.                             // If 5 is returned then the Client's file does not exist
  161.                             // (because the Control's was checked earlier).  
  162.                             // The file will be copied across from the Control.
  163.  
  164.                             If Cmp = 5 Then
  165.                                 Print "Client does not have the file, copying the Control's."
  166.                                 Copy (CtrlFile, ClFile)
  167.                                 FileCopyCount = FileCopyCount + 1
  168.                             Endif
  169.  
  170.                             // If the files are different, rename the Client's file
  171.                             // to a temporary .OLD file and copy the Control's file
  172.                             // to the Client. If the file is copied successfully, delete 
  173.                             // the temporary file, if not then rename the .OLD file 
  174.                             // back to its orriginal name.
  175.  
  176.                             If Cmp > 1 and Cmp <5 Then
  177.                                 Print "Client's file is different"
  178.                                 Print "Renaming ", ClFile, " to ", TmpFile
  179.                                 If FileExists (">" + TmpFile) Then
  180.                                     Delete (">" + TmpFile)
  181.                                 Endif
  182.                                 If Rename (ClFile, TmpFile) Then
  183.                                     If Copy (CtrlFile, ClFile) Then
  184.                                         Print "Client's file successfully updated."
  185.                                         FileUpdateCount = FileUpdateCount + 1
  186.                                     Else
  187.                                         Print "Could not copy the Control's file, replacing original."
  188.                                         Rename (TmpFile, ClFile)
  189.                                     Endif
  190.                                 Else
  191.                                     Print "Could not rename Client's file"
  192.                                 Endif
  193.                             Endif
  194.                         Next
  195.     
  196.                         // Disconnect and move onto the next Client
  197.  
  198.                         Disconnect (CClient) 
  199.                     Else
  200.                         Print "Could not connect to Client ", GetClientName (CCLient)
  201.                     Endif
  202.                 Next
  203.             Else
  204.                 Print "No Clients were found!"
  205.             Endif
  206.  
  207.             // Print a summary
  208.  
  209.             Print "Number of Clients found: ", ClientCount
  210.             Print "  Number of files copied: ", FileCopyCount
  211.             Print "  Number of files updated: ", FileUpdateCount
  212.  
  213.         Endif
  214.     Endif
  215. End Function
  216.  
  217. // This function compares two files to check for differences. If the files have the same
  218. // size, date, time and attributes the function returns 0. If one of these is different from
  219. // the other file, a number between 1 and 4 is returned, depending on the value that is
  220. // different. If one or both of the files don't exist, 5 is returned.
  221.  
  222. Function FileCompare (FName as String, F2Name as String) as Integer
  223.     Dim a as Integer, b as Integer
  224.     Dim c as String, d as String
  225.  
  226.     FileCompare = 0
  227.  
  228.     If FileExists (FName) and FileExists (F2Name) Then
  229.         a = GetFileInfo (FName, FI_SIZE)
  230.         b = GetFileInfo (F2Name, FI_SIZE)
  231.         If a = b Then
  232.             a = GetFileInfo (FName, FI_DATE)
  233.             b = GetFileInfo (F2Name, FI_DATE)
  234.             If a = b Then
  235.                 a = GetFileInfo (FName, FI_TIME)
  236.                 b = GetFileInfo (F2Name, FI_TIME)
  237.                 If a = b Then
  238.                     c = GetFileInfo (FName, FI_ATTRIB)
  239.                     d = GetFileInfo (F2Name, FI_ATTRIB)
  240.                     If c = d Then 
  241.                         Print "The files are the same."
  242.                     Else
  243.                         Print "The file attributes are different"
  244.                         FileCompare = 1
  245.                     Endif
  246.                 Else
  247.                     Print "The files have different time stamps."
  248.                     FileCompare = 2
  249.                 Endif
  250.             Else
  251.                 Print "The files have different dates"
  252.                 FileCompare = 3
  253.             Endif
  254.         Else
  255.             Print "The files are different sizes."
  256.             FileCompare = 4
  257.         Endif
  258.     Else
  259.         Print "One (or both) of the files does not exist."
  260.         FileCompare = 5
  261.     Endif
  262. End Function
  263.