oconnork00
asked on
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
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
ASKER
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.
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.
Does this work for you?
Using WordWrap function from http://www.petenelson.com/ aspwatch/A SPWatch%20 %20WordWra p%20Functi on.htm
Using WordWrap function from http://www.petenelson.com/
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
ASKER
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
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
would you be able to post the input file you are using?
ASKER
see attached, thanks.
input.txt
input.txt
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
ASKER
I just ran that now, and the output file ended up being the same as the input file
output.txt
output.txt
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.
Maybe posting an example of your desired output?
ASKER
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.
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.
ASKER
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
If we added another keyword from the line below, it would have brought it over 140
results.txt
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, it does the job, thanks for thath
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?