Jump to content

Add / Remove Line Breaks


Recommended Posts

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)

Link to comment
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.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
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)

Link to comment
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)
Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...