Solved

Returning 140 characters per line

Posted on 2009-06-29
15
374 Views
Last Modified: 2013-11-09
Hi all,

I have a text file that comprises of lines as such:
"this is one" OR
"this is two" OR
"this is three" OR
Etc

What I am looking for is a batch file that will take all the characters and text and put a maximum of 140 characters per line into a new text file.

If the batch came across a word that would make it run over 140 then I would like it to go back to the LAST " ( snipping off the  OR . (that's a spaceORspace), whether that's 140 or less characters.

i look forward to your replies

Kevin

0
Comment
Question by:oconnork00
  • 8
  • 7
15 Comments
 
LVL 3

Expert Comment

by:astroviper
ID: 24742806
It would probably be much easier, for me at least,  to do in VBScript. Does the situation exist where the total line length could be over say 145 characters because given the rule:
If the batch came across a word that would make it run over 140 then I would like it to go back to the LAST " 
You would end up with just a single " on the line. Unless by ' LAST " ' you mean the second one on the line, but the the total line length would still be over 140 characters... Can you specify what to do in this case or is this not likely to happen?
0
 
LVL 2

Author Comment

by:oconnork00
ID: 24743033
hi astroviper,
Ideally it would need be 140 or less, as I plan to paste this into twitter and can't have any more than 140.

If the case is that there is 139 or less then that's fine, but it would need to clip off the words as a whoe, e.g "help with maths"
rather than
"help with ma
and then go onto a new line to finish the rest:
ths"

As each keyword phrase ends with a quote that would help in determining whether it would fit on that line. If the script knows adding the keyword phrase between the two quotes will make the character count go over 140, then start a new line. A vbscript would be perfect, thanks.



0
 
LVL 3

Expert Comment

by:astroviper
ID: 24743165
Does this work for you?

Using WordWrap function from http://www.petenelson.com/aspwatch/ASPWatch%20%20WordWrap%20Function.htm

strInputFile  = "input.txt"
strOutputFile = "output.txt"
 
Const ForReading    = 1
Const ForWriting    = 2
Const ForAppending  = 8
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInputFile, ForReading)
strText = objInput.ReadAll
objInput.Close
 
arrText = Split(strText, VbCrLf)
strOutput = ""
 
For i = 0 To UBound(arrText)
    arrLine = Split(WordWrap(arrText(i), 140, VbCrLf, False), VbCrLf)
    strOutput = strOutput & arrLine(0) & VbCrLf
Next
 
If objFSO.FileExists(strOutputFile) Then
    Set objOutput = objFSO.OpenTextFile(strOutputFile, ForAppending)
Else
    Set objOutput = objFSO.CreateTextFile(strOutputFile)
End If
objOutput.Write strOutput
objOutput.Close
 
 
Function WordWrap(ByVal strWords, ByVal intWrapLength, ByVal strWrapText, ByVal blnReplaceVbCrLf)
 
    Dim arrWords, arrTrailingCharacters, x
    Dim intRunningLength
 
 
    '*** Strip out carriage returns
    If blnReplaceVbCrLf Then
        strWords = Replace(strWords, vbCrLf, " ")
    End If
 
 
    '*** Split the words into an array using a space. The
    '*** second array just makes it easier to add the Wrap Text
    arrWords = Split(strWords, " ")
    arrTrailingCharacters = Split(strWords, " ")
 
    '*** Set the trailing characters for each word to a space
    For x = LBound(arrTrailingCharacters) To UBound(arrTrailingCharacters)
        arrTrailingCharacters(x) = " "
    Next
 
 
    '*** Now start looping through the words and adding the wrap text
     intRunningLength = 0
 
    For x = LBound(arrWords) To UBound(arrWords)
 
        '*** Calculate the running length of the words
        intRunningLength = intRunningLength + Len(arrWords(x) & " ")
 
        '*** If we're at the exact word wrap length, add the wrapping text
        '*** at the end of the current word and reset the running length
        If intRunningLength = intWrapLength Then
            arrTrailingCharacters(x) = strWrapText
            intRunningLength = 0
        End If
 
 
        '*** If we've pass the wrapping length, put the wrapping text
        '*** at the end of the previous word. Set the running length
        '*** to the length of the current word.
        If intRunningLength >= intWrapLength And x > 0 Then
            arrTrailingCharacters(x - 1) = strWrapText
            intRunningLength = Len(arrWords(x) & " ")
        End If
 
    Next
 
    '*** Build the words and the wrapping text back together and return them
     For x = LBound(arrWords) To UBound(arrWords)
        WordWrap = WordWrap & arrWords(x) & arrTrailingCharacters(x)
    Next
 
End Function

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Author Comment

by:oconnork00
ID: 24743219
Hi astroviper,

I cant seem to get it working. I placed the file I wanted to run it on in c:\input.txt and a blank file in c:\output.txt and error pops up: see attached image


error.JPG
0
 
LVL 3

Expert Comment

by:astroviper
ID: 24743262
would you be able to post the input file you are using?
0
 
LVL 2

Author Comment

by:oconnork00
ID: 24743273
see attached, thanks.
input.txt
0
 
LVL 3

Expert Comment

by:astroviper
ID: 24743305
Okay so honestly, I didn't figure out why the 0 index wasn't valid, however ignoring it didn't seem to matter on the example:

strInputFile  = "input.txt"
strOutputFile = "output.txt"
 
Const ForReading    = 1
Const ForWriting    = 2
Const ForAppending  = 8
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInputFile, ForReading)
strText = objInput.ReadAll
objInput.Close
 
arrText = Split(strText, VbCrLf)
strOutput = ""
 
On Error Resume Next
For i = 0 To UBound(arrText)
    arrLine = Split(WordWrap(arrText(i), 140, VbCrLf, False), VbCrLf)
    strOutput = strOutput & arrLine(0) & VbCrLf
Next
On Error Goto 0
 
If objFSO.FileExists(strOutputFile) Then
    Set objOutput = objFSO.OpenTextFile(strOutputFile, ForAppending)
Else
    Set objOutput = objFSO.CreateTextFile(strOutputFile)
End If
objOutput.Write strOutput
objOutput.Close
 
 
Function WordWrap(ByVal strWords, ByVal intWrapLength, ByVal strWrapText, ByVal blnReplaceVbCrLf)
 
    Dim arrWords, arrTrailingCharacters, x
    Dim intRunningLength
 
 
    '*** Strip out carriage returns
    If blnReplaceVbCrLf Then
        strWords = Replace(strWords, vbCrLf, " ")
    End If
 
 
    '*** Split the words into an array using a space. The
    '*** second array just makes it easier to add the Wrap Text
    arrWords = Split(strWords, " ")
    arrTrailingCharacters = Split(strWords, " ")
 
    '*** Set the trailing characters for each word to a space
    For x = LBound(arrTrailingCharacters) To UBound(arrTrailingCharacters)
        arrTrailingCharacters(x) = " "
    Next
 
 
    '*** Now start looping through the words and adding the wrap text
     intRunningLength = 0
 
    For x = LBound(arrWords) To UBound(arrWords)
 
        '*** Calculate the running length of the words
        intRunningLength = intRunningLength + Len(arrWords(x) & " ")
 
        '*** If we're at the exact word wrap length, add the wrapping text
        '*** at the end of the current word and reset the running length
        If intRunningLength = intWrapLength Then
            arrTrailingCharacters(x) = strWrapText
            intRunningLength = 0
        End If
 
 
        '*** If we've pass the wrapping length, put the wrapping text
        '*** at the end of the previous word. Set the running length
        '*** to the length of the current word.
        If intRunningLength >= intWrapLength And x > 0 Then
            arrTrailingCharacters(x - 1) = strWrapText
            intRunningLength = Len(arrWords(x) & " ")
        End If
 
    Next
 
    '*** Build the words and the wrapping text back together and return them
     For x = LBound(arrWords) To UBound(arrWords)
        WordWrap = WordWrap & arrWords(x) & arrTrailingCharacters(x)
    Next
 
End Function

Open in new window

0
 
LVL 2

Author Comment

by:oconnork00
ID: 24743327
I just ran that now, and the output file ended up being the same as the input file
output.txt
0
 
LVL 3

Expert Comment

by:astroviper
ID: 24743382
That's because no line in the input file was over 140 characters. Can you further clarify what you want the script to do? I thought it was just make sure no line was over 140 characters and remove whole words if it was.
0
 
LVL 3

Expert Comment

by:astroviper
ID: 24743403
Maybe posting an example of your desired output?
0
 
LVL 2

Author Comment

by:oconnork00
ID: 24743406
Ah ok sorry for the confusion here, my mistake for not being clear enough.

What I want is the output file to keep taking the next keyword phrase and adding it to the line above, so that there will be no more than 140 characters on any one line.
0
 
LVL 2

Author Comment

by:oconnork00
ID: 24743427
I've also incuded the number of characters in the text file for you to see...

If we added another keyword from the line below, it would have brought it over 140
results.txt
0
 
LVL 3

Expert Comment

by:astroviper
ID: 24743443
I get it! :) I have to go out now though. I'll be sure to have a look at it in about an hour or so when I get home. It's gonna be much more interesting than I originally expected.
0
 
LVL 3

Accepted Solution

by:
astroviper earned 500 total points
ID: 24743845
This one just substitutes spaces for a character, turns the line breaks into spaces, then uses the same word wrap function and swaps the spaces back in their original places.
This makes it not entirely accurate because if you had one of those strings in quotes that would fit but because it has the OR connected to it before the word wrap, it'll make it that few crucial characters longer and cause the string to wrap early.
Let me know if this is a problem and I will refine the script some more.

'character you won't use in your text
chSplit = "#"
strInputFile  = "input.txt"
strOutputFile = "output.txt"
 
Const ForReading    = 1
Const ForWriting    = 2
Const ForAppending  = 8
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInputFile, ForReading)
strText = objInput.ReadAll
objInput.Close
 
' remove starting text
strText = Mid(strText, InStr(strText, """"))
' remove spaces
strText = Replace(strText, " ", chSplit)
' convert line breaks to spaces
strText = Replace(strText, VbCrLf, " ")
' wrap
strText = WordWrap(strText, 140, VbCrLf, False)
' return characters to original
strText = Replace(strText, chSplit, " ")
strOutput = strText
 
If objFSO.FileExists(strOutputFile) Then
    Set objOutput = objFSO.OpenTextFile(strOutputFile, ForWriting)
Else
    Set objOutput = objFSO.CreateTextFile(strOutputFile)
End If
objOutput.Write strOutput
objOutput.Close
 
' Remove OR from line ends
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strOutputFile, ForReading)
strText = ""
Do Until objInput.AtEndOfStream
    strLine = objInput.ReadLine
    strLine = Left(strLine, InStrRev(strLine, """"))
    strText = strText & strLine & VbCrLf
Loop
objInput.Close
 
Set objOutput = objFSO.OpenTextFile(strOutputFile, ForWriting)
objOutput.Write strText
objOutput.Close
 
WScript.Echo "File wrapping complete"
 
Function WordWrap(ByVal strWords, ByVal intWrapLength, ByVal strWrapText, ByVal blnReplaceVbCrLf)
 
    Dim arrWords, arrTrailingCharacters, x
    Dim intRunningLength
 
 
    '*** Strip out carriage returns
    If blnReplaceVbCrLf Then
        strWords = Replace(strWords, vbCrLf, " ")
    End If
 
 
    '*** Split the words into an array using a space. The
    '*** second array just makes it easier to add the Wrap Text
    arrWords = Split(strWords, " ")
    arrTrailingCharacters = Split(strWords, " ")
 
    '*** Set the trailing characters for each word to a space
    For x = LBound(arrTrailingCharacters) To UBound(arrTrailingCharacters)
        arrTrailingCharacters(x) = " "
    Next
 
 
    '*** Now start looping through the words and adding the wrap text
     intRunningLength = 0
 
    For x = LBound(arrWords) To UBound(arrWords)
 
        '*** Calculate the running length of the words
        intRunningLength = intRunningLength + Len(arrWords(x) & " ")
 
        '*** If we're at the exact word wrap length, add the wrapping text
        '*** at the end of the current word and reset the running length
        If intRunningLength = intWrapLength Then
            arrTrailingCharacters(x) = strWrapText
            intRunningLength = 0
        End If
 
 
        '*** If we've pass the wrapping length, put the wrapping text
        '*** at the end of the previous word. Set the running length
        '*** to the length of the current word.
        If intRunningLength >= intWrapLength And x > 0 Then
            arrTrailingCharacters(x - 1) = strWrapText
            intRunningLength = Len(arrWords(x) & " ")
        End If
 
    Next
 
    '*** Build the words and the wrapping text back together and return them
     For x = LBound(arrWords) To UBound(arrWords)
        WordWrap = WordWrap & arrWords(x) & arrTrailingCharacters(x)
    Next
 
End Function

Open in new window

0
 
LVL 2

Author Closing Comment

by:oconnork00
ID: 31598050
Excellent, it does the job, thanks for thath
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
For loop to add numbers 3 74
How can I do this in Pyhton? 12 125
Sums of coloumns in html/java 15 76
Adding automation to a search and and replace a character in a text file. 5 46
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question