How to Detect a Linebreak in VisualNeo Win
When you move text files between Linux, Mac and Windows -as I do-, you must be aware of the fact that the line endings in Linux, Mac and Windows are different. In Linux and Mac, LF (\n or [#10] in VisualNeo Win) is used and in Windows CRLF (\r\n or [#13][#10] in VisualNeo Win). So in a task where the contents of a text file is assigned to a variable, which will be parsed by the delimiter line break [#13][#10], you have to be sure that all linebreaks are CRLF, i.e. [#13][#10].
There are several methods to find out. For non-experienced regex users or users with a VisualNeo Win version without regex utilities, I suggest the following solution:
Step 1: read the contents of a file to a variable and determine the variable’s length:
SetVar "[input_file]" "[PubDir]input.txt" FileToVar "[input_file]" "[string_content_file]" StrLen "[string_content_file]" "[len_1]"
Step 2: suppose the string [string_content_file] contains the line break [#13][#10], replace it by [#10] now and determine the length of the modified string:
StrReplace "[string_content_file]" "[#13][#10]" "[#10]" "[modified_string_content_file]" "" StrLen "[modified_string_content_file]" "[len_2]" ......................................... If [#13][#10] did exist in [string_content_file], then it is now replaced by [#10]. This means that the modified string is smaller than the original one: ......................................... If "[len_2]" "<" "[len_1]" .you can split the original [string_content_file] by [#13][#10] EndIf
If [#13][#10] did not exist in [string_content_file], then there was no replace. This means that the length of [modified_string_content_file] is equal to [len_1]. Conclusion: probably (1) the original string contains the line break [#10] or exceptionally [#13] or has (theoretically) no line break.
Step 3: find the delimiter [#10] or [#13] and -if found- replace this delimiter with [#13][#10]. If not found, you can not split the string.
If "[len_2]" "=" "[len_1]" .look for [#10] SearchStr "[#10]" "[string_content_file]" "[found]" "" If "[found]" ">" "0" StrReplace "[string_content_file]" "[#10]" "[#13][#10]" "[string_content_file]" "" . split the string etc. Else .look for [#13] SearchStr "[#13]" "[string_content_file]" "[found]" "" If "[found]" ">" "0" StrReplace "[string_content_file]" "[#13]" "[#13][#10]" "[string_content_file]" "" . split the string etc. Else . the original string is for parsing on a line break not valid EndIf EndIf EndIf
I hope this is helpful. Thanks for reading.
Best regards,
Reinier
Footnote:
(1) See for other line breaks: en.wikipedia.org/wiki/Newline
Comments