Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

edit a text file 3

Status
Not open for further replies.

patrick118

Technical User
Jan 14, 2004
315
NL
We have the following problem.

From a certain program we get some txt files but we can't import them correctly. The file has x number of lines but we want every 4 lines to come on 1 line.

example:
original file
yes
no
ok
bye
hello
good
no
ok

What i would like
yesnookbye
hellogoodnook

Is this possible with vbs and can you please help me. I'm not really good in vbs scripts.

Thankx for all you help

Patrick
 
This should do the trick, test.txt is the file name located in to same folder as the script.

i have just got it to put the results in a message box



Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTF = objFSO.OpenTextFile("test.txt")
vText = ""
do while not objTF.AtEndofStream
for i = 1 to 4
vText = vText & objTF.Readline
next 'i
msgbox vText
loop

set objTF = nothing
 
that is great.

Works like a charm now. How do i write it to another textfile?

If you can answer that i am a happy man.

Patrick
 
Set objFSO = CreateObject("Scripting.FileSystemObject")

'input text file
Set objTF = objFSO.OpenTextFile("test.txt")

'output text file
Set f = objFSO.OpenTextFile("output.txt",2,true)

do while not objTF.AtEndofStream
vText = ""
for i = 1 to 4
vText = vText & objTF.Readline
next 'i
f.Writeline( vText)
loop
f.close
Set f = nothing
Set objTF = nothing
Set objFSO = nothing
 
Hello patrick118,

You may run into trouble if the original file contains number of lines not divisible by 4. This is one way how to do it in a general setting.
Code:
file_org="d:\test\abc_org.txt"
file_tfm="d:\test\abc_tfm.txt"

set fso=createobject("scripting.filesystemobject")
if not fso.fileexists(file_org) then
	wscript.echo "Original file does not exist. Operation aborted."
	set fso=nothing : wscript.quit(1)
end if
set ots=fso.opentextfile(file_org,1,false)
s_org=ots.readall
ots.close : set ots=nothing

a_org=split(s_org,vbcrlf)
dim a_tfm() : redim a_tfm(ubound(a_org)\4)
for i=0 to ubound(a_org)\4 
	a_tfm(i)=""
	on error resume next
	for j=0 to (4-1)
		a_tfm(i)=a_tfm(i) & a_org(i*4+j)
		if err<>0 then exit for : err.clear
	next
	on error goto 0
next
set ots=fso.opentextfile(file_tfm,2,true)
ots.write join(a_tfm,vbcrlf)
ots.close : set ots=nothing

set fso=nothing
regards - tsuji
 
Duch and Tsuji

Thank you very much. Both of the scripts are working great and you really helped me out.

Thanks

PAtrick
 
Another way:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile("test.txt") 'input text file
Set f = objFSO.OpenTextFile("output.txt", 2, True) 'output text file
vText = "": i = 0
Do While Not objTF.AtEndofStream
i = i + 1: vText = vText & objTF.Readline
If i Mod 4 = 0 Then
f.Writeline vText
vText = ""
End If
Loop
f.Close: objTF.Close
Set f = Nothing: Set objTF = Nothing: Set objFSO = Nothing

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV thanks.

It's also a way but allready got the working script but perhaps someone else got an idea out of it.

Patrick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top