May 06, 2024, 10:32:57 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Replacing text in a text file

Started by Andy, October 08, 2014, 07:19:44 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

I have a large .txt file which has over 2,000 lines, each line would look something like this:

P1001 This is a description

What I need each line to become in the text file is this:

X + = 1
CodeIn(x) = "P1001"   
DescIn(x) =  "This is a description"

The preview on here won't show the square bracketts so I've had to use the round ones instead.

Can anyone help please?

Thanks,
Andy.



Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Andy

Sorry forgot to say:

The code given - example P1001, will always be 5 characters, but the descriptions will be different in length.

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

DennisL

October 08, 2014, 08:21:50 AM #2 Last Edit: October 08, 2014, 08:32:05 AM by DennisL
hi there andy1966, please find attached my crack at this.  it's not the prettiest and easiest to follow maybe (could have used better variable names etc.) but it seems to do the job in my simple tests.

it's a simple vbscript -you didn't specify if you wanted this as a IWB program or other, so i went with the quickest (and dirtiest).

it expects an input file called "input_file.txt" in the same directory as the script when run and will generate an output file called, strangely enough, "output_file.txt".  it will popup a message box when it has finished.

hope it does what you want.  cheers.

btw, i had to add ".txt" to the end of the filename as the forum doesn't like scripts as attachments.  please remove this to revert back to a ".vbs" file to run.

billhsln

October 08, 2014, 10:33:19 AM #3 Last Edit: October 08, 2014, 10:35:08 AM by billhsln
IWB Code:


x = -1
WHILE (READ(iFile,stmt) = 0)
x++
CodeIn[x] = MID$(stmt,1,5)
DescIn[x] = MID$(stmt,7)
ENDWHILE


MID$ without the 3rd parameter pulls till end of field.

Note, you see my brackets because I have it in a 'Code' block.  See # icon.

Hope that is what you are looking for,
Bill
When all else fails, get a bigger hammer.

LarryMc

Tried to post this way earlier today but couldn't get on the forums till now..
This is my guess for what you wanted to do including the possible source file you are trying to convert.

DEF infile,outfile:FILE

DEF ln:STRING

IF(OPENFILE(infile,getstartpath+"inputfile.txt","R") = 0)
IF(OPENFILE(outfile,getstartpath+"outputfile.txt","W") = 0)
while (READ(infile,ln) = 0)
write outfile,"x+= 1"
int pos=instr(ln," ")
write outfile,"CodeIn[x] = \""+ left$(ln,pos-1)+"\""
write outfile, "DescIn[x] = \""+mid$(ln,pos+1)+"\""
write outfile,""
endwhile
closefile outfile
PRINT "File read successfully"
endif
CLOSEFILE infile
ELSE
   PRINT "File could not be opened"
ENDIF


LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Andy

Dennis, Bill and Larry,

Thanks for all your work, I tried all suggestions.

Larry has a 'heads up' on what I'm working on, and in the end Larry's suggestion worked best for me.

In the end, there were 10 different text files, and in the final total 3,126 lines were converted.

This has really saved me a great deal of time and I'm very greatful to all!

Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.