KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Determine the OS of a file path string
PRODUCT: 4D | VERSION: 12.3 | PLATFORM: Mac & Win
Published On: April 8, 2012

When working in a multi-OS environment and processing file or folder path strings it is quite possible that the need will arise to determine to which OS, Mac OS or Windows, the path string is native to.

The method below, Pathname_OSType, will do just that. It uses the specification from Microsoft on how a valid path string begins and uses the 4D command Match regex to test for the presence of the valid path header and position number 1.

If (True)
   If (False)
      Begin SQL
     /*
     Pathname_OSType ($Path_T) -> OSType

     Purpose: Given a path string, determine what is its native OS

     $0 - Longint - purpose
     $1 - TEXT - purpose
     */

      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
     //=============== Declare Variables ===============
     //method_parameters_declarations

   C_LONGINT($0)
   C_TEXT($Path_T;$1)
     //-------------------------------------------------------
     //local_variable_declarations
   C_LONGINT($Pos_L)
   C_TEXT($RegExPat_T)
   C_BOOLEAN($Found_B)
End if
  //=============== Initialize and Setup ===============

$Path_T:=$1

  // msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
  //
  // Valid start for Windows absolute path strings such as:
  // ([a-zA-Z]:\\\\) - A disk designator with a backslash, e.g. "C:\" or "d:\"
  // (\\\\) - A UNC name of any format, starts with two backslash characters ("\\").
  // (
$RegExPat_T:="(([a-zA-Z]:\\\\)|(\\\\))"
  // )

  //=============== Method Actions ===============

$Found_B:=Match regex($RegExPat_T;$Path_T;1;$Pos_L)
If ($Found_B & ($Pos_L=1))
   $0:=Windows
Else
   $0:=Mac OS
End if