Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Returning 140 characters per line

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
oconnork00
Asked:
oconnork00
  • 8
  • 7
1 Solution
 
astroviperCommented:
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
 
oconnork00Author Commented:
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
 
astroviperCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
oconnork00Author Commented:
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
 
astroviperCommented:
would you be able to post the input file you are using?
0
 
oconnork00Author Commented:
see attached, thanks.
input.txt
0
 
astroviperCommented:
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
 
oconnork00Author Commented:
I just ran that now, and the output file ended up being the same as the input file
output.txt
0
 
astroviperCommented:
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
 
astroviperCommented:
Maybe posting an example of your desired output?
0
 
oconnork00Author Commented:
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
 
oconnork00Author Commented:
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
 
astroviperCommented:
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
 
astroviperCommented:
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
 
oconnork00Author Commented:
Excellent, it does the job, thanks for thath
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now