Tech Tip: File path organizer
PRODUCT: 4D | VERSION: 12.x/13.x/14.x/15.x | PLATFORM: Mac & Win
Published On: August 26, 2016
When having to save a number of external files and having to access them from multiple places in a datbase application it is very efficient and easily maintained if all the file paths are accessed from one project method instead of coded over and over again throughout the application.
The example project method below is an example of how all the file paths can accessed accessed from one source. And when the need comes to move files and folders around, the new paths only have to be updated in one loccation.
Getting a file path is as simple as...
C_TEXT($Path_T) $Path_T:=My_GetFilePath("Objects") If($Path_T#"") $Path_T:=$Path_T+"MyJSON.json" End if |
The project method below was written with a component in mind so the command Get 4D Folder contains the optional "*" parameter to get the Resources folder of the host and not of the component.
If (True) // Name: My_GetFilePath // Path: My_GetFilePath // Purpose: Return a file path in the My_DataFiles folder // $0 - TEXT - Returned file path // $1 - TEXT - Path name C_TEXT($MethodName_T) $MethodName_T:=Current method name //===================== Declare Variables ================================== //method_parameters_declarations C_TEXT($0;$Path_T) C_TEXT($PathName_T;$1) //-------------------------------------------------------------------------- //local_variable_declarations C_LONGINT($Ndx;$SOA;$RIS;$Params_L) End if //====================== Initialize and Setup ================================ $Params_L:=Count parameters $PathName_T:=$1 $Path_T:="" //======================== Method Actions ================================== Case of : ($PathName_T="My_Templates") $Path_T:=Get 4D folder(Current resources folder;*)+\ "My_Templates"+Folder separator : ($PathName_T="Objects") $Path_T:=Get 4D folder(Current resources folder;*)+\ "My_Templates"+Folder separator+\ "Objects"+Folder separator : ($PathName_T="Scripts") $Path_T:=Get 4D folder(Current resources folder;*)+\ "My_Templates"+Folder separator+\ "Scripts"+Folder separator : ($PathName_T="User Settings") $Path_T:=Get 4D folder(Current resources folder;*)+\ "My_Templates"+Folder separator+\ "Objects"+Folder separator+\ "User Settings"+Folder separator : ($PathName_T="") Else ASSERT(False;"Unknown case test!") End case If ($Path_T#"") If (Test path name($Path_T)#Is a folder) // Star parameter to create full folder hierarchy // CREATE FOLDER($Path_T;*) End if End if //======================== Clean up and Exit ================================= $0:=$Path_T |