duper
duper

Reputation: 243

How to pass a parameter from Batch (.bat) to VBScript (.vbs)?

How can I pass a parameter from batch to vbscript? My batch script sends out an email at the end of the automated execution. For that it uses/calls my vbscript (email.vbs) that sends out the email with an actual log file (that has the results for that execution) attached. All those log files are stored in specific folders such as: 201207(July, 2012), 201208(August, 2012) and so on.... I want to pass the folder name or part of (i am thinking about hard coding the 2012 part and get the month number from that parameter through my batch) it as a parameter to my email.vbs so that it can go look for the right folder to grab the right log file. Makes sense?

ECHO Checking the log file for errors...
FINDSTR /C:"RC (return code) = 0" %workDir%\%filenm%_Log.txt && (ECHO Deployment was successful. 
ECHO Email is sent out...
cscript //nologo success_mail_DEV.vbs %workDir%  'passing the directory param. here.
ECHO Press ENTER to exit...
GOTO offshore) || (ECHO Deployment was not successful. Errors were found!
ECHO Email is sent out...
ECHO Press ENTER to exit...
cscript //nologo fail_mail_DEV.vbs %workDir%  'and here
GOTO offshore)

This is part of what I have right now. It is checking for errors in the log file and calling that success/failed mail accordingly. Right now the location name/number is hardcoded in those 2 vbs mailing scripts that you see up there. I am sure there is a way to pass a parameter somewhere up there to the emailing vbscript. But, i don't know how to.

This is my mailing vbscript:

Const ForReading = 1

Set args = WScript.Arguments
directory = args.Item(0) 'thought that workDir would come in here


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(&directory&"\filename.txt", ForReading)
fileName = objTextFile.ReadLine

Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MyTime
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
MyTime = Now

ToAddress = "[email protected]"
MessageSubject = "SUCCESS"
MessageBody = "It was successful" 
MessageAttachment = &directory&"\"&fileName&"_Log.txt"
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf & MyTime
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

objTextFile.Close

But not working...

Thanks in advance!

Upvotes: 6

Views: 50814

Answers (3)

duper
duper

Reputation: 243

Alright, I am updating that question I had. It's finally working, with all of your help, guys. Thanks. Here is how I called the vbscript and passed a parameter to it:

cscript //nologo fail_mail.vbs something.sql 'something.sql is the param. that i'm passing.

Here is what my vbscript for mail looks like:

Const ForReading = 1

Set args = WScript.Arguments
arg1 = args.Item(0)  'the parameter from batch comes in here to arg1
...
...
ToAddress = "[email protected]"
MessageSubject = "WORKED"
MessageBody = "Success" 
MessageAttachment = ""&arg1&""  'here those quotes are important. dk why.
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf & MyTime
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

And it is working. *One can pass more than one parameters using the same technique.

Upvotes: 3

Patrick Cuff
Patrick Cuff

Reputation: 29786

Your VBScript needs some code to accept the arguments, for example:

set args = WScript.Arguments
' Parse args
select case args.Count
case 0
    help
case 1
    sVariable = args(0)
end select

When you call your VBScript, just pass the arg to the script, like you would a command:

cscript //nologo MyScript.vbs arg

Upvotes: 0

Cheeso
Cheeso

Reputation: 192467

To answer the question...

invoke your script this way:

cscript //nologo success_mail_DEV.vbs  ARG1  ARG2

Handle arguments within vbscript via WScript.Arguments.


But may I also suggest that you could eliminate the batch portion of the system completely.

VBSCript is perfectly capable of invoking FINDSTR and handling the output. Or in fact you could implement the search wholly within VBScript with no need to invoke FINDSTR at all.

Upvotes: 7

Related Questions