Sign in to follow this  
Followers 0
face

Add / Remove Line Breaks

7 posts in this topic

i need a program that batch adds line breaks to text files text every x characters and doesnt cut words in half.

program use steps:

1. run script

2. pick folder (contains text files)

3. set every how many character for line breaks

4. choose not to split words in half (word wrap)

Share this post


Link to post
Share on other sites



face, you have been here long enough to know this is not a code-by-request forum. start writing your program, it doesn't sound too hard. if you are having any difficulty, post what you have and everyone here will be only too happy to help.

Share this post


Link to post
Share on other sites

i have this so far

#include <MsgBoxConstants.au3>
#include <Array.au3>
Example()

Func Example()
    Local Const $sMessage = "Select a folder"
    Local $sFileSelectFolder = FileSelectFolder($sMessage, "")
    
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
     Else
        
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder)
    EndIf
EndFunc

now i need to complete steps 3 and 4

3. set every how many character for line breaks

4. choose not to split words in half (word wrap)

Share this post


Link to post
Share on other sites

This should work

Local $sString = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ' & @crlf & _
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum dolores et ea rebum end.'

$x = 50

$res = StringRegExpReplace($sString, "(?s).{1," & $x-1 & "}(\h|$)\K", @CRLF)
Msgbox(0,"", $res)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@mikell
 
I noticed with $x = 27, using your reg.exp., @CRLF is counted as two characters. The count continues to the end of "Lorem", where another @CRLF is inserted.   This leaves "Lorem" on a line by itself.

....
sanctus est Lorem ipsum 
dolor sit amet. 
Lorem 
ipsum dolor sit amet, 
consetetur sadipscing
....

The following example works.   There has to be a better method (See Edit)

Local $sString = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ' & @CRLF & _
        'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum dolores et ea rebum end.'

Local $x = 27

$res = StringRegExpReplace($sString, "(\V{1," & $x - 1 & "})(\s+)", "$1" & @CRLF)

ConsoleWrite($res & @LF)

Edit: Replaced $res = StringRegExpReplace(StringRegExpReplace($sString, "V{1," & $x - 1 & "}(h|$)K", @CRLF), "(R+)", @CRLF)
with the now existing line in the example.

Edited by Malkey

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Malkey

I thought about something like this, but look at the last line ?

Local $sString = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ' & @crlf & _ 
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum dolores et ea rebum end.'

$x = 60
$res = StringRegExpReplace($sString, "(\V{1," & $x - 1 & "})(\s+)", "$1" & @CRLF)

Msgbox(0,"", $res)

Edit

Got it

Local $sString = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ' & @crlf & @crlf & _ 
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum dolores et ea rebum end.'

$x = 50
$res = StringRegExpReplace($sString, ".{1," & $x - 1 & "}\K(\s{1,2}|$)", @CRLF)

Msgbox(0,"", $res)
Edited by mikell

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Now the only problem is getting rid of the trailing linefeed. :)

Local $sString = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ' & @CRLF & _
        'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum dolores et ea rebum end.'

$x = 27
$res = StringRegExpReplace($sString, ".{1," & $x - 1 & "}\K(\s{1,2}|$)", @CRLF)
If StringRegExp($sString, "\v$") = 0 Then $res = StringStripWS($res, 2) ; Remove trailing linefeed (@CRLF) if not present at end of original text ($sString).

ConsoleWrite($res & @LF)

Edit:
Maybe this. :unsure:

Local $sString = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ' & @CRLF & @CRLF & _
        'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum dolores et ea rebum end.'

$x = 60

$res = StringRegExpReplace($sString, ".{" & Int($x * 0.85) & "," & $x - 1 & "}\K(\s{1,2})", @CRLF)
ConsoleWrite($res & @LF)
Edited by Malkey

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0