How to list text files in a directory in Progress 4GL? - progress-4gl

I have a folder in c drive,whicn contain 1000 txt file,i want
to get the list of all these txt file. How can i get this list?

Use the OS-DIR() function.
For example:
DEFINE STREAM dirlist.
DEFINE VARIABLE filename AS CHARACTER FORMAT "x(30)" NO-UNDO.
INPUT STREAM dirlist FROM OS-DIR(".").
REPEAT:
IMPORT STREAM dirlist filename.
DISPLAY filename.
END.
INPUT CLOSE.

For example: ipcPath = "C:\temp\
DEFINE INPUT PARAMETER ipcPath AS CHARACTER NO-UNDO.
DEFINE VARIABLE chFiles AS CHARACTER NO-UNDO.
INPUT FROM OS-DIR(ipcPath).
REPEAT:
IMPORT UNFORMATTED chImport NO-ERROR.
DISPLAY chFiles FORMAT "X(75)".
END.
INPUT CLOSE.
chFiles is a spacedelimeted list and contains the filename, the path, and an 'F' or 'D' tag.

I have a directory-tools program which enable a developer to do all kinds of fun things with file systems. You can get the code here: http://communities.progress.com/pcom/docs/DOC-16578

Related

How to copy one text file to another text file with changes (editing df files)

I am fairly new to progress 4gl. I am trying to copy the contents of one df file into another file where I want to copy everything except the areas of the df file. this is what I have so far.
define stream mystream.
input from "C:\OpenEdge\WRK\df file\sample.df".
do while true on endkey undo, leave:
output to "C:\OpenEdge\WRK\df file\test1.df".
end.
output close.
display "finished".
I would first like to copy all the contents of the first df file into another df file, but right now I'm getting a blank file. Please let me know where I am going wrong.
This is what I do:
grep -v AREA < dbname.df > dbname.df2
If you are stuck with on OS that lacks "grep" or if you just have an urge to do it with 4gl code I'd do something like:
define variable lineIn as character no-undo.
input from "dbname.df".
output to "dbname.df2".
repeat:
import unformatted lineIn. /* read the data a whole line at a time */
if lineIn begins " AREA" then next. /* skip lines that start with " AREA" */
put unformatted lineIn skip. /* spit the input line back out */
if lineIn = "" then put unformatted skip(1). /* yes, this is a weird thing */
end.
To copy all the contents of first df file to another df file, use this code:
DEFINE VARIABLE TEXT-STRING AS CHARACTER FORMAT "X(76)".
INPUT FROM "C:\OPENEDGE\WRK\CMD-LOG.DF".
OUTPUT TO "C:\OPENEDGE\WRK\MY.DF".
DO WHILE TRUE ON ENDKEY UNDO, LEAVE:
IMPORT UNFORMATTED TEXT-STRING.
MESSAGE TEXT-STRING .
END.
INPUT CLOSE.
OUTPUT CLOSE.

Writing Tables from Matlab into CSV

I have several tables in matlab that and I would like to write all to one .csv file, vertically concatenating. I would like to keep the column names from each table as the top row, and would like to use a loop to write the csv. The ultimate goal is to read the data in to R, but R.matlab did not work well. Suggestions about how to do this?
Alternatively how can I change filenames in a for loop using the iterator?
e.g. along the lines of
for i=1:10
writecsv('mydatai.csv',data(i))
end
So I must have at the end 10 csv files as output.
You can change the filename within the loop by using for sprintf string formatting function, for example:
dlmwrite(sprintf('mydata%i.csv', i), data(i) )
Note that the %i portion of the string is the sprintf formatting operator for an integer, it is just a coincidence that you also decided to name your iterator variable 'i'.
You can append extra data to an existing CSV by using the dlmwrite function, which uses a comma delimiter as the default, and including the '-append' flag.
Another way would be to use
writetable(Table,filename )
and to change file name after every alternation you can use
filename = ['mydata' num2str(i) '.csv']

check file existance in progress 4GL

How to check existance of particular file by use of code.
Eg.
def var a as character.
a = "abc.p"
run value(a).
---> here first i want to check if abc.p exist in workspace or not.
You can use the SEARCH function. Directly from the online manual:
SEARCH function
Searches the directories and libraries defined in the PROPATH environment variable for a file. The SEARCH function returns the full pathname of the file unless it is found in your current working directory. If SEARCH does not find the file, it returns the Unknown value (?).
Syntax
SEARCH ( opsys-file )
opsys-file
A character expression whose value is the name of the file you want to find. The name can include a complete or partial directory path. If opsys-file is a constant string, you must enclose it in quotation marks (" "). The value of opsys-file must be no more than 255 characters long.
Example:
DEFINE VARIABLE cPgm AS CHARACTER NO-UNDO.
cPgm = "test.p".
IF SEARCH(cPgm) <> ? THEN
RUN VALUE(cPgm).
If you provide a fully qualified pathname, SEARCH checks if the file exists. In this case, SEARCH does not search directories on the PROPATH.
If you do not want to use the propath you can use the FILE-INFO system handle.
After setting FILE-NAME, you can check the FILE-TYPE if it exists. See also the Progress Help for FILE-INFO.
FILE-INFO:FILE-NAME = a.
IF FILE-INFO:FILE-TYPE MATCHES "*F*"
THEN RUN VALUE(FILE-INFO:FULL-PATHNAME).

Input a .i in a progress 10.2b

I'm inputting a file that needs to be converted to an xml file but I also want to input a .i with the definition of the temp table used to create the xml. Also the delimiter isn't working (I need a way to convert a variable to something the command can read). Thanks!
define input parameter pInputFile as character no-undo.
define input parameter pDelimiter as character no-undo.
???define input parameter pIncludeFile as character no-undo.???
define output parameter pOutputFile as character no-undo init "/tmp/out..
/* start of .i */
define temp-table ttGeneric no-undo
field cust_id as integer
field name as character
field address as character
field address2 as character
field city as character
field state as character
field zip as character
field cust_key as character
index idx is primary cust_id.
/* end of .i */
input stream sImport from value(pInputFile) no-echo.
repeat:
create ttGeneric.
import stream sImport delimiter pDelimiter ttGeneric.
end.
input stream sImport close.
temp-table ttGeneric:write-xml("LONGCHAR", pOutputFile, yes).
maybe set a pre-processor in the calling program (some how).
Delimiters to IMPORT and EXPORT must be literal strings. You cannot use variables, fields, parameters or anything like that.
I have, on occasion, worked around that with a CASE statement. i.e.:
case pDelimiter:
when "," then import stream sImport delimiter "," ttGeneric.
when "|" then import stream sImport delimiter "|" ttGeneric.
end.
Ugly. But it works.
I think you might be trying to say that you want to pass the name of an include file that contains a TT definition? And somehow associate that definition with a temp-table?
If that is more or less correct then you are probably being too specific about your requirement -- you probably really just want to dynamically create a TT whose definition is external to the program and unknown at compile time.
One way to do that is to use the read-xmlschema() method -- you're already using write-xml() so it's a small step... first convert your .i to a little .p like so:
define temp-table ttGeneric no-undo
field cust_id as integer
field name as character
field address as character
field address2 as character
field city as character
field state as character
field zip as character
field cust_key as character
index idx is primary cust_id.
buffer ttGeneric:write-xmlschema( "file", "ttgeneric.xsd", true, ?, ?, ? ).
return.
(This little stub lets you create the .XSD file. It serves no other purpose. Just run it once to get that file.)
Then when you want to use that temp-table:
define variable tx as handle no-undo.
create temp-table tx.
tx:read-xmlschema( "ttgeneric.xsd", "file", ?, ?, ? ).
(Note: Unlike with delimiters you can use variables & parameters etc. for the xsd name and it could be in a longchar rather than a file...)
The next adventure you will run into is figuring out a replacement for IMPORT that works with dynamic temp-tables. Buffer handles don't have import() and export() methods :(
The following snippets may help:
define variable dummy as character no-undo extent 128.
...
dummy = ?.
import dummy.
...
do i = 1 to 128:
if dummy[i] = ? then leave.
tx:buffer-field( i ):buffer-value() = dummy[i].
end.

Reading input string from a text file in progress4gl

I have a .txt file having some text. I want to read this file and store it into a string.
Is there available any function for that?
Or how to do it ? please help
define variable s as longchar no-undo.
copy-lob from file "fileName.txt" to s.
DEF VAR ch-var AS CHARACTER NO-UNDO.
INPUT FROM VALUE(file-name).
REPEAT:
IMPORT UNFORMATTED ch-var.
END.