KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Constructing the path to a long pathname
PRODUCT: 4D | VERSION: 2003 | PLATFORM: Mac & Win
Published On: June 24, 2004

Suppose you create a document in 4D and you want to place it somewhere on disk. You would have to know exactly where you want to store the document and also know that this location exists. Or, you can create the path to the location you want to place the document.

The following method, called PreparePath, does exactly that:

C_TEXT($1;$folderPath;$createPath;$nextFolder)
C_STRING(1;$dirGlyph)
C_LONGINT($platform)

$folderPath:=$1

PLATFORM PROPERTIES($platform)

If ($folderPath#"")

  ` prepare the paths. For windows, we need to go passed the drive specifier
  If ($platform=3)
    If (Position(":\\";$folderPath)#0)
      $createPath:=Substring($folderPath;0;Position("\\";$folderPath))
      $folderPath:=Substring($folderPath;Position("\\";$folderPath)+1)
    End if
    $dirGlyph:="\\"
  Else
    $dirGlyph:=":"
  End if

  ` if the path does not end with the directory glyph or separator, we add it
  If ($folderPath[[Length($folderPath)]]#$dirGlyph)
    $folderPath:=$folderPath+$dirGlyph
  End if

  Repeat

  ` find the next folder in the directory tree
    $nextFolder:=Substring($folderPath;0;Position($dirGlyph;$folderPath))
    $folderPath:=Substring($folderPath;Position($dirGlyph;$folderPath)+1)

    $createPath:=$createPath+$nextFolder

  ` create the folder if it does not exist
    Case of
      :(Test path name($createPath)<0)
        CREATE FOLDER($createPath)
      :(Test path name($createPath)=1)
        $nextFolder:=""
    End case

  Until ($nextFolder="")

End if

If you called the following method and passed a long path name as in:

PreparePath("C:\\Top Folder\\Middle Folder\\Bottom Folder\\")

It would create those three folders in the C: drive. With this method, you would not have to worry about testing for a location since this method not only tests, but creates the location for you.

Note: If you try creating a path which is actually a path to a file, then the method will not do anything.