easy to do, open first file and split, same way as second file
use instr to find position of "(" and again to find position of ")"
then use mid to join the part upto and including "(", & the replacement from the first file array & mid again the part from and including ")" advance the index for the array from first file so that it will put the next string each time
lastly use join for the array of lines, open the second file again, this time for output then print the joined string to the file and close
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Open "second.txt" For Output As f1 'overwrite updated data to file
Print #f1, join(arrsecond, vbNewLine)
Close f1
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
i or j has gone past it's valid value
i probably can't do, so it is most likly j, this will happen if more replacements are needed than in first file
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
i have no idea how it can make a replacement that is not in the list, post your complete code, also the text files if possible so i can try it
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
i hope u can help me with this because the first file is written in arabic
newoutfile.txt is the first file
and outfile.txt is the second file that i need to change
i downloaded your textfiles and ran the code as i posted above, i just copied from the post, changed the file names, it gave no errors and appeared to work right
here is the output file
Edit: you were quite right, i could not understand the arabic at all, which is why i say appeared to work, you can check it out
Last edited by westconn1; Nov 2nd, 2006 at 03:58 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
some of the replacement text (the arabic) is unicode, i don't know too much about unicode, but read this post here http://www.vbforums.com/showthread.php?t=365738, about unicode in vb6, there will be many other posts on converting to unicode, vb automaically converts the unicode from the file to ANSI as soon as it reads the file, if you read the file as a byte array you then have the values to get the unicode character from the 2 bytes that make the unicode character, but i am unable to tell you how to put the unicode into your string and save it.
after typing all this i just made it work so try this code
VB Code:
Sub om()
Dim arrfirst() As Byte, arrsecond() As String, i As Long, j As Long
Dim pos As Long, pos1 As Long, strt As Long, arra() As Byte, arrb() As Byte
f1 = FreeFile
Open "C:\Documents and Settings\peter\My Documents\newoutfile.txt" For Input As f1 ' add path to file
arrfirst = Input(LOF(f1), #f1)
Close f1
For i = 0 To UBound(arrfirst)
Debug.Print arrfirst(i)
Next
Open "C:\Documents and Settings\peter\My Documents\outfile.txt" For Input As f1
arrsecond = split(Input(LOF(f1), #f1), vbNewLine)
Close f1
Open "C:\Documents and Settings\peter\My Documents\testoutfile.txt" For Output As f1 'overwrite updated data to file
strt = 0
For i = 0 To UBound(arrsecond)
If Left(arrsecond(i), 10) = " SOLUTION" Then ' from your post though don't look quite right
i am not sayin the coding is perfect, but try it out and see if it is correct, i also attatch the result file
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
but i need to overwrite in the outfile not in testoutfile
i mean without extracting the solutions lines and put them in other file
i need all information and the same arrangment of outfile
because most of my codes depends on outfile.txt arrangment
i just change it to a separate output file, so i could run the code again without keep recopying the files after they had been overwritten
just change the filename for output back to the one you want, it makes no difference
as i said i am not sure that this is the proper way to code, so someone might put up some improvments
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
no i think that you did't understand i need to keep all information in the outfile ... if i just change the files names i will lose some data that i need in other functoins in outfile.txt
i attached a sample of output with all information i need which i didn't got in file outfile.txt
and the second one (outfile2.txt) is what i'm getting know
Open "C:\Documents and Settings\peter\My Documents\testoutfile.txt" For Output As f1 'overwrite updated data to file
ReDim bom(5)
For i = 0 To 5 'read BOM from file
bom(i) = arrfirst(i)
Next
Print #f1, bom;
strt = 6 ' start after BOM
For i = 0 To UBound(arrsecond)
If Left(arrsecond(i), 10) = " SOLUTION" Then ' find line for replace
pos = InStr(arrsecond(i), "(") 'start position
pos1 = InStr(arrsecond(i), ")") ' end position
rpl = getnext(arrfirst, strt) ' to get the first line from the byte array
strt = strt + (Len(rpl) * 2) + 4 ' next starting position in byte array
' print the line to file, with replacement
Print #f1, Mid(arrsecond(i), 1, pos); rpl; Mid(arrsecond(i), pos1, Len(arrsecond(i)) - pos1 + 1) ' print the line to file, with replacement
Else
Print #f1, arrsecond(i) '; rpl
End If
Next
Close f1
End Sub
Function getnext(arri() As Byte, strt As Long)
Dim i As Long, arrb() As Byte
ReDim arrb(100)
For i = strt To UBound(arri)
If arri(i) = 13 Then
Exit For
Else
arrb(i - strt) = arri(i)
End If
Next
ReDim Preserve arrb(i - strt - 1)
getnext = arrb
End Function
Last edited by westconn1; Nov 6th, 2006 at 04:49 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Open "C:\Documents and Settings\peter\My Documents\testoutfile.txt" For Output As f1 'overwrite updated data to file
ReDim bom(5)
bom(0) = &HEF
bom(2) = &HBB
bom(4) = &HBF
Print #f1, bom;
For i = 0 To UBound(arrsecond)
If Left(arrsecond(i), 10) = " SOLUTION" Then ' find line for replace
pos = InStr(arrsecond(i), "(") 'start position
pos1 = InStr(arrsecond(i), ")") ' end position
' print the line to file, with replacement
Print #f1, Mid(arrsecond(i), 1, pos); arrfirst(j); Mid(arrsecond(i), pos1, Len(arrsecond(i)) - pos1 + 1) ' print the line to file, with replacement
j = j + 1
Else
Print #f1, arrsecond(i) '; rpl
End If
Next
Close f1
End Sub
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete