KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Create folder hierarchy before exporting and importing table methods
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: December 6, 2018

The 4D command METHOD GET PATHS with method type parameter "Path all objects" can be useful when retrieving all methods (e.g. project, table, form object methods) from the database. However, attempting to export table methods are a bit trickier as they are named with forward slashes and 4D does not allow creating files containing characters associated with file directories (e.g. forward slash for Windows and colon for macOS). The naming convention of exported table methods and error message is shown below:

[tableForm]/{tableName}/{formName}/{objectName}




In order to prevent this error and successfully export table methods, the file name must be replaced with the "/" character with the correct folder separator according to the operating system. For 4D on Windows, the folder separators must be the double backslash "\\" while macOS folder separators must be the colon ":". Afterwards, the folder hierachy must be created using the CREATE FOLDER command before writing the methods to a document. An example of the folder hierarchy from CREATE FOLDER is provided below containing two tables: People and State.



For importing, the table methods files will be retrieved using DOCUMENT LIST, but the file names will be named with folder separators based on the OS as shown below.

Windows:
[tableForm]\\People\\Input\\vRecNum


MacOS:
[tableForm]:People:Input:vRecNum


In order for a successful import, the 4D command METHOD SET CODE needs the file names to be converted back with forward slash "/" separators as shown below.

Convert name back to this format:
[tableForm]/People/Input/vRecNum


For implementation, the utility function is provided below for more context.

// Method: UTIL_Import_Export_All_Methods
// Description
//
// Utiltiy method to import and export all project, table, and form object methods
// ----------------------------------------------------


C_TEXT($root_t;$filePath_t;$fileNameToSet_t;$code_t)
C_LONGINT($i)
C_BLOB($blob_b)
ARRAY TEXT($fileNames_at;0)
ARRAY TEXT($code_at;0)

$root_t:=Get 4D folder(Database folder)+"methods"+Folder separator

CONFIRM("Import or export?";"Export";"Import")

// Export condition
If (OK=1)
  
   // Create "methods" folder if doesn't exists
   If (Test path name($root_T)#Is a folder)
     CREATE FOLDER($root_t;*)
   End if
  
   // Get all object paths and code for each object
   METHOD GET PATHS(Path all objects;$fileNames_at)
   METHOD GET CODE($fileNames_at;$code_at)
  
   // Step through each project or object method
   For ($i;1;Size of array($fileNames_at))
     $filePath_t:=$fileNames_at{$i}
    
     // Update folder separators based on OS
     If (Is macOS)
       $filePath_t:=Replace string($filePath_t;"/";":")
     Else
       $filePath_t:=Replace string($filePath_t;"/";"\\")
     End if
  
     SET BLOB SIZE($blob_b;0)
     TEXT TO BLOB($code_at{$i};$blob_b;UTF8 text without length)
  
     // create directory hierarchy for table form objects
     CREATE FOLDER($root_t+$filePath_t;*)
     BLOB TO DOCUMENT($root_t+$filePath_t;$blob_b)
   End for
  
   ALERT("successfully exported")
  
Else // Import condition
  
   // Get all files within root and sub folders
   DOCUMENT LIST($root_t;$fileNames_at;Recursive parsing)
   For ($i;1;Size of array($fileNames_at))
     $filePath_t:=$fileNames_at{$i}
  
     // $filePath_t needs to have first separator removed for DOCUMENT TO BLOB
     // $fileNameToSet_t needs forward slash separators for METHOD SET CODE
     If (Is macOS)
       $fileNameToSet_t:=Replace string($fileNames_at{$i};":";"/")
       $filePath_t:=Replace string($filePath_t;":";"";1)
     Else
       $fileNameToSet_t:=Replace string($fileNames_at{$i};"\\";"/")
       $filePath_t:=Replace string($filePath_t;"\\";"";1)
     End if
  
     DOCUMENT TO BLOB($root_t+$filePath_t;$blob_b)
     $code_t:=BLOB to text($blob_b;UTF8 text without length)
     METHOD SET CODE($fileNameToSet_t;$code_t)
   End for
  
   ALERT("successfully imported")
  
End if