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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Long text string to smaller strings of specified length

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB
Please if anyone is expert in VBScript can you help me as I am not.

What I need is long string turned into smaller ones of specified length with all spaces removed and my own additional string added to the beginning and end of each line.

The specified length must be allowed to be specified in this example I have chosen 11 .

The last line will be what ever is left over in this example it is 6 left over.

The string could be 300,000 characters or more or less it will always be different.

This is the string to add at front of each line
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("

This is the string to add at end of each line
",x,2))) : Next

Below is as far as I can get without your help.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(".\output.txt", True)

Dim mystring
mystring = "4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 00 00 00 00 00 00 40”

objFile.WriteLine mid(Replace(mystring," ",""),1,11)
objFile.Close

It outputs to text file the following line
4D5A9000030

But if it worked the way I need it to the text file would be
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("4D5A9000030",x,2))) : Next
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("00000040000",x,2))) : Next
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("00FFFF0000B",x,2))) : Next
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("80000000000",x,2))) : Next
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("000040",x,2))) : Next
 
>This is the string to add at front of each line
>For x = 1 To 511 Step 2

Are you sure?

Anyway, here's a starter:

Code:
[blue]Sub Breakup(strSource, lSize)
    strSource = Replace(strSource, " ", "")
    For lp = 1 To Len(strSource) Step lSize
        MsgBox Mid(strSource, lp, lSize)
    Next
End Sub[/blue]
 
traverse the string and biting off chuncks along the way. Either define, resize, and add each chunk to an array or (as in the example below) add each chunk to a new string and separate them which a unique string (delimiter) and split the new string into an array.

Code:
strDelim = "^" 
strText = "abcdefghijklmnopqrstuvwxyz"
intChunk = inputBox("Chunk size:")

do while len(strText)
   if (intChunk > len(strText)) then intChunk = len(strText)
   strChunk = left(strText, intChunk)
   replace(strText, strChunk, "", 1)
   if (len(strText) = 0) then strTokenized = strTokenized & strDelimiter
loop

arrChunks = split(strTokenized, strDelimiter)

With a chunk size of 4, strTokenized will look like [tt]abcd^efgh^ijkl^mnop^qrst^uvwx^yz[/tt]. After spliting, the array will look like:

[tt]
arrChunks(0) = "abcd"
arrChunks(1) = "efgh"
arrChunks(2) = "ijkl"
arrChunks(3) = "mnop"
arrChunks(4) = "qrst"
arrChunks(5) = "uvwx"
arrChunks(6) = "yz"
[/tt]

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Oh Boy. My example was untested. Here's what it should be

Code:
strDelim = "^" 
strText = "abcdefghijklmnopqrstuvwxyz"
intChunk = cint(inputBox("Chunk size:"))

do while len(strText)
	if (intChunk > len(strText)) then intChunk = len(strText)
	strChunk = left(strText, intChunk)
	strText = replace(strText, strChunk, "", 1)
	
	if (len(strText) <> 0) then strChunk = strChunk & strDelim
loop

arrChunks = split(strTokenized, strDelim)

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Thank you for the replies but like I said I am not good with VBScript so if anyone has the precise solution to what I posted I would be grateful

I did try the code but could not get the result I posted from them
 
Code:
[blue]Dim mystring
mystring = "4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 00 00 00 00 00 00 40"

Breakup mystring, 11

Sub Breakup(strSource, lSize)
    strHead = "For x = 1 To 511 Step 2 : ts.Write Chr(Clng(""&H"" & Mid("""
    strTail = """,x,2))) : Next"
    strSource = Replace(strSource, " ", "")
    
    With CreateObject("Scripting.FileSystemObject").CreateTextFile(".\output.txt", True)
        For lp = 1 To Len(strSource) Step lSize
            .WriteLine strHead & Mid(strSource, lp, lSize) & strTail
        Next
    End With
End Sub[/blue]
 
Ah. I was too hasty to provide a solution that I did not acknowledge the whole post. It looks as though you are writing a script to write a portion of a script? What is your end goal? There might be a easier way of doing it.

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Thank you strongm

Your code is excellent. It works and produces exactly what I was looking for.

I hope others will find your solution as helpful as I have.

I have been able to learn from what you have coded.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top