Skip to content
⌘ NSIS Forum Archive

break line control

13 posts

MyPC8MyBrain#

break line control

Hi everyone 🙂
ive been experimenting with blank/break line control,
the goal is to parse a file prior to editing to assure consistent spacing between data blocks,
the data blocks are just some text instructions in segments separated by a line break or sometimes two or more,

i want create a unified 2 line break between each block,

i thought a simple search and replace for "$\r$\n" replacing with "$\r$\n$\r$\n"
and than passing again replacing "$\r$\n$\r$\n$\r$\n" with "$\r$\n$\r$\n"
should do the trick; but as i found out i was wrong 🙁

any suggestion how i should go about it?

TIA
Chris
Anders#
Are you editing in-place or from one file to another?

I would do it in one pass, add if needed and ignore if too many when writing to the other file.
MyPC8MyBrain#
Thank you Anders <3
yes i am appending in place,

im currently having a hard time capturing the pattern of two line breaks,
atm my attempts only add never subtract 🙁

ATB
Chris
MyPC8MyBrain#
even from one file to another still no go 🙁
i can only replace a single line but not two,
as soon as the second line comes to play the it cant be found,
when only capturing a single line; this basically closes all gaps,

any advice?

TIA
Chris
Anders#
Are you keeping a count of empty lines in your loop? And reset the count when you hit a non-empty line?
MyPC8MyBrain#
i am not; this is my last attempt which delete all spaces,

Section
ClearErrors
FileOpen $0 "test.txt" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop:
   FileRead $0 $2
   IfErrors done
   StrCmp $2 "$\r$\n" 0 +2
      StrCpy $2 ""
   FileWrite $1 $2
   Goto loop
done:
   FileClose $0
   FileClose $1
   Delete "test.txt"
   CopyFiles /SILENT $R0 "test.txt"
   Delete $R0
SectionEnd 

ATB
Chris
Anders#
Section
FileOpen $0 "test.txt" "w"
FileWrite $0 "Hello$\r$\n"
FileWrite $0 "World$\r$\n"
FileWrite $0 "$\r$\n"
FileWrite $0 "One$\r$\n"
FileWrite $0 "Two$\r$\n$\r$\n"
FileWrite $0 "333$\r$\n$\r$\n$\r$\n"
FileWrite $0 "EOF$\r$\n"
FileClose $0
ClearErrors
FileOpen $0 "test.txt" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop:
   FileRead $0 $2
   IfErrors done
   
   ${If} $2 != "$\r$\n"
   ${AndIf} $2 != "$\r"
   ${AndIf} $2 != "$\n"
      FileWrite $1 $2
      FileWrite $1 "$\r$\n"
   ${EndIf}
   Goto loop
done:
   FileClose $0
   FileClose $1
   Delete "test.txt"
   CopyFiles /SILENT $R0 "test.txt"
   Delete $R0
   ExecShellWait "" "test.txt"
   Delete "test.txt"
SectionEnd 
?
MyPC8MyBrain#
Thank you Anders <3

it doesn't seem to like the first loop ${If},
i changed to !If and it moved on to not liking the next ${AndIf}

Edit:
adding !include LogicLib.nsh fixed the stop above,
trying to figure out why its now stopping here ExecShellWait "" "test.txt"

ATB
Chris
MyPC8MyBrain#edited
YaY \ o / its working

i bow to your expertise Master <3

TIA
Chris
Anders#
Originally Posted by MyPC8MyBrain View Post
trying to figure out why its now stopping here ExecShellWait "" "test.txt"
This only works in recent NSIS v3.
MyPC8MyBrain#
adding both
!include LogicLib.nsh
!include StdUtils.nsh 
and changing from
   ExecShellWait "" "test.txt" 
to
   StdUtils::ExecShellWait "" "test.txt" 
had your code working fully (custom build based 3.02)

i ended with commenting out last two lines as the last deletes our file and the one before kept giving me error,
something about "this file has no handle" or something,

   #ExecShellWait "" "test.txt" 
   #Delete "test.txt" 
ATB
Chris