How to copy one text file to another text file with changes (editing df files) - progress-4gl

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.

Related

How to append CSV file into dbf?

Do you know how to append a .csv file into DBF by using FoxPro command , instead of using import wizard?
I have tried the code below, but it is not working.
Code:
Append from getfile() Delimited With ,
You can use append from:
local lcFileName
lcFilename = getfile()
if !empty(m.lcFileName)
append from (m.lcFileName) type delimited
endif
This would do appending including the header line. However, if you have a field that is not character, then you could use it to skip header line. For example say there is a column named theDate of type date and should not be empty, then you could say:
append from (m.lcFileName) type delimited for !empty(theDate)
Or if you are appending into an empty cursor, you could say - ... for recno()>1.

Progress OpenEdge simple utility with input parameter

I have a .p file and I run it in unix console. and i love it . because it just a simple utility to run. It is simple and helps me learn.
Now I started to get more fancy. I am wondering if I can do an input parameter to a .p file?
this is how I usually run my .p file.
Now if test.p needs 2 parameter .. how do I do it in the .p? and how do I run it in the console?
This is what I have in the test.p and dOrd and dLocation is the input parameter I want.
output to /usr2/appsrv/test/test.txt.
def var dOrd like Ord.Ord.
def var dLocation like Ord.Ord.
find OrdCSRef no-lock where OrdCSRef.Ord = dOrd and OrdCSRef.Loc = dLocation no-error.
if available OrdCSRef then do:
put unformatted OrdCSRef.CSOrdRef skip.
end.
else
put unformatted "Create CSOrdRef" skip.
end.
output close.
I have tried the following syntax in the unix console. but obviously it will not work.
INPUT parameters is what you are looking for. Change test.p as follows and then run it as "RUN /usr2/appsrv/test/test.p ("ARG1", "ARG2")"
output to /usr2/appsrv/test/test.txt.
def input parameter dOrd like Ord.Ord.
def input parameter dLocation like Ord.Ord.
find OrdCSRef no-lock where OrdCSRef.Ord = dOrd and OrdCSRef.Loc = dLocation no-error.
if available OrdCSRef then do:
put unformatted OrdCSRef.CSOrdRef skip.
end.
else
put unformatted "Create CSOrdRef" skip.
end.
output close.

MATLAB simultaneous read and write the same file

I want to read and write the same file simultaneously. Here is a simplified code:
clc;
close all;
clearvars;
fd = fopen ('abcd.txt','r+'); %opening file abcd.txt given below
while ~feof(fd)
nline = fgetl(fd);
find1 = strfind(nline,'abcd'); %searching for matching string
chk1 = isempty(find1);
if(chk1==0)
write = '0000'; %in this case, matching pattern found
% then replace that line by 0000
fprintf(fd,'%s \n',write);
else
continue;
end
end
File abcd.txt
abcde
abcd23
abcd2
abcd355
abcd65
I want to find text abcd in string of each line and replace the entire line by 0000. However, there is no change in the text file abcd.txt. The program doesn't write anything in the text file.
Someone can say read each line and write a separate text file line by line. However, there is a problem in this approach. In the original problem, instead of finding matching text `abcd, there is array of string with thousands of elements. In that case, I want to read the file, parse the file for find matching string, replace string as per condition, go to next iteration to search next matching string and so on. So in this approach, line by line reading original file and simultaneously writing another file does not work.
Another approach can be reading the entire file in memory, replacing the string and iterate. But I am not very sure how will it work. Another issue is memory usage.
Any comments?
What you are attempting to do is not possible in a efficient way. Replacing abcde with 0000, which should be done for the first line, would require all remaining text forward because you remove one char.
Instead solve this reading one file and write to a second, then remove the original file and rename the new one.

How to replace last line of the file with a new text?

I am appending a file in scala, and I want to replace the last line of the file with a new text. I know how to append to a file, but I can't figure out how to overwrite the last line.
I am appending like this:
val fw = new FileWriter("src/file.txt", true) ;
fw.write("new item");
Could anybody help?
The most efficient way I can think of is the following:
Read the file as a RandomAccessFile.
Go to the end of the file Start reading backwards till you find the
end of the last but one line.
Get rid of this last line
Append your line to this new file
Here is an example of how to remove the last line in Java using RandomAccessFile.
Delete last line in text file

How to list text files in a directory in 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