Mateen
asked on
To search and replace word into a text file from PB7 Script
Please read this first.
https://www.experts-exchange.com/questions/21848080/To-write-into-a-text-file.html
After adding the line the file looks like this.
kse_code,rate_date,open_ra te,high_ra te,low_rat e,close_ra te,turnove r
AACIL,12 05 2006,23.6,24.3,23.3,23.55, 1077500
AASM,12 05 2006,55,59.85,55,59.85,150 0
ABCF,12 05 2006,9.25,9.6,9.25,9.45,27 3500
ABL,12 05 2006,105.75,109,105.75,107 .15,469500
ABOT,12 05 2006,170,170,165.25,166.5, 3600
ACBL,12 05 2006,90,91.4,89.55,89.7,95 5500
ACF,12 05 2006,16.45,16.9,16.3,16.9, 410000
ACPL,12 05 2006,98,104,98,102.5,6100
ADTM,12 05 2006,3.25,3.25,3.25,3.25,2 000
Now I want to replace "12 05 2006" with "2006/05/12" in the entire file.
Please use variable in the code. for example
ls_old_value = "12 05 2006"
ls_new_value= "2006/05/12"
https://www.experts-exchange.com/questions/21848080/To-write-into-a-text-file.html
After adding the line the file looks like this.
kse_code,rate_date,open_ra
AACIL,12 05 2006,23.6,24.3,23.3,23.55,
AASM,12 05 2006,55,59.85,55,59.85,150
ABCF,12 05 2006,9.25,9.6,9.25,9.45,27
ABL,12 05 2006,105.75,109,105.75,107
ABOT,12 05 2006,170,170,165.25,166.5,
ACBL,12 05 2006,90,91.4,89.55,89.7,95
ACF,12 05 2006,16.45,16.9,16.3,16.9,
ACPL,12 05 2006,98,104,98,102.5,6100
ADTM,12 05 2006,3.25,3.25,3.25,3.25,2
Now I want to replace "12 05 2006" with "2006/05/12" in the entire file.
Please use variable in the code. for example
ls_old_value = "12 05 2006"
ls_new_value= "2006/05/12"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Sandeep,
Thanks for replying on Sunday.
I am sure your code will work. I would try it out on Monday.
Right now, I am closing this question.
If I face any problem , I would ask here.
You people are lovely. Help others for nothing.
Best Regards
Mateen
You may , please, have a look at
https://www.experts-exchange.com/questions/21849882/To-download-a-file-from-within-pb7-script.html
Thanks for replying on Sunday.
I am sure your code will work. I would try it out on Monday.
Right now, I am closing this question.
If I face any problem , I would ask here.
You people are lovely. Help others for nothing.
Best Regards
Mateen
You may , please, have a look at
https://www.experts-exchange.com/questions/21849882/To-download-a-file-from-within-pb7-script.html
ASKER
Hi sandeep
I have tested your code today.
Nothing happened.
I have tested your code today.
Nothing happened.
Hi,
what you mean by nothing happened ??? It worked or not ? :(
And for your previous question why don't you add that logic in this logic itself. No need to open and write the same file again and again.
li_filenum = FileOpen(ls_filename,LineM ode!,Read! ,LockWrite !)
li_filenum2 = FileOpen(ls_temp,LineMode! ,Write!,Lo ckWrite!,R eplace!)
//// Add header line code ////
FileWrite(li_temp, "kse_code,rate_date,open_r ate,high_r ate,low_ra te,close_r ate,turnov er")
DO while loop....
...
and rest of the logic
Regards,
Sandeep
what you mean by nothing happened ??? It worked or not ? :(
And for your previous question why don't you add that logic in this logic itself. No need to open and write the same file again and again.
li_filenum = FileOpen(ls_filename,LineM
li_filenum2 = FileOpen(ls_temp,LineMode!
//// Add header line code ////
FileWrite(li_temp, "kse_code,rate_date,open_r
DO while loop....
...
and rest of the logic
Regards,
Sandeep
ASKER
Hi Sandeep
No replacement occured.
I tried
/*
LOGIC : Sandeep Patel
1) open original file in line by line read mode and temporary file to write in it.
2) read from original file line by line and check for all occurance of old value in it and replace with new value.
3) write new modified line to temporary file
4) close both files.
5) now again open original file in write mode with replace mode and temporary file in read mode
6) read each line from temporary file and write in original file
7) close both files.
*/
integer li_filenum,li_filenum2
string ls_line
blob lb_text
string ls_filename
string ls_temp
long start_pos=1
string ls_old_value, ls_new_value , ls_mystring
ls_old_value = "12 05 2006"
ls_new_value= "2006/05/12"
ls_filename = 'd:\\download\\120052006.t xt' // original file
ls_temp = 'c:\\temp\\test_copy.txt' // Temporary file
li_filenum = FileOpen(ls_filename,LineM ode!,Read! ,LockWrite !)
li_filenum2 = FileOpen(ls_temp,LineMode! ,Write!,Lo ckWrite!,R eplace!)
Do While FileRead(li_filenum,ls_lin e) > 0
ls_mystring = ls_line
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos)
// Only enter the loop if you find old_str.
DO WHILE start_pos > 0
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
// Find the next occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos+Len(ls_new_value ))
LOOP
ls_line = ls_mystring
FileWrite(li_filenum2,ls_l ine)
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
li_filenum = FileOpen(ls_temp,LineMode! ,Read!,Loc kWrite!)
li_filenum2 = FileOpen(ls_filename,LineM ode!,Write !,LockWrit e!,Replace !)
Do While FileRead(li_filenum,ls_lin e) > 0
FileWrite(li_filenum2,ls_l ine)
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
//======================== ===
<<And for your previous question why don't you add that logic in this logic itself. No need to open and write the same file again and again.>>
Once all thing done, I would convert into one.
No replacement occured.
I tried
/*
LOGIC : Sandeep Patel
1) open original file in line by line read mode and temporary file to write in it.
2) read from original file line by line and check for all occurance of old value in it and replace with new value.
3) write new modified line to temporary file
4) close both files.
5) now again open original file in write mode with replace mode and temporary file in read mode
6) read each line from temporary file and write in original file
7) close both files.
*/
integer li_filenum,li_filenum2
string ls_line
blob lb_text
string ls_filename
string ls_temp
long start_pos=1
string ls_old_value, ls_new_value , ls_mystring
ls_old_value = "12 05 2006"
ls_new_value= "2006/05/12"
ls_filename = 'd:\\download\\120052006.t
ls_temp = 'c:\\temp\\test_copy.txt' // Temporary file
li_filenum = FileOpen(ls_filename,LineM
li_filenum2 = FileOpen(ls_temp,LineMode!
Do While FileRead(li_filenum,ls_lin
ls_mystring = ls_line
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos)
// Only enter the loop if you find old_str.
DO WHILE start_pos > 0
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
// Find the next occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos+Len(ls_new_value
LOOP
ls_line = ls_mystring
FileWrite(li_filenum2,ls_l
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
li_filenum = FileOpen(ls_temp,LineMode!
li_filenum2 = FileOpen(ls_filename,LineM
Do While FileRead(li_filenum,ls_lin
FileWrite(li_filenum2,ls_l
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
//========================
<<And for your previous question why don't you add that logic in this logic itself. No need to open and write the same file again and again.>>
Once all thing done, I would convert into one.
Hi,
first check that is it creating a temp file at c:\\temp\\test_copy.txt. If not it means you don't have rights to write at c:\\temp or may be temp folder not exist at c: drive. If it is creating file then put message boxes in do while loop and check the value of variable ls_mystring before replace and after replace
Do While FileRead(li_filenum,ls_lin e) > 0
ls_mystring = ls_line
messagebox('before', ls_mystring)
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos)
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
messagebox('after',ls_myst ring)
ls_line = ls_mystring
FileWrite(li_filenum2,ls_l ine)
Loop
Hope it will help you to debug the code.
Regards,
Sandeep
first check that is it creating a temp file at c:\\temp\\test_copy.txt. If not it means you don't have rights to write at c:\\temp or may be temp folder not exist at c: drive. If it is creating file then put message boxes in do while loop and check the value of variable ls_mystring before replace and after replace
Do While FileRead(li_filenum,ls_lin
ls_mystring = ls_line
messagebox('before', ls_mystring)
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos)
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
messagebox('after',ls_myst
ls_line = ls_mystring
FileWrite(li_filenum2,ls_l
Loop
Hope it will help you to debug the code.
Regards,
Sandeep
ASKER
Hi sandeep,
I tried my level best to make it work myself, but could not succeed.
However I have found the problem area.
messagebox('ls_mystring_be fore',ls_m ystring) // your debug line
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos)
MESSAGEBOX('before_pos_val ue',string (start_pos )) // my debug line
// Only enter the loop if you find old_str.
DO WHILE start_pos > 0
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
messagebox('ls_mystring_af ter',ls_my string) //your debug line
// Find the next occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos+Len(ls_new_value ))
messagebox('AFTER_pos_valu e',string( start_pos) ) // my debug line
ls_mystring is always having correct line.
The problem lies in the variable start_pos
It shows a value of 6 one time and then throught the loop it shows value of zero. The net result is that the replacement is made in one only one line of the entire file.
I tried my level best to make it work myself, but could not succeed.
However I have found the problem area.
messagebox('ls_mystring_be
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos)
MESSAGEBOX('before_pos_val
// Only enter the loop if you find old_str.
DO WHILE start_pos > 0
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
messagebox('ls_mystring_af
// Find the next occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos+Len(ls_new_value
messagebox('AFTER_pos_valu
ls_mystring is always having correct line.
The problem lies in the variable start_pos
It shows a value of 6 one time and then throught the loop it shows value of zero. The net result is that the replacement is made in one only one line of the entire file.
Hi
as i told you that there is no chance to repeat the date in a single line in your case, you can remove inner do while loop
let me put entire code again. Also while removing inner loop i forget to change one more thing.
Please copy this code. I have tested it, it's working fine. Only you need to change the name of files.
integer li_filenum,li_filenum2
string ls_line
blob lb_text
string ls_filename
string ls_temp
long start_pos=1
string ls_old_value, ls_new_value , ls_mystring
ls_old_value = "12 05 2006"
ls_new_value= "2006/05/12"
ls_filename = 'c:\temp\test.txt' // original file
ls_temp = 'c:\temp\test_copy.txt' // Temporary file
li_filenum = FileOpen(ls_filename,LineM ode!,Read! ,LockWrite !)
li_filenum2 = FileOpen(ls_temp,LineMode! ,Write!,Lo ckWrite!,R eplace!)
FileWrite(li_filenum2,'kse _code,rate _date,open _rate,high _rate,low_ rate,close _rate,turn over')
Do While FileRead(li_filenum,ls_lin e) > 0
ls_mystring = ls_line
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, 1)
// Only enter the loop if you find old_str.
If start_pos > 0 Then
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
ls_line = ls_mystring
End If
FileWrite(li_filenum2,ls_l ine)
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
li_filenum = FileOpen(ls_temp,LineMode! ,Read!,Loc kWrite!)
li_filenum2 = FileOpen(ls_filename,LineM ode!,Write !,LockWrit e!,Replace !)
Do While FileRead(li_filenum,ls_lin e) > 0
FileWrite(li_filenum2,ls_l ine)
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
Regards,
Sandeep
as i told you that there is no chance to repeat the date in a single line in your case, you can remove inner do while loop
let me put entire code again. Also while removing inner loop i forget to change one more thing.
Please copy this code. I have tested it, it's working fine. Only you need to change the name of files.
integer li_filenum,li_filenum2
string ls_line
blob lb_text
string ls_filename
string ls_temp
long start_pos=1
string ls_old_value, ls_new_value , ls_mystring
ls_old_value = "12 05 2006"
ls_new_value= "2006/05/12"
ls_filename = 'c:\temp\test.txt' // original file
ls_temp = 'c:\temp\test_copy.txt' // Temporary file
li_filenum = FileOpen(ls_filename,LineM
li_filenum2 = FileOpen(ls_temp,LineMode!
FileWrite(li_filenum2,'kse
Do While FileRead(li_filenum,ls_lin
ls_mystring = ls_line
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, 1)
// Only enter the loop if you find old_str.
If start_pos > 0 Then
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
ls_line = ls_mystring
End If
FileWrite(li_filenum2,ls_l
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
li_filenum = FileOpen(ls_temp,LineMode!
li_filenum2 = FileOpen(ls_filename,LineM
Do While FileRead(li_filenum,ls_lin
FileWrite(li_filenum2,ls_l
Loop
FileClose(li_filenum)
FileClose(li_filenum2)
Regards,
Sandeep
ASKER
Hi Sandeep,
This time it is 100% ok.
Thanks for all the valuable time u devoted 4 me.
My very best regards
Mateen
This time it is 100% ok.
Thanks for all the valuable time u devoted 4 me.
My very best regards
Mateen
one more thing to add if there is no chance to repeat the date in a line you can remove do while loop.
Do While FileRead(li_filenum,ls_lin
ls_mystring = ls_line
// Find the first occurrence of old_str.
start_pos = Pos(ls_mystring, ls_old_value, start_pos)
// Replace old_str with new_str.
ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
ls_line = ls_mystring
FileWrite(li_filenum2,ls_l
Loop
Regards,
Sandeep