swap lines of text in a text file

February 19, 2009 at 05:43:45
Specs: Windows 2000 SP4
Hi,

I have a text file that has hundreds of lines of text. I need to read all the lines in the text file and then switch the 2 lines around.

So i have Text A and Text B in this text file called peters.txt that has many lines of text. I want to put Text B in place of Text A and Text A in place of Text B.

The example text i want it to swap is the lines below:

0PW222222PeterPiper PIP Peterpan, Panland

0PW222222PeterPaner PAN Peterpan, Panland

I want it to switch it in the same file like below so the second line is where the first line was and vice versa:

0PW222222PeterPaner PAN Peterpan, Panland

0PW222222PeterPiper PIP Peterpan, Panland

Can you help please?


See More: swap lines of text in a text file

Report •


#1
February 19, 2009 at 06:08:36
TRIPLE POST!!!!!!!!! please be patient, you are flooding the forum.

if you really want to do it in C/C++, then its probably school assignment. i cant help you to graduate with no skill.

if not, then..
use notepad. click Edit-Replace:
1. Replace TEXTA->TEMP
2. Replace TEXTB->TEXTA
3. Replace TEMP ->TEXTB
done.


Report •

#2
February 19, 2009 at 08:59:31
Hi Sorry,

Its not a school assignment.

I need to automate it so i can add the command for it to run from a batch file so when the other procedures are run, i can just add this one in as it needs to be done every 2 week cycles.

So i cannot use notepad, I have had a try in vb6 and C++ but have failed, i am a newcomer trying to learn but cannot figure it out. The data provided is dummy data.

My try in vb6 is displayed below:

Private Sub Command1_Click()

Dim sTemp1
Dim sTemp2
sTemp1 = "0PW222222PeterPiper PIP Peterpan, Panland"
sTemp2 = "0PW222222PeterPaner PAN Peterpan, Panland"

Dim sTemp$
Dim fso As New FileSystemObject
Dim fsoTS As TextStream

Open "C:\tester\testnew\test.txt" For Input As #1
While Not EOF(1)
If sTemp$ = sTemp1 Then
'not sure what goes here
End If
Line Input #1, temp$
Wend

Close #1
End Sub

I dont mind vb6, c++ or in a batch file. Thanks


Report •

#3
February 19, 2009 at 14:48:26
Are the two lines adjacent to each other?
or can they be spaced n lines apart.
Does line A always come before line B?


Report •

Related Solutions

#4
February 19, 2009 at 15:12:06
Line A is always directly on top of Line B, like below:

0PW222222PeterPiper PIP Peterpan, Panland
0PW222222PeterPaner PAN Peterpan, Panland

When i run the exe i would be looking for it switch the lines like below so they are both present but the other way around:

0PW222222PeterPaner PAN Peterpan, Panland
0PW222222PeterPiper PIP Peterpan, Panland

There are around 1000 lines in the file that these lines are present in. Thank you for your response Fred


Report •

#5
February 19, 2009 at 18:12:11
If line b always follows line a:

pseudo code:
(I don't use VB but other free (open source)
BASICS)

test$ = "find"

open #1 input file
open #2 output file

while not eof(1)

line input #1, A$
if test$ = A$ then
hold$ = A$
line input #1, A$
print #2, A%
print #2, hold$
goto continue
endif
print #2, A$

continue:
wend

close #2 output
close #1 input



Report •

#6
February 19, 2009 at 20:32:25
If you're a newbir to all these languages, it would be a real good idea to go back to 'hello world' nf work your way along.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#7
February 20, 2009 at 00:04:09
hey M2. You're always hard at it with the batch. I'm a 40 year user of BASIC and this was a relatively easy problem that the OP started.

Report •

#8
February 20, 2009 at 00:55:16
here is a hint: (tested on vb6)

read file:

    ff = FreeFile
    Open filename For Input As ff
    SwitchString = Input$(LOF(ff), #ff)
    Close ff

writefile:

    ff = FreeFile
    Open filename For Output As ff
    Print #ff, SwitchString
    Close ff

Replace function

    SwitchString = Replace(SwitchString, Text2, Text1)


Report •

#9
February 20, 2009 at 01:29:36
Do i replace filename with the name of my file?I still cant get it to work, it keeps giving me an errors.

Thank you all for your responses, especially reno and fred.


Report •

#10
February 20, 2009 at 03:01:12
I am using sequential input and output. The input is your file nqme. Output is a temp name. If you need the original name you have to delete input and rename output.

Report •

#11
February 20, 2009 at 03:47:42
"I'm a 40 year user of BASIC"

Did you invent it?

:(


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#12
February 20, 2009 at 04:15:59
Hi I have figured it out, thanks guys. I did it in vb6. How do i check if the output file already exists and if it does i want to delete it?

Report •

#13
February 20, 2009 at 04:27:25

Public Function FileExist(ByVal path As String, Optional flag As Integer) As Boolean
On Error GoTo ErrHandler
    FileExist = False
    If (flag = vbDirectory) Then
        path = Dir(path, vbDirectory)
    Else
        path = Dir(path)
    End If
    If Not IsNull(path) And path <> "" Then FileExist = True
    
ExitHere:
    Exit Function
ErrHandler:
    FileExist = False
    If Err <> 0 Then MsgBox "Error " & Err.Number & ":(FileExist) -->" & Err.Description, , "Error"
    Resume ExitHere
End Function


Report •

#14
February 20, 2009 at 10:51:46
M2 I first used basic on a tty terminal with punched tape for program backup. Before the PC on a HP mainframe time share system.

Report •

#15
February 20, 2009 at 14:50:41
Hi, i have got both lines switched, and they should be separated by this ascii character: � I want to enter this in the InStr that i have that writes the switched line out, what do i need to do to add that in? Does anyone know? After googling it i am getting that it equals , how do i add that symbol into vb6 so it outputs that before the line is switched.

Report •

#16
February 20, 2009 at 15:24:24
Use the ascii value of the character. Print
chr$(nn).

Report •

#17
March 4, 2009 at 03:15:28
thank you for your help, finally got it working.

Report •

#18
March 5, 2009 at 08:33:12
Hi, now i have another problem with the same swap, basically, the line symbol that separates the two lines seems to be a unix stlye carriage return that looks like a small rectangle, how can i insert this in either the vb string or even outside of the string. thanks

Report •

#19
March 5, 2009 at 09:16:32
1. vbnewline
2. vbcrlf
3. chr(13)&chr(10) or is it chr(10)&chr(13)

feel free to choose


Report •

#20
March 9, 2009 at 05:20:04
Hi, the one i finally used was vblf which displayed the correct symbol. Thanks for you reply reno.

Final problem to this application i have is that when i run the executable i have hard coded the file path in, where as i really want the user to insert the path in and then run the exe in that location. how do i do this, i have google it and have had some attempts but its not bringing up the cmd window to allow me to enter the path.

If Command$ <> Empty Then
'// we have a file to open
OpenFile Command$ '// call your openfile procedure
End If


Report •

#21
March 9, 2009 at 06:42:34
better way is to have a button that open up a browse dialog, that call windows api "GetOpenFileNameA".
here is the declaration code:
Declare Function GetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean

google for it.

i am using the code originally written by Ken Getz with little modification.
SOURCE: http://www.mvps.org/access/api/api0...
i believe there is a wrapper code somewhere written for vb6, you just have to google it out.

errr, btw, vb6 dont have access to cmd window.
something similar to cmd window is console api, similar but not same.

as for command line argument:
i dont see any error in it. try msgbox it, eg. MsgBox "'" & Command$ & "'"


Report •

#22
March 9, 2009 at 07:26:27
I dont want to open the file, I just want to set the path for the filename.

Pathname = "d:\peterpan.txt"

The pathname code is hardcoded as you can see, i just want the user to set the path as it is different on every system. Is there anyway i can do this without using a button control. thanks


Report •

#23
March 9, 2009 at 09:21:21
hmm, i dont quite understand, maybe this one what you are looking for. i though you are looking for passing file name from command-line.
filename = app.path & "\peterpan.txt"

or is it you are looking to get the pathname of "d:\peterpan.txt"?? so the result is "d:\"


Report •

#24
March 10, 2009 at 06:02:04
The question i had was that i want to pass in the pathname and filename into the application as arguments when it is run from the command line.

the way i have done this is by using the command$ in my application, and then just adding the argument in the cmd window. thanks for your help it was most useful


Report •


Ask Question