Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reg: How to use batch script output value  in  vbscript

Posted on 2016-10-19
10
Medium Priority
?
40 Views
Last Modified: 2016-11-08
Hi All,

i have Below Batch script command

@echo off
set VAR=%errorlevel%
D:\SchedTasks\IFRS-Transfer\psftp.exe IFRS@DM3CXJ -pw XXXX -b IFRSTransfer.txt
echo %VAR%
exit /b %ERRORLEVEL%

And i wanted to use VAR value of this batch script in VBscript.

so kindly please tell me how can i use .

Thanks,
Abhishek
0
Comment
Question by:Abhishek kumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 18

Expert Comment

by:xtermie
ID: 41849602
Use the WScript.Shell object's Environment property.
Example (gets the NUMBER_OF_PROCESSORS variable, this is taken from
Windows Script Host documentation):

Set WshShell = WScript.CreateObject("WScript.Shell") 
Set WshSysEnv = WshShell.Environment("SYSTEM") 
WScript.Echo WshSysEnv("NUMBER_OF_PROCESSORS") 

Open in new window


Refer to
https://msdn.microsoft.com/en-us/library/at5ydy31(v=vs.84).aspx
https://msdn.microsoft.com/en-us/library/6s7w15a0(v=vs.84).aspx
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41849605
Why? VAR contains the error level at batch start, which is 0. This doesn''t have any "value". The exit code is important, and that is what you automatically get from the Shell.Run method calling the batch file.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41849611
You're aware that %VAR% contains the errorlevel before the psftp.exe call?
Anyway, assuming you want to call the VBScript from this batch (or are you calling the batch from VBScript? that would be rather cumbersome, because you might just as well run the psftp.exe directly from VBScript), you just pass it as command line argument:
VBS:
VAR = WScript.Arguments(0)
Wscript.Echo "VAR is " & VAR

Open in new window

Batch:
@echo off
set VAR=%errorlevel%
D:\SchedTasks\IFRS-Transfer\psftp.exe IFRS@DM3CXJ -pw XXXX -b IFRSTransfer.txt
echo %VAR%
cscript.exe //nologo "C:\Temp\Whatever.vbs" %VAR%
exit /b %ERRORLEVEL%

Open in new window

1
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Abhishek kumar
ID: 41849756
Hi,
Actually i wanted to use the psftp.exe exit code value in vb script for sending the mail whether file transfer is successful or Failed.

Here i am calling Batch script command inside VB Script.

So please help me how can i do this .

Thanks,
Abhishek
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points (awarded by participants)
ID: 41849778
Batch:
@echo off
D:\SchedTasks\IFRS-Transfer\psftp.exe IFRS@DM3CXJ -pw XXXX -b IFRSTransfer.txt
exit /b %ERRORLEVEL%

Open in new window

VBS:
Set objShell = CreateObject("WScript.Shell")
intReturn = objShell.Run(Chr(34) & "D:\Temp\Whatever.cmd" & Chr(34), 0, True)

Open in new window


Or just call psftp.exe directly from VBS:
strFtpProgram = "D:\SchedTasks\IFRS-Transfer\psftp.exe"
strFtpUser = "IFRS@DM3CXJ"
strFtpPassword = "XXXX"
strFtpFile = "IFRSTransfer.txt"

Set objShell = CreateObject("WScript.Shell")
strCommand = Chr(34) & strFtpProgram & Chr(34) & " " & strFtpUser & " -pw " & Chr(34) & strFtpPassword & Chr(34) & " -b " & Chr(34) & strFtpFile & Chr(34)
intReturn = objShell.Run(strCommand, 0, True)

Open in new window

Run Method (Windows Script Host)
https://msdn.microsoft.com/en-us/library/d5fk67ky.aspx
1
 

Author Comment

by:Abhishek kumar
ID: 41849908
Thank you so much :)

its working as expected :)
0
 

Expert Comment

by:Abhishek kumar
ID: 41851460
Hi oBdA,

For below Script

@echo off
pscp.exe -i seom.ppk D:\Informatica\9.6.1\server\infa_shared\TgtFiles\IFRS\Customer\zrrem19cus* seom@sapcir-qa.intranet.z.ca:/infiles
exit /b %ERRORLEVEL%

its not returning the Exitcode value and also the batch command is running for longer time

so kindly please help me how can i use exit code value of this batch script in VBscript.

Thanks,
Abhishek
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41851467
That's not an issue with the batch file itself.
If this batch runs a long time, then it's the pscp.exe command that's taking long; the batch script will just wait until it's done.
Once pscp.exe is done, the errorlevel returned by it will be passed to the calling program just like with psftp.exe (that is, if pscp.exe returns an errorlevel to begin with - not all programs do).
1
 

Expert Comment

by:Abhishek kumar
ID: 41851531
Okay Sir.

Thank you so much :)
0
 
LVL 18

Expert Comment

by:xtermie
ID: 41878454
Solution provided
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question