Creating file with a Constant

Hi,

I’m trying to create a external file using Constant which has a text script.

    If Documents <> Nil Then
      
      ListSourceSplitArray = Split(FileListArray(i), ",")   
      
      Dim f As FolderItem = Documents.Child(ListSourceSplitArray(1))
      
      If f <> Nil Then
        Try
          Dim t As TextOutputStream = TextOutputStream.Create(f)
          Dim FindConstantName As String
          FindConstantName = ListSourceSplitArray(0)
          
          t.Write(FindConstantName)
          t = Nil
        Catch e As IOException
          //handle
        End Try
      End If
    End If

Constant Name : ListSourceSplitArray(0)
I save the name of constant into ListSourceSplitArray(0) and thought I can write the contents of the Constant by the code : t.Write(FindConstantName).
However, I just see the file naming the Constant name instead of its contents.

Can anyone let me know what it is wrong?

You can’t reference a constant by a string. One possibility is to use a select-case statement and iterate through the constants:

select case FindConstantName Case "one" t.write(constantOne) Case "two" t.write(constantTwo) End Select

I didn’t know that. Let me try it first.

Thank you so much.

There are other ways to do this too.

Include in your project the files in a folder with names like “constantone.txt”, “constanttwo.txt”, etc. Copy the right file to the destination at runtime.

Create one constant and set it’s content to JSON, something like this:

{
  "constantone" : "Contents of file 1" ,
  "constanttwo" : "Contents of file 2"
}

At runtime, read the constant into a JSONItem or Dictionary, then use the “constant” names as keys.

Personally, I’d use the first idea.

Kem Tekinay:

For the first idea, those external files should be in windows directory?
I’m trying to hide my script and want to copy those ones into the special directory when it is needed in Shell object.

Thanks.

For the way of using Constant, I noticed that the file created by Constant lacks of LF.
'chr(10) is a Line Feed (LF): ASCII Code 10

Is there any way to add LF to each line after CR? That’s CRLF…

Use ReplaceLineEndings:

myText = ReplaceLineEndings( kConstant, EndOfLine.Windows )

You can use a build script to copy the folder of your files into the Resources folder of the project. That way you’ll know where to find them.

Another idea: Build a SQL database one table of two files: name, content and include that with the project. You can encrypt the database if you want to thwart casual browsing of the files.

Constant way worked well with ReplaceLineEndings.

Thank you so much.