2013-12-17 40 views
2

即時通訊嘗試發送短信使用gnokii短信庫(http://gnokii.org/)與vb.net,im創建一個單獨的bat文件,並從我的vb.net代碼調用該bat文件傳遞muliline和單行參數到MS DOS bat文件

這是我的VB代碼

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 

這是我的bat文件

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

我的問題是我想傳遞兩個參數,以郵件正文和手機號碼沒有硬ç ODE他們

消息體包括用空格和多線和手機號碼cosistend只有一行不帶空格

我如何能在bat文件實現

請幫助

回答

2

首先,你應該,如果測試, gnokii.exe通過管道接受多行文字。
只需創建一個多行文本文件,並與

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

嘗試,如果這個作品應該也可以從一個批處理文件發送,並添加換行到文本。

@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。
也存在使用百分比擴展的解決方案,但這要複雜得多。
Explain how dos-batch newline variable hack works

要在評論中使用此參數,您需要在VB中格式化消息。

所以,當你想發送短信一樣

你好
這是一個文本
三行

您需要發送到批

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

而且您的批次應該看起來像

setlocal EnableDelayedExpansion 
set LF=^ 


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

第二個解決方案,當這不起作用。

創建一個臨時文件,並使用重定向

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" 
+0

的是1號線和2號線,從vb.net生病傳球人數超過2線怎麼辦呢 ? –

+0

不錯的jeb!你能否解釋爲什麼要使用EnableDelayedExpansion這個工作? – RGuggisberg

+0

@AmilaThennakoon'line1'和'line2'是你的短信的多行文字。當你想通過兩行以上的行時,添加更多的換行符,比如'Hello!LF!這是line2!LF!這是line3' – jeb