Text files [examples]

Read a file into a text string
FILETOSTR
myString = filetostr("myfile.txt")

https://www.lianja.com/doc/index.php/FILETOSTR()


Write a text string out to a file
STRTOFILE
myString = "Hello World"
 
// Create file if it does not exist, overwrite if it does
nBytes = strtofile(mystring, "myfile.txt")
 
// Append to file's previous contents
nBytes = strtofile(mystring, "myfile.txt",.T.)
 
// Create file if it does not exist, overwrite if it does
nBytes = strtofile(mystring, "myfile.txt",0)
 
// Append to file's previous contents
nBytes = strtofile(mystring, "myfile.txt",1)
 
// Create file if it does not exist, overwrite if it does.  Include Unicode BOM
nBytes = strtofile(mystring, "myfile.txt",2)
 
// Create file if it does not exist, overwrite if it does.  Include UTF-8 BOM
nBytes = strtofile(mystring, "myfile.txt",4)

https://www.lianja.com/doc/index.php/STRTOFILE()


Return the value from a key in a specified section of an ‘ini’ file
INI_GET
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
key1value = ini_get('global','key1','unset','test.ini',.T.)

https://www.lianja.com/doc/index.php/INI_GET()


Return all the key/value pairs from a specified section of an ‘ini’ file
INI_GETSECTION
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
obj_global = ini_getsection('global','test.ini')
? obj_global.key3
This is $(key2) and $(key1)

https://www.lianja.com/doc/index.php/INI_GETSECTION()


Set the value of a key in a specified section of an ‘ini’ file
INI_SET
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
ini_set('global','key1','new value','test.ini')

https://www.lianja.com/doc/index.php/INI_SET()


Set all the key/value pairs of a specified section of an ‘ini’ file
INI_SETSECTION
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
obj_global = ini_getsection('global','test.ini')
? obj_global.key3
This is $(key2) and $(key1)
obj_global.key3 = 'value3'
ini_setsection(obj_global,'global','test.ini')
? ini_get('global','key3','','test.ini')
value3

https://www.lianja.com/doc/index.php/INI_SETSECTION()


Enables or disable the inclusion of field name headings in COPY TO … TYPE CSV exports
SET CSVHEADING
open database southwind
use products
set csvheading off
copy to exportproducts type CSV delimited with '|'
type exportproducts.csv

https://www.lianja.com/doc/index.php/SET_CSVHEADING


Copy a memo field into a file
COPY MEMO
seek "JimL"
do while emp_code = "JimL"
    copy memo notes to comments additive
    skip
enddo

https://www.lianja.com/doc/index.php/COPY_MEMO


Capture output text in a file
SET ALTERNATE
set alternate to alt
? "Write this text to file"
set alternate off
? "Don’t write this text to file"
?
set console off
? "write this to file to the file, not to the screen"
close alternate
set console on
return

https://www.lianja.com/doc/index.php/SET_ALTERNATE


Enable/disable text merging, specify text merging target or specify text merge delimiting characters
SET TEXTMERGE
open database southwind
use example
set textmerge to balance.txt
set textmerge delimiters to "{{","}}"
set textmerge on
go top
do while balance > 0
    text noshow
    Date: {{date()}}
    Name: {{last_name}}  {{first_name}}
 
    Account Number  :  {{account_no}}
    Current Balance  :   {{balance}}
    Credit Limit:        :   {{limit}}
    endtext
    skip
enddo
set textmerge off
set textmerge to

https://www.lianja.com/doc/index.php/SET_TEXTMERGE


Evaluate a character string, merging any delimited expressions
TEXTMERGE
fd = fcreate("template.txt")
fputs(fd,"Dear <<trim(first_name)>>,")
fputs(fd, "Thank you for your email of <<date()>>.")
fputs(fd, "We will be back in contact shortly.")
fputs(fd, "Best regards,")
fputs(fd, "<<username()>>")
fclose(fd)
 
m_contents = filetostr("template.txt")
open database southwind
use example in 0 current
 
?textmerge(m_contents)

https://www.lianja.com/doc/index.php/TEXTMERGE()


Write a string with ‘C’ style picture formatting to a file opened with the specified file pointer
FPRINTF

 

fp=fcreate('fprintf.txt')
// When %s is specified, the corresponding argument is converted to 
// character format (similar to specifying etos()).
// Widths correspond to the default values, e.g. numerics are 10
fprintf(fp,'It is %s, %s to be more precise\n',year(date()),datetime())
fprintf(fp,'The value of pi is %s\n',pi())
fprintf(fp,'They cost %s per %s\n',$99,100)
fprintf(fp,'Logicals can be %s or %s\n',.T.,.F.)
// Formatting characters can contain a width, which will left pad with spaces 
fprintf(fp,'Right-justify and pad left: %10s this\n','Like')
// Left justify by placing a '-' directly following the '%' character 
fprintf(fp,'Left-justify and pad right: %-10s this\n','Like')
// %d is for numerics
fprintf(fp,'It is %d\n',year(date()))
// %t and %T are for formating datetime data types.
fprintf(fp,'It is %d, %t to be more precise\n',year(date()),datetime())
fprintf(fp,'It is %d, %T to be even more precise\n',year(date()),datetime())
// %f is for floating point numerics
fprintf(fp,'The value of pi is %f\n',pi())
// Decimal places can also be specified for floating point numerics (%f)
fprintf(fp,'The value of pi to two decimal places is %4.2f\n',pi())
// %y is for formatting currency data types
fprintf(fp,'They cost %y per %d\n',$99,100)
fprintf(fp,'They cost %y per %d\n',$99,1000)
fprintf(fp,'They cost %y per %d\n',$99,10000)
//%l and %L are for formatting logical datatypes.
fprintf(fp,'Logicals can be %l or %l\n',.T.,.F.)
fprintf(fp,'Logicals can also be %L or %L\n',.T.,.F.)
fclose(fp)
// Contents of fprintf.txt
It is       2009, 11/11/2009 11:41:51 AM to be more precise
The value of pi is  3.1415926
They cost $99.0000 per        100
Logicals can be True or False
Right-justify and pad left:       Like this
Left-justify and pad right: Like       this
It is 2009
It is 2009, 11/11/2009 11:41:51 AM to be more precise
It is 2009, Wednesday November 11 2009 11:41:51 to be even more precise
The value of pi is 3.141593
The value of pi to two decimal places is 3.14
They cost $99.0000 per 100
They cost $99.0000 per 1000
They cost $99.0000 per 10000
Logicals can be True or False
Logicals can also be Yes or No

https://www.lianja.com/doc/index.php/FPRINTF()


Import

use example
// Standard data file
append from custsdf sdf
// Fixed length fields
append from custfix fixed
// Delimited (',' separated, "" around character fields)
append from custdel delimited
// Delimited with blank (single space separates fields)
append from custblank delimited with blank
// Delimited with  (',' separated,  around character fields)
append from custpipe delimited with |
// Microsoft Excel compatible comma-separated
append from custcsv csv

Export

use example
// Standard data file
copy to custsdf sdf
// Fixed length fields
copy to custfix fixed
// Delimited (',' separated, "" around character fields)
copy to custdel delimited
// Delimited with blank (single space separates fields)
copy to custblank delimited with blank
// Delimited with  (',' separated,  around character fields)
copy to custpipe delimited with |
// Microsoft Excel compatible comma-separated
copy to custcsv csv
select * from shippers to file shiptxt

https://www.lianja.com/doc/index.php/Importing_and_Exporting_Data


Output lines of text

open database southwind
use example
set textmerge to balance.txt
set textmerge delimiters to "{{","}}"
set textmerge on
go top
do while balance > 0
    \Date: {{date()}}
    \\Name: {{last_name}}  {{first_name}}
 
    \Account Number  :  {{account_no}}
    \Current Balance  :   {{balance}}
    \Credit Limit:        :   {{limit}}
    \
    skip
enddo
set textmerge off
set textmerge to

https://www.lianja.com/doc/index.php/Single_Textmerge


Output lines of text

open database southwind
use example
set textmerge to balance.txt
set textmerge delimiters to "{{","}}"
set textmerge on
go top
do while balance > 0
    \Date: {{date()}}
    \\Name: {{last_name}}  {{first_name}}
 
    \Account Number  :  {{account_no}}
    \Current Balance  :   {{balance}}
    \Credit Limit:        :   {{limit}}
    \
    skip
enddo
set textmerge off
set textmerge to

https://www.lianja.com/doc/index.php/Double_Textmerge


open database southwind
use example
fp=fcreate("names.txt")
count=0
do while not eof()
    count = count + fputs(fp,trim(last_name) + ", "+trim(first_name))
    skip
enddo
fclose(fp)
echo str(count,5) + " bytes written.\n"
 
fp = fopen("names.txt")
count = 0
do while not feof(fp)
    if left(fgets(fp),5) = "Smith"
        ++count
    endif
enddo
fclose(fp)
echo str(count,5) + " Smiths found.\n"
close databases

https://www.lianja.com/doc/index.php/Working_with_Files_and_File_Systems_in_Recital


Advertisements

OS envinronment [examples]

Available disk space
DISKSPACE
? diskspace()
  60754290
// Example of a routine within a program
use patrons
size = header()+recsize()*reccount()
if size > diskspace()
    dialog box "Insufficient disk space."
return
endif

https://www.lianja.com/doc/index.php/DISKSPACE()


Environment information
GETENV
? getenv("DB_DATADIR")
C:\lianja\data\

https://www.lianja.com/doc/index.php/GETENV()


Update the value of an environment variable
PUTENV
putenv("APP_USER",user())

https://www.lianja.com/doc/index.php/PUTENV()


Display the Printer dialog box and return the name of the selected printer
GETPRINTER
m_dest = getprinter()

https://www.lianja.com/doc/index.php/GETPRINTER()


Return number of available printers and load an array with printer names
APRINTERS
aprinters(printerlist)
? printerlist

https://www.lianja.com/doc/index.php/APRINTERS()


System information
NETNAME
? netname()
system=Win32s on Windows NT nodename=unknown version=6.1 machine=MY-PC

https://www.lianja.com/doc/index.php/NETNAME()


Return software version information
VERSION
? version()
Lianja Version 1.1.3
? version(1)
Compiled on May  7 2014 12:11:59
? version(2)
1.0.0
? version(3)
No License error code

https://www.lianja.com/doc/index.php/VERSION()


Operating System name
OS
? os()
Linux x86

https://www.lianja.com/doc/index.php/OS()


Determine whether an application is being run via Lianja Web (Firecat Server)
ISFIRECAT
if not isfirecat()
  activate screen
endif

https://www.lianja.com/doc/index.php/ISFIRECAT()


Determine whether an application is being run via Lianja
ISLIANJA
if not islianja()
    activate screen
endif

https://www.lianja.com/doc/index.php/ISLIANJA()


Machine and OS account name
ID
? id()
My-PC # Winuser

https://www.lianja.com/doc/index.php/ID()


Return the output from (optionally piped) Linux commands
PIPETOSTR
// Return information about user Lianja process
? pipetostr("ps -ef | grep db.exe | grep "+user())

https://www.lianja.com/doc/index.php/PIPETOSTR()


Returns complete host system information
SYS

 

? sys(0)
system=Win32s on Windows NT nodename=unknown version=6.1 machine=MY-PC

https://www.lianja.com/doc/index.php/SYS()


Lianja/VFP provides tight integration with the command shell. The ` … ` command sequence (backticks) can be used to run external shell commands that are piped together and to substitute the output into a Lianja character string. For security reasons backticks are disabled in the Cloud Server.

Try this in the console:

Windows:

? "Date is `date /t`"

Linux:

? "Date is `date`"

Files and folders [examples]

Display a directory of files
DISPLAY FILES
display files *.prg

https://www.lianja.com/doc/index.php/DISPLAY_FILES


Display a directory of files
LIST FILES
list files

https://www.lianja.com/doc/index.php/LIST_FILES


Specify a directory in which files will be searched for
SET APPDIR
set appdir to C:\lianja\vault\

https://www.lianja.com/doc/index.php/SET_APPDIR


Specify a directory in which data files will be searched for and stored
SET DATADIR
set datadir to C:\lianja\data\

https://www.lianja.com/doc/index.php/SET_DATADIR


Specify default directory in which files will be searched for and stored
SET DEFAULT
set default to C:\Users\MyUser\AppData\Local\Temp\

https://www.lianja.com/doc/index.php/SET_DEFAULT


Specify default directory in which files will be searched for and stored
SET DIRECTORY
set directory to C:\accounts\

https://www.lianja.com/doc/index.php/SET_DIRECTORY


Allows case sensitive processing of file/directory names
SET FILECASE
set filecase on

https://www.lianja.com/doc/index.php/SET_FILECASE


Controls file specification display
SET FULLPATH
use customers
? dbf()
CUSTOMERS.DBF
set fullpath on
? dbf()
C:\LIANJA\DATA\SOUTHWIND\CUSTOMERS.DBF

https://www.lianja.com/doc/index.php/SET_FULLPATH


Specify a directory in which files will be searched for
SET LIBDIR
set libdir to C:\lianja\libvault\

https://www.lianja.com/doc/index.php/SET_LIBDIR


Specify the directory search path
SET PATH
set path to /home/app/prgs, /home/app/data

https://www.lianja.com/doc/index.php/SET_PATH


Specify the path to be used by the SYS(3) temporary file name function
SET TMPDIR
set tmpdir to "C:\Temp\"
? sys(3)
C:\Temp\000cf30006

https://www.lianja.com/doc/index.php/SET_TMPDIR


Determines whether the full path is included for temporary file name functions
SET TMPNAMPATH
? tmpnam()
C:\Users\Me\AppData\Local\Temp\_00000a7a0001.tmp
set tmpnampath off
? tmpnam()
_00000a7a0002.tmp

https://www.lianja.com/doc/index.php/SET_TMPNAMPATH


Return number of files and/or directories matching a file pattern and load file information into an array
ADIR
nTables = adir(aTables, "*.dbf")

https://www.lianja.com/doc/index.php/ADIR()


Return number of files matching a file pattern and optionally load file information into arrays
AFILES
nTables = afiles("*.*",afilenames)

https://www.lianja.com/doc/index.php/AFILES()


Display a directory of files
DIR
dir
dir *.prg

https://www.lianja.com/doc/index.php/DIR


Last modification date of a file
FDATE
? fdate("main.prg")
05/12/1999

https://www.lianja.com/doc/index.php/FDATE()


Time a file was last modified
FTIME
? ftime("main.prg")
11:51:38

https://www.lianja.com/doc/index.php/FTIME()


Check whether file exists
FILE
if file("names.ndx")
    erase names.ndx
endif

https://www.lianja.com/doc/index.php/FILE()


Change directory
CD
cd newdir

https://www.lianja.com/doc/index.php/CD


Current directory
DEFAULT
? default()
/home/user1
// In Lianja/VFP Command Window
pwd
/home/user1

https://www.lianja.com/doc/index.php/DEFAULT()


Current App directory
APPDIR
Lianja.openapp("lianjademo")
? appdir()
C:\Lianja\apps\lianjademo\
? Lianja.appdir
C:\Lianja\apps\lianjademo\

https://www.lianja.com/doc/index.php/APPDIR()


Current data directory
DATADIR
? datadir()
C:\Lianja\data\
? Lianja.datadir
C:\Lianja\data\

https://www.lianja.com/doc/index.php/DATADIR()


Path setting
PATH
? path()
/home/apps/

https://www.lianja.com/doc/index.php/PATH()


Current file-protection mask
GETPROT
old_mask = getprot()
setprot("S:RWED,O:RWED,G:RW,W:RW")
pack
setprot(old_mask)

https://www.lianja.com/doc/index.php/GETPROT()


Change current file-protection mask
SETPROT
old_mask = getprot()
setprot("S:RWED,O:RWED,G:RW,W")
use accounts
setprot(old_mask)

https://www.lianja.com/doc/index.php/SETPROT()


Add a backslash to a path expression if required.
ADDBS
rename (addbs(getenv([DB_CLASSDIR]))+ "myclass.cls") (addbs(getenv([DB_CLASSDIR]))+ ;
"myoldclass.cls")

https://www.lianja.com/doc/index.php/ADDBS()


Path for the specified file
FULLPATH
? fullpath("customers.dbf")
C:\lianja\data\southwind\customers.dbf

https://www.lianja.com/doc/index.php/FULLPATH()


Base filename from a given file specification
BASENAME
? dbf()
customers.dbf
set fullpath on
? dbf()
/opt/lianja/data/southwind/customers.dbf
? basename(dbf())
customers.dbf

https://www.lianja.com/doc/index.php/BASENAME()


Return a file name with a new extension if one does not already exist
DEFAULTEXT
open database southwind
use customers
? defaultext(alias(),"dbd")
? defaultext(alias(),"dbt")
? defaultext(alias(),"dbx")

https://www.lianja.com/doc/index.php/DEFAULTEXT()


Return a file name with a new extension
FORCEEXT
FORCEXT
open database southwind
use customers
? forceext(dbf(),"dbd")
? forceext(dbf(),"dbt")
? forceext(dbf(),"dbx")

https://www.lianja.com/doc/index.php/FORCEEXT()

https://www.lianja.com/doc/index.php/FORECEXT()


Return a file name with a new path
FORCEPATH
open database southwind
use customers
? forcepath(dbf(),"C:\Lianja\data\newdb\")

https://www.lianja.com/doc/index.php/FORCEPATH()


Return the file extension from a given file specification
JUSTEXT
? justext("C:\Lianja\apps\lianjademo\lianjademo.lianja")
lianja

https://www.lianja.com/doc/index.php/JUSTEXT()


Return filename without the path from a given file specification
JUSTFNAME
? justfname("C:\Lianja\apps\lianjademo\lianjademo.lianja")
lianjademo.lianja

https://www.lianja.com/doc/index.php/JUSTFNAME()


Return the path from a given file specification
JUSTPATH
? justpath("C:\Lianja\apps\lianjademo\lianjademo.lianja")
C:\Lianja\apps\lianjademo\

https://www.lianja.com/doc/index.php/JUSTPATH()


Return the stem (filename without extension) from a given file specification
JUSTSTEM
? juststem("C:\Lianja\data\southwind\customers.dbf")
customers

https://www.lianja.com/doc/index.php/JUSTSTEM()


Return the drive from a given file specification
JUSTDRIVE
? justdrive("C:\Lianja\apps\lianjademo\lianjademo.lianja")
C:

https://www.lianja.com/doc/index.php/JUSTDRIVE()


Temporary filename
TMPNAM
// Filename with '.tmp' extension and TMPDIR() path
cTempFile = tmpnam()
// Filename with '.txt' extension and specified path
cTempFile = tmpnam("C:\temp\", ".txt")
// Filename with '.txt' extension and TMPDIR() path
cTempFile = tmpnam(".txt")
// Filename with '.tmp' extension and no path
cTempFile = tmpnam(.F.)

https://www.lianja.com/doc/index.php/TMPNAM()


Current directory
HOME
?home()
C:\USERS\USER1\

https://www.lianja.com/doc/index.php/HOME()


Current directory
CURDIR
? curdir()
/home/user1

https://www.lianja.com/doc/index.php/CURDIR()


Change directory
CHDIR
chdir newdir

https://www.lianja.com/doc/index.php/CHDIR


Copy any type of file
COPY FILE
close tables
copy file patrons.dbf to backup.dbf

https://www.lianja.com/doc/index.php/COPY_FILE


Delete specified file
DELETE FILE
delete file orders.dbf

https://www.lianja.com/doc/index.php/DELETE_FILE


Delete a file or files
ERASE
if file("backup.old")
    erase backup.old
endif

https://www.lianja.com/doc/index.php/ERASE


Create a directory
MD
md newdir

https://www.lianja.com/doc/index.php/MD


Create a directory
MKDIR
mkdir newdir

https://www.lianja.com/doc/index.php/MKDIR


Delete a directory
RD
rd newdir

https://www.lianja.com/doc/index.php/RD


Change the name of a file
RENAME
rename patrons.dbf to patron.dbf
rename events.ndx event.ndx

https://www.lianja.com/doc/index.php/RENAME


Delete a directory
RMDIR
rmdir newdir

https://www.lianja.com/doc/index.php/RMDIR


Count files matching pattern
FILECOUNT
nfiles = filecount("*.dbf")

https://www.lianja.com/doc/index.php/FILECOUNT()


Display a dialog allowing a directory to be selected and its name returned
GETDIR
cDirSelected = getdir("C:\Lianja","","Please select a directory")

https://www.lianja.com/doc/index.php/GETDIR()


Check whether directory exists
DIRECTORY
? directory("C:\Lianja\Apps\lianjademo")

https://www.lianja.com/doc/index.php/DIRECTORY()


Display an Open dialog allowing a filename to be selected and returned
LOCFILE
cProgramSelected = locfile("prg","Please select a program")

https://www.lianja.com/doc/index.php/LOCFILE()


Display a Save As dialog allowing a filename to be selected or specified and returned
PUTFILE
cProgramSaveAs = putfile("Save the program","default.prg","prg")

https://www.lianja.com/doc/index.php/PUTFILE()


Display an Open dialog allowing a filename to be selected and returned
GETFILE
cScript = getfile("Visual FoxPro(*.prg);;Python(*.py);;JavaScript(*.js);;PHP(*.php)","","",0,"Select a script")

https://www.lianja.com/doc/index.php/GETFILE()


Information about a file
FILEINFO
? fileinfo("test.exe")
30,9,2003,20,12,23,A,4731284

https://www.lianja.com/doc/index.php/FILEINFO()


Size of file
FSIZE
? fsize("example.dbf")
     37580

https://www.lianja.com/doc/index.php/FSIZE()


Current temporary file directory location
TMPDIR

 

// envar Lianja Server only
? getenv([DB_TMPDIR])
? tmpdir()
set tmpdir to "C:\tmp\"
? tmpdir()

https://www.lianja.com/doc/index.php/TMPDIR()


open database southwind
use example
fp=fcreate("names.txt")
count=0
do while not eof()
    count = count + fputs(fp,trim(last_name) + ", "+trim(first_name))
    skip
enddo
fclose(fp)
echo str(count,5) + " bytes written.\n"
 
fp = fopen("names.txt")
count = 0
do while not feof(fp)
    if left(fgets(fp),5) = "Smith"
        ++count
    endif
enddo
fclose(fp)
echo str(count,5) + " Smiths found.\n"
close databases

https://www.lianja.com/doc/index.php/Working_with_Files_and_File_Systems_in_Lianja


External files

Q:

Is it possible to export a lianja .dbf file back into a vfp .dbf file. We need to copy some data into legacy vfp software to keep our system functioning while software is getting rewritten. — can’t seem to delete the post, but found that I could export the data using a text file.

A:

There are several ways to do this. If this is a one off, then use the copy command with a delimited type. Then import that into your VFP table.

http://www.lianja.com/doc/index.php/COPY

If this is something you plan to do often, then probably setting up an ODBC conncetion to lianja makes the most sense.
http://www.lianja.com/doc/index.php/…ver_on_Windows


Lianja provides tight integration with the command shell. The ` … ` command sequence (backticks) can be used to run external shell commands that are piped together and to substitute the output into a Lianja character string.

After select/copy/paste these examples in Console workspace on usual Windows:
Code:

? "`cmd /c date /t`"    // works OK

? "Date is `date`"     // does not work

Barry uses cygwin and both commands work for him.
If it is not easy to find backtick on the keyboard, the code for backtick is ALT+96

I’ve tested the proper formatted commands using back ticks and they work as expected.

On further investigation I’ve found that it appears that when I “run cmd” from Lianja, it is cmd.exe that is hanging. Lianja is simply waiting for cmd.exe to finish. If I shut down cmd.exe from task manager, Lianja continues just fine.

So the crash is definitely an issue with my system and, as you say, since I have no reason to ever need to this in my code, it’s not even an issue.

I have simplified this behavior on Windows in Lianja 3.4RC14. You now no longer need to specify cmd /c you can just use the backticks but remember that the command itself must be a valid windows command that produces output.

? “The date is `date /t`”


Q:

with Lainja.SpawnApp() I can call other app.

EG: lianja.SpawnApp(“lianjademo”), start the app, but the deployed app, not the “building app”.

Can I set the window size of the opened app?

A:

Lianja.spawnApp() only takes an app name not command line args.
https://www.lianja.com/doc/index.php/Lianja

You should use spawn() or run() if you want to specify command line args.

A2:

run(“C:\lianja\bin\lianjaruntime.exe –utf8 –debug –nosplashscreen –app sampleproductsentry –geometry 10,10,800,600”)

and it work.. 🙂

so, I can:

Code:
Lianja.sessionStorage.setItem("p_retvalue", False)
run("C:\lianja\bin\lianjaruntime.exe --utf8 --debug --nosplashscreen --kiosk --app sampleproductsentry --geometry 10,10,800,600")
value = Lianja.sessionStorage.getItem("p_retvalue")	
messagebox(etos(Value))

in my “sampleproductsentry” app I do:

Code:
Lianja.sessionStorage.removeItem("p_retvalue")
Lianja.sessionStorage.setItem("p_retvalue", PRODUCTS.PRODUCTID)
Lianja.closeApp()
quit

from the first app, I call the second, when the user close it, the control return to the first and I can read the value passed.


Q:

Lianja.spawnApp conveniently allows running a .dbo (compiled prg) directly, as so:

lcDbo = “myprg.dbo”
lcPathedDbo = set(“dire”) + lcDbo
Lianja.spawnApp(lcPathedDbo)

Here’s the challenge: I’m launching this from a web service (.rsp page application/json), and need to have the .dbo accept a parameter so that it processes a particular instance based on data pointed to by the parameter.

Although SessionStorage might look like an answer, that has the same issue: I would need to communicate the “key” for the .dbo to access.

A:

You cannot use Lianja.spawnApp() from a server page. The Lianja system object is only available in the clients I.e desktop, web or mobile.

Lianja.spawnApp() is desktop specific as it relies on being called from the lianja runtime client.

Rather than spawning a process from inside a web service I’d recommend you implement a consumer/producer architecture which will be much more scaleable.

See my post in these forums on resource locks.

You could add request records to a table in the web service always locking a known resource exclusively beforehand.

You then have a background job (using cron on linux or task scheduler on Windows) running that reads records from the table and processes the requests. When there are no more requests, lock a known resource and zap the table then unlock the resource.


 

External files

Q:
Using (Foxpro) functions filetostr and strtofile to read and write a specific file (f.e. e:\xxx\yyy\z.txt) on my server is ok in DeskApp but not in WebApp.
Error message: File not exists. Error number: 15
Is it not possible to use these functions in a Webapp?
A:
filetostr() will run where the code runs, which is on the server, not on the client. Could that be the issue?

You can upload a file using networkrequest (http://www.lianja.com/community/show…load-in-Lianja) Here’s a thread for further reference: http://www.lianja.com/community/arch…hp/t-3027.html

Note that you can upload a file from the user’s desktop into a data file using the Attachments section, and then can run filetostr() on the memofield using mtos() .
assuming you have deployed the app to the server on the APaaS, and are calling the filetostr() through a call on the server (using Lianja.evaluate() from the JavaScript, or using an .rsp page) there should be no issue.


Q:
I would like to store some text from a .txt file to a memory variable. Below is what I did, but It does not work. invmemo still does not have the string from the text file. Any suggestions.

Code:
...
ans = messagebox("Do you want to add a memo to the invoice?", 36, "ADD MEMO")
if ans = 6
memofilename=GETFILE('txt',"","",2,'Add a .txt file to memo')
invmemo = filetostr(memofilename)
endif

A:

Code:
REPLACE invmemo with filetostr(memofilename)
//or
APPEND MEMO invmemo FROM memofilename

Q:
Is it possible to get a return value from a run or spawn command
A:
Yes, using spawnid() etc. you can know when the process ends.
So, the spawned
process writes to SessionStorage, and the calling process picks up the result from SessionStorage.


Q:
What would be a step by step to create a Lianja windows service?
A:
Method 1: .bat file calling app with lianjaruntime.exe –app appname Run as Windows Scheduled Task, set to run on startup, with highest permissions.

Method 2: srvany.exe — blog post by Calvin Hsai here: http://blogs.msdn.com/b/calvin_hsia/…13/282351.aspx


I have an older VFP App where I use the run command to run some background processes. I have replaced them with the Lianja spawn command

The Spawn command uses the same syntax as the run command.
Spawn will run your process in the background and return control of your app back to while it is running.

Thats very handy if you need to run simultaneous operations while not relinquishing control of your application.
1) if you are running a lot of processes, you can end up overloading the processor and hurting UI performance. This would especially be the case if you were running a Lianja Desktop App on a remote desktop-type (RDP or ICA) environment.
2) you can monitor what processes you have running by getting the pid of a spawned process using ActivePid().
3) If you do have a cajillion background processes to run, you can run the spawn off a web service call to another server where the Lianja Cloud Server is running. Maintaining a central database with server and pid will allow you to coordinate all this.
4) You can get return values in a variety of ways. SharedMemory is one way (google Lianja Shared Memory). Another way is to the use monitoring database table entry for a given server and pid to hold the return value.


Q:
In my program, I have a canvas section with a command button.
When the button is clicked, I access a local database then transfer the rows of data to an excel sheet.
So far it’s all working fine except for the processing speed.
It starts off by processing approx. 4 rows every second. Shortly afterwards, the process slows down.

Code:
scan
  scatter to ExcelExport
  WITH oExcel
    .Range("A" + ALLTRIM(str(excelRowCount))).Value = ALLTRIM(LNAME)
    ...
  ENDWITH

  // Activate another table
  select ...
  append blank
  gather FROM ExcelExport
  replace DATEARCHIVED with date()
  skip 0
               
  excelRowCount = excelRowCount + 1
  progBarCount = progBarCount + 1
  Lianja.Get("page.section.pbExport").value = progBarCount
endscan

A:
If excel is indeed the issue, see if you can issue one command to excel that exports the data you need in csv format (or even ado-style xml). Exporting that will be a quick task, as will importing into a cursor in Lianja. From there, things should fly.
“winner” code which execute almost instant:

Code:
proc page1_section1_field2_click()
        cDirSelected = getdir("C:\","","Please Select A Directory For File Saving")
        IF inuse("exceldata") = .T.
            SELECT exceldata
        ELSE
            select 0
            use exceldata
        ENDIF
        cDateTime = ttoc(datetime(),1)
        if right(cDirSelected,1)  "\"
            cDirSelected = cDirSelected + "\"
        endif
        cSaveAsPath = cDirSelected + "ExcelExport_" + cDateTime + ".txt"
       
        set textmerge to (cSaveAsPath)
        set textmerge delimiters to "{{","}}"
        set textmerge on
        c9=chr(9)
        scan
\{{DLNAME}}{{c9}}
\\{{DFNAME}}{{c9}}
\\{{CLNUM}}{{c9}}
\\{{DTYPE}}{{c9}}
\\{{DADDR1}}{{c9}}
\\{{DADDR2}}{{c9}}
\\{{DADDR3}}{{c9}}
\\{{DCITY}}{{c9}}
\\{{DPROVINCE}}{{c9}}
\\{{DPCODE}}{{c9}}
\\{{DCOUNTRY}}


 

External files

Q:
I would like to access a file saved in a folder that is in the app folder.
How would I access the app path?

For example, say file abc.xls exists in an Excel folder.
The development path could be:
C:\Lianja\apps\testing\Excel\abc.xls.

I could use the following:

Code:
oExcel = CREATEOBJECT("Excel.application")
// Need help here.
cExcelPath = app.path + "\Excel\abc.xls"
oWorkbook = oExcel.Workbooks.Open(cExcelPath)

A:
DEFAULT() or even SYS(2003) should do it for you.



External files

RUN executes a command line without capturing it’s output, waits for command completion

SPAWN
executes a command line without capturing it’s output. Starts the command and continues execution in parallel

PIPETOSTR()
executes a command line capturing and returning whatever it wrote to its standard output, waits for completion.
Bacticks can be used inside character strings to capture the output and substitute into the character string

SHELLEXEC
is a windows only and non portable WINAPI function call

Also note that you can execute a program via file association e.g. To open up PDF file.

Code:
Lianja.showDocument("myfile.pdf")

When including the full path to a file please include “file:///“. For example:

Code:
showdocument("file:///f:\staff\sherlie\book2.xlsx")

One way is to use the RUN command:

Code:
RUN "c:\windows\EXPLORER.EXE /n, /e, X:\Folder_of_my_choosing"

 

Code:
WSHShell = CreateObject("WScript.Shell")
eExe = WSHShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe\")
if type("eExe") = "C"
        ? "Key exists: " + eExe
else
        ? "Key not found"
endif
RELEASE WSHShell

 


Is it actually a specific file you need to open? Is it a scenario where you could create a temporary file (TMPNAM() will give you a temporary file name in the temporary files directory) so that multiple users aren’t trying to access the same file?
If it’s the CSV report files we are talking here and the CSV is already open in Excel, the fopen() will return a filehandle of -1, ferror() will return -1, errno() will return 13 and strerror(errno()) will return ‘Permission denied’. That doesn’t tell you who has the file open, or whether the ‘Permission denied’ is due to another cause.


Q:
What would be good way to run an executable? Ping. Pipe (Linux)
A:
From the Linux desktop you could use ! and output to a file, then handle the file e.g.

Code:
!ping -c 5 192.168.3.3 > /home/yvonne/ping.txt
type /home/yvonne/ping.txt

 


When creating a new window that runs an App the –args “arg” that you specify is in the same format as Lianja.showDocument() NOT args to a script.
So for example to load the “customerorders” app and lookup the customerid.

Code:
Lianja.spawnApp("c:\lianja\bin\lianjaruntime --app mysalesapp --args "page:customers?action=search&text=1220")

Excel file formats are different between older and newer releases and I am unsure what version of excel you are using so you need to research the ODBC driver that matches the version you use.
Be aware that excel is not multiuser either so you need to bear this in mind if you are building a multiuser application that interacts with an excel spreadsheet.


Q:
Is it possible to call a Windows command-line application from within a Lianja App?
A:

Code:
Lianja.run("path_to_exe","arg1,arg2...")

or

Code:
Lianja.spawn("path_to_exe","arg1,arg2...")

Q:
The reason I ask is that I can call a prg with it and get no error; but there is also no evidence that the prg is running. Executing

Code:
? lianja.spawnapp("testspawn.prg")

A:
In fact I stand corrected, my code will run .prg, .php, .py. .js and app files.
You may need to set the path of the filename if its not an App.

Code:
result = Lianja.spawnApp(filename | appname, [username, [password, [args] ] ])
if result
  // process is running
endif

Lianja.showDocument() has some interesting functionality that is not well known.
You can
invoke the desktop browser,
run external applications,
run other apps (in a new runtime container)
and also
run Lianja/VFP scripts (in a new runtime container).
Lianja.showDocument() handles desktop associations. e.g..

Code:
Lianja.showDocument("http://www.google.com")
Lianja.showDocument("myspreadsheet.xlsx")
Lianja.showDocument("myspecialapp.exe")
Lianja.showDocument("lianja --app myapp --username myusername --password mypassword")
Lianja.showDocument("lianja myform.prg")