Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - string manipulation - join lines

Reply
 
Old 07-12-2009   #1 (permalink)
James


 
 

string manipulation - join lines

I want to take some text like below and join 4 lines together separated
by semi colons. Each line would have some text added to the beginning
and end of the line. If the line contained the number 234234 the line
would be skipped

asdf
adaffad
adfafasdf
adfaeqe
234234
erwe
sdfs
sdfd
sdfgsfd


The result would look like this

extra text;asdf;adaffad;adfafasdf;adfaeqe;extra text
extra text;erwe;sdfs;sdfd;sdfgsfdextra text

What kind of looping structure would I use to achieve this?

Thanks,

James

My System SpecsSystem Spec
Old 07-12-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: string manipulation - join lines


"James" <jwanders@xxxxxx> wrote in message
news:eoe6m.42500$_S2.33452@xxxxxx
Quote:

>I want to take some text like below and join 4 lines together separated by
>semi colons. Each line would have some text added to the beginning and end
>of the line. If the line contained the number 234234 the line would be
>skipped
>
> asdf
> adaffad
> adfafasdf
> adfaeqe
> 234234
> erwe
> sdfs
> sdfd
> sdfgsfd
>
>
> The result would look like this
>
> extra text;asdf;adaffad;adfafasdf;adfaeqe;extra text
> extra text;erwe;sdfs;sdfd;sdfgsfdextra text
>
> What kind of looping structure would I use to achieve this?
>
> Thanks,
>
> James
Presumably your lines come from a text line. If so then something like this
would work:

sString = "Extra Text;"
while not oFile.AtEndOfStream
sLine = oFile.ReadLine
if sLine = "234234" then
wscript.echo sString & "Extra text"
sString = "Extra Text;"
else
sString = sString & sLine & ";"
end if
wend
wscript.echo sString & "Extra text"

I recommend you download the helpfile script56.chm from the Microsoft site
to see fully documented examples of all basic VB Script functions.


My System SpecsSystem Spec
Old 07-12-2009   #3 (permalink)
James


 
 

Re: string manipulation - join lines

Pegasus [MVP] wrote:
Quote:

> "James" <jwanders@xxxxxx> wrote in message
> news:eoe6m.42500$_S2.33452@xxxxxx
Quote:

>> I want to take some text like below and join 4 lines together separated by
>> semi colons. Each line would have some text added to the beginning and end
>> of the line. If the line contained the number 234234 the line would be
>> skipped
>>
>> asdf
>> adaffad
>> adfafasdf
>> adfaeqe
>> 234234
>> erwe
>> sdfs
>> sdfd
>> sdfgsfd
>>
>>
>> The result would look like this
>>
>> extra text;asdf;adaffad;adfafasdf;adfaeqe;extra text
>> extra text;erwe;sdfs;sdfd;sdfgsfdextra text
>>
>> What kind of looping structure would I use to achieve this?
>>
>> Thanks,
>>
>> James
>
> Presumably your lines come from a text line. If so then something like this
> would work:
>
> sString = "Extra Text;"
> while not oFile.AtEndOfStream
> sLine = oFile.ReadLine
> if sLine = "234234" then
> wscript.echo sString & "Extra text"
> sString = "Extra Text;"
> else
> sString = sString & sLine & ";"
> end if
> wend
> wscript.echo sString & "Extra text"
>
> I recommend you download the helpfile script56.chm from the Microsoft site
> to see fully documented examples of all basic VB Script functions.
>
>
Thanks, I've downloaded the file but when I open it no informaton in
the right pane is displayed. Instead it says "Navigation to the webpage
was canceled"

James
My System SpecsSystem Spec
Old 07-12-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: string manipulation - join lines


"James" <jwanders@xxxxxx> wrote in message
news:6af6m.42501$_S2.39160@xxxxxx
Quote:

> Pegasus [MVP] wrote:
Quote:

>> "James" <jwanders@xxxxxx> wrote in message
>> news:eoe6m.42500$_S2.33452@xxxxxx
Quote:

>>> I want to take some text like below and join 4 lines together separated
>>> by semi colons. Each line would have some text added to the beginning
>>> and end of the line. If the line contained the number 234234 the line
>>> would be skipped
>>>
>>> asdf
>>> adaffad
>>> adfafasdf
>>> adfaeqe
>>> 234234
>>> erwe
>>> sdfs
>>> sdfd
>>> sdfgsfd
>>>
>>>
>>> The result would look like this
>>>
>>> extra text;asdf;adaffad;adfafasdf;adfaeqe;extra text
>>> extra text;erwe;sdfs;sdfd;sdfgsfdextra text
>>>
>>> What kind of looping structure would I use to achieve this?
>>>
>>> Thanks,
>>>
>>> James
>>
>> Presumably your lines come from a text line. If so then something like
>> this would work:
>>
>> sString = "Extra Text;"
>> while not oFile.AtEndOfStream
>> sLine = oFile.ReadLine
>> if sLine = "234234" then
>> wscript.echo sString & "Extra text"
>> sString = "Extra Text;"
>> else
>> sString = sString & sLine & ";"
>> end if
>> wend
>> wscript.echo sString & "Extra text"
>>
>> I recommend you download the helpfile script56.chm from the Microsoft
>> site to see fully documented examples of all basic VB Script functions.
> Thanks, I've downloaded the file but when I open it no informaton in the
> right pane is displayed. Instead it says "Navigation to the webpage was
> canceled"
>
> James
Google is your friend. When you type "Navigation to the webpage was
canceled" and "chm" into a search box then you are guided to sites such as
this one: http://weblog.helpware.net/?p=36.


My System SpecsSystem Spec
Old 07-13-2009   #5 (permalink)
James


 
 

Re: string manipulation - join lines

Pegasus [MVP] wrote:
Quote:

> "James" <jwanders@xxxxxx> wrote in message
> news:6af6m.42501$_S2.39160@xxxxxx
Quote:

>> Pegasus [MVP] wrote:
Quote:

>>> "James" <jwanders@xxxxxx> wrote in message
>>> news:eoe6m.42500$_S2.33452@xxxxxx
>>>> I want to take some text like below and join 4 lines together separated
>>>> by semi colons. Each line would have some text added to the beginning
>>>> and end of the line. If the line contained the number 234234 the line
>>>> would be skipped
>>>>
>>>> asdf
>>>> adaffad
>>>> adfafasdf
>>>> adfaeqe
>>>> 234234
>>>> erwe
>>>> sdfs
>>>> sdfd
>>>> sdfgsfd
>>>>
>>>>
>>>> The result would look like this
>>>>
>>>> extra text;asdf;adaffad;adfafasdf;adfaeqe;extra text
>>>> extra text;erwe;sdfs;sdfd;sdfgsfdextra text
>>>>
>>>> What kind of looping structure would I use to achieve this?
>>>>
>>>> Thanks,
>>>>
>>>> James
>>> Presumably your lines come from a text line. If so then something like
>>> this would work:
>>>
>>> sString = "Extra Text;"
>>> while not oFile.AtEndOfStream
>>> sLine = oFile.ReadLine
>>> if sLine = "234234" then
>>> wscript.echo sString & "Extra text"
>>> sString = "Extra Text;"
>>> else
>>> sString = sString & sLine & ";"
>>> end if
>>> wend
>>> wscript.echo sString & "Extra text"
>>>
>>> I recommend you download the helpfile script56.chm from the Microsoft
>>> site to see fully documented examples of all basic VB Script functions.
>> Thanks, I've downloaded the file but when I open it no informaton in the
>> right pane is displayed. Instead it says "Navigation to the webpage was
>> canceled"
>>
>> James
>
> Google is your friend. When you type "Navigation to the webpage was
> canceled" and "chm" into a search box then you are guided to sites such as
> this one: http://weblog.helpware.net/?p=36.
>
>
Thank you.

I'm not just joining all lines together. I know how to do that. I still
can't figure out how I can join only four lines at a time like in the
above example. After I've joined the first four lines I need to join
the next four and so on. What kind of looping structure would I use?

Thanks,

My System SpecsSystem Spec
Old 07-13-2009   #6 (permalink)
Pegasus [MVP]


 
 

Re: string manipulation - join lines


"James" <jwanders@xxxxxx> wrote in message
news:fIy6m.2024$nU7.1580@xxxxxx
Quote:

> Pegasus [MVP] wrote:
Quote:

>> "James" <jwanders@xxxxxx> wrote in message
>> news:6af6m.42501$_S2.39160@xxxxxx
Quote:

>>> Pegasus [MVP] wrote:
>>>> "James" <jwanders@xxxxxx> wrote in message
>>>> news:eoe6m.42500$_S2.33452@xxxxxx
>>>>> I want to take some text like below and join 4 lines together
>>>>> separated by semi colons. Each line would have some text added to the
>>>>> beginning and end of the line. If the line contained the number
>>>>> 234234 the line would be skipped
>>>>>
>>>>> asdf
>>>>> adaffad
>>>>> adfafasdf
>>>>> adfaeqe
>>>>> 234234
>>>>> erwe
>>>>> sdfs
>>>>> sdfd
>>>>> sdfgsfd
>>>>>
>>>>>
>>>>> The result would look like this
>>>>>
>>>>> extra text;asdf;adaffad;adfafasdf;adfaeqe;extra text
>>>>> extra text;erwe;sdfs;sdfd;sdfgsfdextra text
>>>>>
>>>>> What kind of looping structure would I use to achieve this?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> James
>>>> Presumably your lines come from a text line. If so then something like
>>>> this would work:
>>>>
>>>> sString = "Extra Text;"
>>>> while not oFile.AtEndOfStream
>>>> sLine = oFile.ReadLine
>>>> if sLine = "234234" then
>>>> wscript.echo sString & "Extra text"
>>>> sString = "Extra Text;"
>>>> else
>>>> sString = sString & sLine & ";"
>>>> end if
>>>> wend
>>>> wscript.echo sString & "Extra text"
>>>>
>>>> I recommend you download the helpfile script56.chm from the Microsoft
>>>> site to see fully documented examples of all basic VB Script functions.
>>> Thanks, I've downloaded the file but when I open it no informaton in
>>> the right pane is displayed. Instead it says "Navigation to the webpage
>>> was canceled"
>>>
>>> James
>>
>> Google is your friend. When you type "Navigation to the webpage was
>> canceled" and "chm" into a search box then you are guided to sites such
>> as this one: http://weblog.helpware.net/?p=36.
> Thank you.
>
> I'm not just joining all lines together. I know how to do that. I still
> can't figure out how I can join only four lines at a time like in the
> above example. After I've joined the first four lines I need to join the
> next four and so on. What kind of looping structure would I use?
>
> Thanks,
>
Did you try the sample code I posted? Let's have a look at your current
code!


My System SpecsSystem Spec
Old 07-13-2009   #7 (permalink)
Larry Serflaten


 
 

Re: string manipulation - join lines


"James" <jwanders@xxxxxx> wrote

Quote:

> I'm not just joining all lines together. I know how to do that. I still
> can't figure out how I can join only four lines at a time like in the
> above example. After I've joined the first four lines I need to join
> the next four and so on. What kind of looping structure would I use?
You'd first Split the lines out to an array so that each line has its own index.
You know then, that you want to get 4, skip 1, get 4, skip 1 and so on,
adding your extra text as you go:

Data = Split(...)
idx = 0
text = ""
For i = 1 to Ubound(Data) \ 5
text = text & "start text;"
For j = 1 to 4
text = text & Data(idx) & ";"
idx = idx + 1
Next
text = text & "end text" & vbCrLf
idx = idx + 1
Next

The inner loop gets your 4 items and the outer loop
handles your extra text and skipping the 5th item.

LFS



My System SpecsSystem Spec
Old 07-14-2009   #8 (permalink)
James


 
 

Re: string manipulation - join lines

Larry Serflaten wrote:
Quote:

> "James" <jwanders@xxxxxx> wrote
>
>
Quote:

>> I'm not just joining all lines together. I know how to do that. I still
>> can't figure out how I can join only four lines at a time like in the
>> above example. After I've joined the first four lines I need to join
>> the next four and so on. What kind of looping structure would I use?
>
> You'd first Split the lines out to an array so that each line has its own index.
> You know then, that you want to get 4, skip 1, get 4, skip 1 and so on,
> adding your extra text as you go:
>
> Data = Split(...)
> idx = 0
> text = ""
> For i = 1 to Ubound(Data) \ 5
> text = text & "start text;"
> For j = 1 to 4
> text = text & Data(idx) & ";"
> idx = idx + 1
> Next
> text = text & "end text" & vbCrLf
> idx = idx + 1
> Next
>
> The inner loop gets your 4 items and the outer loop
> handles your extra text and skipping the 5th item.
>
> LFS
>
>
>
Thanks, I've added this to my code but it only joins the first 4 lines
and writes them as one line. Subsequent lines after the 4th don't show
up. Do I need to use a different method of writing the text variable or
use an array to save the new lines?

Dim objFSO, strTextFile, strData, Data, i , j, idx, text
Const ForReading = 1
Const ForWriting = 2

'text file variable
strTextFile = "c:\scripts\test.txt"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

Data = Split(strData,vbCrLf)
idx = 0
text = ""
For i = 1 to Ubound(Data) \ 5
text = text & ".;"
For j = 1 to 4
text = text & Data(idx) & ";"
idx = idx + 1
Next
text = text & "config save" & vbCrLf
idx = idx + 1

Next

Set objFile = objFSO.OpenTextFile(strTextFile, ForWriting)
objFile.Write text
objFile.Close
My System SpecsSystem Spec
Old 07-14-2009   #9 (permalink)
ekkehard.horner


 
 

Re: string manipulation - join lines

James schrieb:
Quote:

> I want to take some text like below and join 4 lines together separated
> by semi colons. Each line would have some text added to the beginning
> and end of the line. If the line contained the number 234234 the line
> would be skipped
>
> asdf
> adaffad
> adfafasdf
> adfaeqe
> 234234
> erwe
> sdfs
> sdfd
> sdfgsfd
>
>
> The result would look like this
>
> extra text;asdf;adaffad;adfafasdf;adfaeqe;extra text
> extra text;erwe;sdfs;sdfd;sdfgsfdextra text
>
> What kind of looping structure would I use to achieve this?
>
As you want to write blocks of 4 lines from the source file to
1 line of the destination file, .ReadAll and Split are bad ideas.
Just read the lines of the source file and write to the destination
file while handling the special cases (1st & 4th line):

Const csSkip = "234234"
Const csPfx = "prefix"
Const csSfx = "suffix"
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sSrcFSpec : sSrcFSpec = ".\join4lines\src00.txt"
WScript.Echo oFS.OpenTextFile( sSrcFSpec ).ReadAll()
Dim sDstFSpec : sDstFSpec = ".\join4lines\dst00.txt"
Dim tsSrc : Set tsSrc = oFS.OpenTextFile( sSrcFSpec )
Dim tsDst : Set tsDst = oFS.CreateTextFile( sDstFSpec, True )
Dim nLine : nLine = 0
Do Until tsSrc.AtEndOfStream
Dim sLine : sLine = Trim( tsSrc.ReadLine() )
If "" <> sLine And csSkip <> sLine Then
nLine = nLine + 1
Select Case nLine
Case 1 ' Start
tsDst.Write csPfx & ";" & sLine
Case 2, 3
tsDst.Write ";" & sLine
Case 4
tsDst.WriteLine ";" & sLine & ";" & csSfx
nLine = 0
End Select
Else
WScript.Echo "skipping", tsSrc.Line, sLine
End If
Loop
tsDst.Close
tsSrc.Close
WScript.Echo oFS.OpenTextFile( sDstFSpec ).ReadAll()

output:

=== join4Lines: join 4 lines ==========================
asdf
adaffad
adfafasdf
adfaeqe
234234
erwe
sdfs
sdfd
sdfgsfd

skipping 6 234234
prefix;asdf;adaffad;adfafasdf;adfaeqe;suffix
prefix;erwe;sdfs;sdfd;sdfgsfd;suffix

=== join4Lines: 0 done (00:00:00) =====================


My System SpecsSystem Spec
Old 07-14-2009   #10 (permalink)
mayayana


 
 

Re: string manipulation - join lines

I think this is what you want:

'--------------------

Dim objFSO, strTextFile, strData, Data, i, j, idx, text, s, AllText
Const ForReading = 1
Const ForWriting = 2

strTextFile = "c:\windows\desktop\test.txt"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

Data = Split(strData, vbCrLf)
idx = 0
text = "extratextbegin;"
For i = 0 to Ubound(Data)
s = Data(i)
If s <> "234234" Then
text = text & s & ";"
idx = idx + 1
If idx = 4 Then
AllText = AllText & text & "extratextend" & vbCrLf
text = "extratextbegin;"
idx = 0
End If
End If
Next

Set objFile = objFSO.OpenTextFile(strTextFile, ForWriting)
objFile.Write AllText
objFile.Close

'----------------------

'-- Notes: This is not optimizrd for a large file.
If you have a very large file you can add each
line to an array and then use Join. Also, this
assumes that the line to be dropped is always
the exact same. (The "234234")


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
string manipulation VB Script
Re: String manipulation using replace etc. VB Script
Re: String manipulation using replace etc. VB Script
string manipulation PowerShell
string manipulation PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46