KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Storing Text Documents in a Local Method for Later Use
PRODUCT: 4D | VERSION: 2003 | PLATFORM: Mac & Win
Published On: July 15, 2004

Sometimes when you are referring to external scripts in 4D, you will not want to rely on their being there. For example, if you are using AP Sublaunch to run a VBScript called Example.vbs, you might not be certain that this script exists at all times.

There are a couple solutions you can try: store the file as a blob in the data file or generate the script through a method in 4D. This example uses the latter. If you have a script called "Example.vbs" that looked like the following

This
is
a
Test

You can use this method to read in the text document so that it can be stored in a 4D project method.

ChangeToQuotes("$text")

The method asks you to select a document that you would like to insert and after you have chosen one, places the result into the clipboard. The below code is the result of calling the method on the Example.vbs file:

C_BLOB($blobContainer)
$text:=$text+"This"+Char(Carriage return )
$text:=$text+"is"+Char(Carriage return )
$text:=$text+"a"+Char(Carriage return )
$text:=$text+"Test"+Char(Carriage return )
TEXT TO BLOB($text;$blobContainer;Text without length;*)

Now that the script is stored in a local method, you can export the script into whatever format you want. It can be beneficial to use this technique in a case where you want your application to generate text files that it may depend on without relying on external files or the database's data file. Below is the entire ChangeToQuotes method.

C_TEXT($1;$variableName;$line;$docPath;$prepend)
C_BOOLEAN($isDone)
C_BLOB($blob)
C_LONGINT($characterCount;$currentCount)

$variableName:=$1

$docRef:=Open document($docPath)
    $docPath:=Document

If ($docPath#"")
    `$docPath:=$documents{1}
 
  TEXT TO BLOB("C_BLOB($blobContainer)"+Char(Carriage return );$blob;Text without length ;*)

  Repeat
    RECEIVE PACKET($docRef;$line;Char(Carriage return ))

    If (OK=0)
      $isDone:=True
    End if

    $characterCount:=Length($line)

    If (($currentCount+$characterCount)>20000)
      TEXT TO BLOB("TEXT TO BLOB("+$variableName+";$blobContainer;Text without length;*)"+Char(Carriage return );$blob;Text without length ;*)
      $currentCount:=0
    Else
      $currentCount:=$currentCount+$characterCount
    End if

    $line:=Replace string($line;"\\";"\\\\")
    $line:=Replace string($line;Char(Double quote );"\\"+,Char(Double quote ))

    If ($currentCount>1)
      $prepend:=$variableName+":="+$variableName+"+"
    Else
      $prepend:=$variableName+":="
    End if

    $line:=$prepend+Char(Double quote )+$line+Char(Double quote )+"+Char(Carriage return )"+Char(Carriage return )

    TEXT TO BLOB($line;$blob;Text without length ;*)

  Until ($isDone)

  TEXT TO BLOB("TEXT TO BLOB("+$variableName+";$blobContainer;Text without length;*)";$blob;Text without length ;*)

  CLOSE DOCUMENT($docRef)
  CLEAR CLIPBOARD
  APPEND TO CLIPBOARD("TEXT";$blob)
End if