home *** CD-ROM | disk | FTP | other *** search
- // PARSE.SCP - Sample Script functions to parse filenames.
- // Copyright (c) 2000, Vector Networks Limited
- // All Rights Reserved
- //
- // Revision History:
- // 5.3 22-Aug-00 AB - Created.
- // 7.0 04-Feb-02 DB - Add debug code to SplitPath.
-
- // If the Script that will use this function does not already INCLUDE RINSTR,
- // uncomment the following line.
- // $INCLUDE "RINSTR.SCP"
-
- // This function takes a file path and splits it down into drive, path, file
- // name and type which it returns as a list. For example, the path
- // "C:\EXAMPLE\FOLDER\FILE.TXT" becomes "C:",
- // "EXAMPLE\FOLDER", "FILE", and "TXT".
-
- Function SplitPath (Path as String) as String
- Dim Drive as String, FileName as String, Type as String
- Dim a, b as Integer, TempList as List
-
- // Print "Splitting path: ", Path
-
- // Find the last "." in the Path string, take the string to the
- // right of this, store it, and then cut it form the string.
-
- a = RInStr (Path, ".")
- b = Len (Path)
-
- If a != 0 Then
- Type = Right (Path, b - a)
- Path = Left (Path, a - 1)
- // Print "Path: ", Path, " Type: ", Type
- Endif
-
- // Now, find the last "\" in the string and get the filename from that.
-
- a = RInStr (Path, "\")
- b = Len (Path)
-
- FileName = Right (Path, b - a)
- Path = Left (Path, a)
- // Print "Path: ", Path, " FileName: ", FileName
-
- // Search for the first : and remove all that is in front of it.
- // This should be the drive letter.
-
- a = InStr (Path, ":")
- b = Len (Path)
-
- If a != 0 Then
- Drive = Left (Path, a)
- Path = Right (Path, b - a)
- // Print "Path: ", Path, " Drive: ", Drive
- Endif
-
- // The Path could have a leading and trailing \.
- // Trim one or both off.
-
- a = InStr (Path, "\")
- b = Len (Path)
-
- If (a = 1) Then
- Path = Right (Path, b - 1)
- Endif
-
- a = RInStr (Path, "\")
- b = Len (Path)
-
- If (a = b) Then
- Path = Left (Path, b - 1)
- Endif
-
- If (Drive != "") Then
- // Print "Drive: ", Drive
- Endif
- If (Path != "") Then
- // Print "Path: ", Path
- Endif
- If (FileName != "") Then
- // Print "FileName:", FileName
- Endif
- If (Type != "") Then
- // Print "Type: ", Type
- Endif
-
- // The Split strings can now be put into a list.
-
- AddItem (TempList, Drive)
- AddItem (TempList, Path)
- AddItem (TempList, FileName)
- AddItem (TempList, Type)
-
- SplitPath = TempList
- End Function
-
- // This function takes two paths (as lists) and creates and parses them.
- // The parts of path1 that are specified will overwirte those in path2.
-
- Function ParsePath (TempList1 as List, TempList2 as List) as String
- Dim Path as String, Path1 as List, Path2 as List
- Dim Item as String, FileName as String, Type as String, Drive as String
-
- // Put the contents of the lists passed to the function into two new
- // lists so that they can be accessed properly.
-
- For Each Item in TempList2 do
- AddItem (Path2, Item)
- Next
-
- For Each Item in TempList1 do
- AddItem (Path1, Item)
- Next
-
- // Take the first items from the lists, use the one specified in
- // Path1 if there is one there, if not use the one form Path2.
-
- If GetItem (Path1, 1) = "" Then
- Drive = GetItem (Path2, 1)
- Else
- Drive = GetItem (Path1, 1)
- Endif
-
- // Do the same for the other items in the list.
-
- If GetItem (Path1, 2) = "" Then
- Path = GetItem (Path2, 2)
- Else
- Path = GetItem (Path1, 2)
- Endif
-
- If GetItem (Path1, 3) = "" Or GetItem (Path1, 3) = "*" Then
- FileName = GetItem (Path2, 3)
- Else
- FileName = GetItem (Path1, 3)
- Endif
-
- If GetItem (Path1, 4) = "" Or GetItem (Path1, 4) = "*" Then
- Type = GetItem (Path2, 4)
- Else
- Type = GetItem (Path1, 4)
- Endif
-
- // Now put the final path together, adding the delimiters
- // and return this value.
-
- ParsePath = Drive + "\" + Path + "\" + FileName + "." + Type
- End Function
-
- // This function parses two string paths, using SplitPath to convert
- // them to lists and then using ParsePath to merge them together.
-
- Function ParseStrPath (Path1 as String, Path2 as String) as String
- Dim Path1List as List, Path2List as List
-
- // Convert the paths to lists so that they can be parsed.
-
- Path1List = SplitPath (Path1)
- Path2List = SplitPath (Path2)
-
- // Return the result
-
- ParseStrPath = ParsePath (Path1List, Path2List)
- End Function
-