Passing muliline and single line parameters to a MS DOS bat file

891 views Asked by At

im try to send sms using gnokii sms library (http://gnokii.org/) with vb .net,im created a seperate bat file and call that bat file from my vb.net code

this is my vb code

 Dim process As New System.Diagnostics.Process
    Dim startInfo As New ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory & "sms.bat")
    process.StartInfo = startInfo
    process.StartInfo.Arguments = txtBody.Text'text typed in text box

this is my bat file

@echo off
echo Begin Transaction
echo "message body" |  c:\sms\gnokii.exe   --sendsms 0771234567 'this is mobile no
pause

my problem is i want to pass two parameters to message body and mobile no without hard code them

message body consisted with spaces and multilines and mobile no cosistend only single line without spaces

how can i Achieve in bat file

please help

1

There are 1 answers

10
jeb On BEST ANSWER

First you should test, if the gnokii.exe accepts multiline texts via a pipe.
Simply create a multiline text file and try it with

type mySMS.txt | c:\sms\gnokii.exe   --sendsms 0771234567

If this works it should be also possible to send it from a batch file and add linefeeds into the text.

@echo off
setlocal EnableDelayedExpansion
set LF=^


rem ** The two empty lines are required **
echo Begin Transaction
echo Line1!LF!Line2 |  c:\sms\gnokii.exe   --sendsms 0771234567 'this is mobile no

EnableDelayedExpansion should be used when linefeed characters are used.
There exists also solutions to use it with percent expansion, but that is much more complicated.
Explain how dos-batch newline variable hack works

To use this with parameters like in your comment, you need to format the message in VB.

So when you want to send a text like

Hello
this is a text
with three lines

You need to send to the batch

process.StartInfo.Arguments = "Hello!LF!this is a text!LF!with three lines"

And your batch should look like

setlocal EnableDelayedExpansion
set LF=^


set "text=%~1"
echo !text! | c:\sms\gnokii.exe --sendsms %2

A second solution, when this doesn't work.

Create a temporary file and use redirection

setlocal EnableDelayedExpansion
set LF=^


set "text=%~1"
echo !text! > "%TMP%\sms.txt"
c:\sms\gnokii.exe --sendsms %2 < "%TMP%\sms.txt"
del "%TMP%\sms.txt"