Author Topic: How to pass CRLF in a parameter  (Read 259 times)

Jeremy Whilde

  • Jr. Member
  • **
  • Posts: 50
How to pass CRLF in a parameter
« on: August 02, 2018, 09:36:10 am »
How do you pass a carriage return line feed in a parameter? I have a compiled WinBatch script that sends some text to another application window and this needs to be supplied from command line parameters. But in the compiled version I cannot seem to pass the CRLF correctly whatever I do when it is compiled all I get displayed in the external application is a single line of text!

If I do the following inside the script and run it uncompiled then I can pass multiple lines of text with the CRLF correctly inserted and displayed in the external application window:

param1 = "text" ; Simulate sending param 1

crlf = StrCat(Num2Char(13), Num2Char(10))

param2 = "Line1%crlf%Line2" ; Simulate sending param 2

....The rest of the script ....

; Send the text

If Param1 == "text" then cSetWndText(Controlhandle, param2)


So this will display correctly as:

Line1
Line2


But when I compile the script  and  run it with parameters of text "Line1%crlf%Line2"

I get:

Line1%crlf%Line2


Any help greatly appreciated

Thanks, JW


td

  • Tech Support
  • *****
  • Posts: 2624
    • WinBatch
Re: How to pass CRLF in a parameter
« Reply #1 on: August 02, 2018, 01:21:12 pm »
If by "command line parameters" you are referring to launching a compiled script with a parameter from a shortcut or the Windows command shell, this is expected behavior.  Substitution is only performed on the lines within your script, not on the contents of variables.   Your command line parameter is not a line in your script it.  It is the content of a variable.   

You might want to consider using the StrReplace function on param1 in your script.
 
Code: Winbatch
if param0 then message('Command line', StrReplace(param1, '<crlf>', @crlf))
exit

Where the command line argument is something like "My command line parameter<crlf>has a carriage return and line feed"
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


Jeremy Whilde

  • Jr. Member
  • **
  • Posts: 50
Re: How to pass CRLF in a parameter
« Reply #2 on: August 03, 2018, 03:02:21 am »
OK yes, thank you the substitution explanation makes sense when considering the compiled executable receiving the parameters. So the following fixes the issue:

newText = StrReplace(param2, '<crlf>', @crlf)

If Param1 == "text" then cSetWndText(Controlhandle, newText)

Thanks, JW

notds

  • Newbie
  • *
  • Posts: 6
Re: How to pass CRLF in a parameter
« Reply #3 on: August 22, 2018, 03:38:42 pm »
param2 = "Line1<crlf>Line2" ; Simulate sending param 2

newText = StrReplace(param2, '<crlf>', @crlf)

If Param1 == "text" then cSetWndText(Controlhandle, newText)

OR

Code: Winbatch

Param2 = "You could just do your" : @CRLF : "Next line like this" : @CRLF : "And your next line like this"

If Param1 == "text" then cSetWndText(Controlhandle, Param2)

;or

If Param1 == "text" then cSetWndText(Controlhandle, "You could just do your" : @CRLF : "Next line like this" : @CRLF : "And your next line like this")