KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Folder Separators and Escape Characters
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: March 2, 2018

Folder separators and escape characters often necessary and are useful when developing an application to ensure the code is robust and flexible. Below is a brief discussion of the use cases for folder separators and escape characters within 4D applications.

Folder Separator
Often times when developing an application it is necessary for that application to be used on both MAC and WINDOWS platforms. On a MAC, the file paths are separated by a colon, for example a path on a MAC to the resources folder of a 4D structure may look like this:

Macintosh HD:Users:kme:Desktop:DEV_Apps:myApp.4dbase:Resources:

The same path on a WINDOWS machine may look like this:

C:\\Users\\kme\\Desktop\\DEV_Apps\\myApp.4dbase\\Resources\\

The thing to notice about these two paths, apart from that they are different, is that the folder separators for MAC are ":" and for WINDOWS are "\\". Thinking about this, it may seem obvious the solution would look something like this:

Case of
  : ($platform="WINDOWS")
    $path:=Get 4D folder(Current resources folder)\
      +"text_files"+"\\"+"TestFile.txt"

  : ($platform="MAC")
    $path:=Get 4D folder(Current resources folder)\
      +"text_files"+":"+"TestFile.txt"
End case

But instead of the application determining what separator to use, let 4D do it by using the folder separator constant in place of a concatenation of strings:

$path:=Get 4D folder(Current resources folder)\
  +"text_files"+Folder separator+"TestFile.txt"

Escape Character
When concatenating strings, often times it is necessary to include quotes inside a string. For example consider this text string:

I went to say hello but instead I said "hey".

If putting this string into a variable, there are two ways to include the quotes around the word 'hey'.

Method 1 (use 4D command Char):

$str:="I went to say hello but instead I said "+Char(34)+"hey"+Char(34)+"."

Though Char(34) may represent the escape character currently, which allows for the quotes inside of the string, it is possible that in the future this will change! To be safe, use the below method instead.

Method 2 (use escape char "\"):

$str:="I went to say hello but instead I said \"hey\"."

The use of the escape character "\" works better and is cleaner looking code than the concatenation of the Char(34) character.