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

  1. // PARSE.SCP - Sample Script functions to parse filenames.
  2. // Copyright (c) 2000, Vector Networks Limited
  3. // All Rights Reserved
  4. //
  5. // Revision History:
  6. // 5.3 22-Aug-00 AB  - Created.
  7. // 7.0 04-Feb-02 DB  - Add debug code to SplitPath.
  8.  
  9. // If the Script that will use this function does not already INCLUDE RINSTR,
  10. // uncomment the following line.
  11. // $INCLUDE "RINSTR.SCP"
  12.  
  13. // This function takes a file path and splits it down into drive, path, file 
  14. // name and type which it returns as a list.  For example, the path 
  15. // "C:\EXAMPLE\FOLDER\FILE.TXT" becomes "C:", 
  16. // "EXAMPLE\FOLDER", "FILE", and "TXT".
  17.  
  18. Function SplitPath (Path as String) as String
  19.   Dim Drive as String, FileName as String, Type as String
  20.   Dim a, b as Integer, TempList as List
  21.  
  22. //  Print "Splitting path: ", Path
  23.  
  24.   // Find the last "." in the Path string, take the string to the 
  25.   // right of this, store it, and then cut it form the string.
  26.  
  27.   a = RInStr (Path, ".")
  28.   b = Len (Path)
  29.  
  30.   If a != 0 Then
  31.     Type = Right (Path, b - a)
  32.     Path = Left (Path, a - 1)
  33. // Print "Path: ", Path, " Type: ", Type
  34.   Endif
  35.  
  36.   // Now, find the last "\" in the string and get the filename from that.
  37.  
  38.   a = RInStr (Path, "\")
  39.   b = Len (Path)
  40.  
  41.   FileName = Right (Path, b - a)
  42.   Path = Left (Path, a)
  43. // Print "Path: ", Path, " FileName: ", FileName
  44.    
  45.   // Search for the first : and remove all that is in front of it. 
  46.   //  This should be the drive letter.
  47.  
  48.   a = InStr (Path, ":")
  49.   b = Len (Path)
  50.  
  51.   If a != 0 Then
  52.     Drive = Left (Path, a)
  53.     Path = Right (Path, b - a)
  54. // Print "Path: ", Path, " Drive: ", Drive
  55.   Endif
  56.  
  57.   //  The Path could have a leading and trailing \. 
  58.   //  Trim one or both off.
  59.  
  60.   a = InStr (Path, "\")
  61.   b = Len (Path)
  62.  
  63.   If (a = 1) Then 
  64.     Path = Right (Path, b - 1)
  65.   Endif
  66.  
  67.   a = RInStr (Path, "\")
  68.   b = Len (Path)
  69.  
  70.   If (a = b) Then 
  71.     Path = Left (Path, b - 1)
  72.   Endif
  73.  
  74.   If (Drive != "") Then
  75. //  Print "Drive: ", Drive
  76.   Endif
  77.   If (Path != "") Then
  78. //  Print "Path: ", Path
  79.   Endif
  80.   If (FileName != "") Then
  81. //  Print "FileName:", FileName
  82.   Endif
  83.   If (Type != "") Then
  84. //  Print "Type: ", Type
  85.   Endif
  86.  
  87.   // The Split strings can now be put into a list.
  88.  
  89.   AddItem (TempList, Drive)
  90.   AddItem (TempList, Path)
  91.   AddItem (TempList, FileName)
  92.   AddItem (TempList, Type)
  93.  
  94.   SplitPath = TempList
  95. End Function
  96.  
  97. // This function takes two paths (as lists) and creates and parses them.
  98. // The parts of path1 that are specified will overwirte those in path2.
  99.  
  100. Function ParsePath (TempList1 as List, TempList2 as List) as String
  101.   Dim Path as String, Path1 as List, Path2 as List
  102.   Dim Item as String, FileName as String, Type as String, Drive as String
  103.  
  104.   // Put the contents of the lists passed to the function into two new
  105.   // lists so that they can be accessed properly.
  106.  
  107.   For Each Item in TempList2 do
  108.     AddItem (Path2, Item)
  109.   Next
  110.  
  111.   For Each Item in TempList1 do
  112.     AddItem (Path1, Item)    
  113.   Next
  114.  
  115.   // Take the first items from the lists, use the one specified in
  116.   // Path1 if there is one there, if not use the one form Path2.
  117.  
  118.   If GetItem (Path1, 1) = "" Then
  119.     Drive = GetItem (Path2, 1)
  120.   Else
  121.     Drive = GetItem (Path1, 1)
  122.   Endif
  123.     
  124.   // Do the same for the other items in the list.
  125.  
  126.   If GetItem (Path1, 2) = "" Then
  127.     Path = GetItem (Path2, 2)
  128.   Else
  129.     Path = GetItem (Path1, 2)
  130.   Endif
  131.  
  132.   If GetItem (Path1, 3) = "" Or GetItem (Path1, 3) = "*" Then
  133.     FileName = GetItem (Path2, 3)
  134.   Else
  135.     FileName = GetItem (Path1, 3)
  136.   Endif
  137.  
  138.   If GetItem (Path1, 4) = "" Or GetItem (Path1, 4) = "*" Then
  139.     Type = GetItem (Path2, 4)
  140.   Else
  141.     Type = GetItem (Path1, 4)
  142.   Endif
  143.  
  144.   // Now put the final path together, adding the delimiters 
  145.   // and return this value.
  146.  
  147.   ParsePath = Drive + "\" + Path + "\" + FileName + "." + Type
  148. End Function
  149.  
  150. // This function parses two string paths, using SplitPath to convert 
  151. // them to lists and then using ParsePath to merge them together.
  152.  
  153. Function ParseStrPath (Path1 as String, Path2 as String) as String
  154.   Dim Path1List as List, Path2List as List
  155.  
  156.   // Convert the paths to lists so that they can be parsed.
  157.  
  158.   Path1List = SplitPath (Path1)
  159.   Path2List = SplitPath (Path2)
  160.  
  161.   // Return the result
  162.  
  163.   ParseStrPath = ParsePath (Path1List, Path2List)
  164. End Function
  165.