Why "Process" class in vb.net doesn't capture errors from cmd.exe? - vb.net

I'm trying to run dos commands within vb.net program and capture output. I have the following code:
Dim CMDServer As Diagnostics.ProcessStartInfo
Dim CMDReply As Diagnostics.Process
CMDServer = New Diagnostics.ProcessStartInfo
CMDServer.FileName = "cmd.exe"
CMDServer.UseShellExecute = False
CMDServer.RedirectStandardOutput = True
CMDServer.CreateNoWindow = True
CMDServer.Arguments = "/C " + command
CMDReply = Process.Start(CMDServer)
Dim Reply As String = CMDReply.StandardOutput.ReadToEnd()
The code runs successfully if command is a valid dos command, and I get the output in Reply. If the command have no output ( eg: cd\ ) Reply is null. The problem is Reply is null even when the command is invalid. How to capture errors like "command is not recognized as an internal or external command...", "The system cannot find the path specified.." etc.. Please help me. Thanks..

Error messages come in a different output stream called StandardError. Just use a StreamReader or read it directly. Of course, the RedirectStandardError-Property of your ProcessStartInfo instance must be set to True.
Also, there is a ExitCode-Property which returns the ExitCode of the program after it has finished. 0 means 'successful'. Other error codes can be found in the MSDN Documentation. Here is a list of the common exit codes. For example, 2 means The system cannot find the file specified..

Errors are probably output on CMDReply.StandardError, not CMDReply.StandardOutput; try reading it, too. (And set CMDServer.RedirectStandardError to True as well.)

Related

Shell Command to launch application with parameters

I am trying to launch spotfire application from VBA like following
Dim retval As Double
retval = Shell("Path\Spotfire.Dxp.exe", vbNormalFocus)
It works. It launches spotfire with the default servername,username and password
But I want to launch the application by giving the servername, username and password as parameters in the script. How do I do it?
I tried this
retval = Shell("Path\Spotfire.Dxp.exe -s http://spotfire.abcd.com -u ABCD\A7 -p ABC", vbNormalFocus)
But it launched the application with default parameters and gives error at the end "Unable to load file. Could not find the specified file : -s"
Please suggest the possible solution.
after checking our support db, I found a few references to / notation instead of -. so the following command should work:
c:\path\Spotfire.Dxp.exe /server:http://localhost:8080/ /username:user /password:pass

Jython not finding file when using variable to pass file name

So heres the issue guys,
I have a very simple little program that reads in some setup details from a file (to make it reuseable for other sets of data) and stores them into variables.
It then uses one of those variables to open another file that I need to write some results to, as well as various search parameters.
When passing the variable to the .open() function, it fails saying it cant find the file, but when passing the exact same information, but as a written string instead of a variable, it works.
Is this a known problem, or am I just doing something wrong?
The code(problem bit bolded)
def urlTrawl(filename):
import urllib
read = open(getMediaPath(filename), "rt")
baseurl = read.readline()
orgurl = read.readline()
lasturlfile = read.readline()
linksfile = read.readline()
read.close()
webpage = ""
links = ""
counter = 0
lasturl = ""
nexturl = ""
url = ""
connection = ""
try:
read = open(lasturlfile, "rt")
lasturl = read.readline()
except IOError:
print "IOError"
webpage = connection.read()
connection.close()
**file = open(linksfile, "wt")**
file.close()
file = open(lasturlfile, "wt")
file.write(nexturl)
return 1
The information being passed in
http://www.questionablecontent.net/
http://www.questionablecontent.net/view.php?comic=2480
C:\\Users\\James\\Desktop\\comics\\qclast.txt
C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt
strip\"
src=\"
\"
Pevious
Next
f=\"
\"
EDIT: removed working code, to narrow down the problem area and updated code to use a direct reference rather then a relative one.
I found the problem in the end.
The problem was that it was reading in the \n at the end of each line in my details file, and of course the \n isn't anywhere in the website data I'm reading. Removing the last character of each read did the trick:
baseurl = baseurl[:-1]
orgurl = orgurl[:-1]
lasturlfile = lasturlfile[:-1]
linksfile = linksfile[:-1]
search1 = search1[:-1]
search2 = search2[:-1]
search3 = search3[:-1]
search4 = search4[:-1]
search5 = search5[:-1]
search6 = search6[:-1]
I might not be right, but I think this is what's happening.
You're saying this works fine:
file = open('C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt', "wt")
But this doesn't:
# After reading three lines
linksfile = read.readline()
file = open(linksfile, "wt")
There is a difference between these two. In the first piece of code, the double slashes are escapes. They resolve to single slashes when Python is done parsing. Like so:
>>> print 'C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt'
C:\Users\James\Desktop\comics\comiclinksqc.txt
But when you read that same text from the file, there's no parsing of the text. That means that the string stored in your variable still has double slashes.
Try this command out. I bet it fails the same way as when you read the file path in:
file = open(r'C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt', "wt")
The r stands for "raw"; it prevents Python from interpreting escape characters. If it does fail the same way, then the double slashes are your problem. To fix it, in your file, you need to remove the double slashes:
C:\Users\James\Desktop\comics\comiclinksqc.txt
This isn't a problem in CPython 2.7; I'm betting it's not in 3.x, either. CPython interprets double slashes in some manner that they are effectively a single slash (in most cases, at least). So this may be an issue specific to Jython.
If unclean paths cause errors, you might want to consider doing something to clean them up. os.path.abspath might be helpful, although I can't say if Jython's implementation works as well as CPython's:
>>> print os.path.abspath(r'C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt')
C:\Users\James\Desktop\comics\comiclinksqc.txt
>>> print os.path.abspath(r'C:/Users/James/Desktop/comics/comiclinksqc.txt')
C:\Users\James\Desktop\comics\comiclinksqc.txt
I am trying to create a script which will list the datasource name and will show the connection pool utilization(pooled connection, Free Pool Size ext.)
But facing the issue when list the connection pool, if the data source name having space in between the name like "Default Datasource"
then it is listing list "Default Datasource and it is not parsing the datasource name correctly to the next function.
datasource = AdminConfig.list('DataSource', AdminConfig.getid( '/Cell:'
+ cell + '/')).splitlines()
for datasourceID in datasource:
datasourceName = datasourceID.split('(')[0]
print datasourceName
Request you to help if possible drop me mail at bubuldey#gmail.com
Regards,
Bubul

Running DOS command from vb.net

I want to run the following command from vb.net code. When I put it in process.start(" ")
it returns syntax error. Please advise
>E:\UnInstall\SQLServer\SQLServerExpress2008\SQLEXPR_x64_ENU.exe / SQ/SAPWD="testpwd123"/security=SQL/BROWSERSVCSTARTUPTYPE="Enabled"/TCPENABLED="1"/NPENABLED="0"/INDICATEPROGRESS="True"/INSTANCENAME="CBEInstance"/IACCEPTSQLSERVERLICENSETERMS="True"
You specify the file to run with Process.StartInfo.Filename, and the command line arguments with Process.StartInfo.Arguments.
Dim DosRun As Process = New Process
DosRun.StartInfo.FileName = "E:\UnInstall\SQLServer\SQLServerExpress2008\SQLEXPR_x64_ENU.exe"
DosRun.StartInfo.Arguments = String.Format("SQ/SAPWD=testpwd123/security=SQL/BROWSERSVCSTARTUPTYPE=Enabled/TCPENABLED=1/NPENABLED=0/INDICATEPROGRESS=True/INSTANCENAME=CBEInstance/IACCEPTSQLSERVERLICENSETERMS=True")
DosRun.Start()

How to write a ping to a text file using VBS

If I am using VBS to run some CMD commands, in this example ping, how could I write the command to a text file using VBS not DOS?
Set objCmdTest = WScript.CreateObject ("WScript.Shell")
Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\vbs\test.txt",8,true)
Output.WriteLine (objCmdTest.run ("ping failboat"))
Output.WriteLine (objCmdTest.run ("ping 8.8.8.8"))
So this is what I'm working with however what happens is; The script runs, the file is made, 2 command prompts open to run the pings and finally the text inside the file reads:
0
0
When I'd much prefer it to have the ping output.
FYI: Please don't offer suggestions that require me to use DOS for the writing, I'd like to see how VBS can do what I need for multiple reasons, thanks!
The instruction Output.WriteLine (objCmdTest.run ("ping failboat")) will write the return value of the Run method to the output file. If you want to append the command output to an output file you have to either redirect the output in the command:
objCmdTest.run "%COMSPEC% /c ping failboat >>C:\vbs\test.txt", 0, True
or use Exec instead of Run:
Set ping = objCmdTest.Exec("ping failboat")
Do While ping.Status = 0
WScript.Sleep 100
Loop
Output.WriteLine ping.StdOut.ReadAll
WScript.Shell's run method returns the process's exit code. In order to get access to an application's output, you need to use the exec method instead, and use the object that returns to get access to the process's standard output through its StdOut property.

Upload Files to Mainframes from VB.net

I am trying to upload a file from my pc to mainframes. I am trying to upload it using Chilkat FTP2. Below is the code.
The file I am trying to upload is 2009102600000
Dim ftp As New Chilkat.Ftp2()
Dim success As Boolean
' Any string unlocks the component for the 1st 30-days.'
success = ftp.UnlockComponent("Anything for 30-day trial")
If (success <> true) Then
MsgBox(ftp.LastErrorText)
Exit Sub
End If
ftp.Hostname = "www.myside.com"
ftp.Username = "****"
ftp.Password = "****"
' The default data transfer mode is "Active" as opposed to "Passive".'
' Change it to Passive by setting the Passive property:'
ftp.Passive = true
' Connect and login to the FTP server.'
success = ftp.Connect()
If (success <> true) Then
MsgBox(ftp.LastErrorText)
Exit Sub
End If
' Change to the remote directory where the file will be uploaded.'
success = ftp.ChangeRemoteDir("ABC.SITEUPLOAD.UPLOAD")
If (success <> true) Then
MsgBox(ftp.LastErrorText)
Exit Sub
End If
' Upload a file.'
Dim localFilename As String
localFilename = "c:\2009102600000"
Dim remoteFilename As String
remoteFilename = "2009102600000"
success = ftp.PutFile(localFilename,remoteFilename)
If (success <> true) Then
MsgBox(ftp.LastErrorText)
Exit Sub
End If
ftp.Disconnect()
MsgBox("File Uploaded!")
The error I am getting is dataset not found use MVS dsn name or something like that.
I would really appreciate if you can help me out with this one please.
I'm not at all sure you can treat data set prefixes as directories. When I'm doing uploads to the mainframe with ftp, I always just specify the full target name. I would get rid of the
ftp.ChangeRemoteDir("ABC.SITEUPLOAD.UPLOAD")
section altogether and just change:
remoteFilename = "2009102600000"
to:
remoteFilename = "'ABC.SITEUPLOAD.UPLOAD.2009102600000'"
if it's a sequesntial data set, or:
remoteFilename = "'ABC.SITEUPLOAD.UPLOAD(2009102600000)'"
if it's a member (in which case the data set will have to exist first).
It would also help if you changed the MsgBox statements so that they included an indication as to what is actually causing the error. Something along the lines of:
MsgBox("Connect error: " & ftp.LastErrorText)
MsgBox("ChangeRemoteDir error: " & ftp.LastErrorText)
MsgBox("PutFile error: " & ftp.LastErrorText)
instead of the generic:
MsgBox(ftp.LastErrorText)
One other point: you'll notice I've put single quotes around the targets above. That's because z/OS has a habit of (sometimes) prefixing your login name to members. It may be that:
put xyz.txt upload(xyz)
is actually trying to put it into yourname.upload(xyz). Quoting it will prevent that.
Update: You know, I just noticed something that totally escaped me the first time I read this question. The error message spells it out plainly.
Data set name segments and member names within partitioned data sets are limited to 8 characters. Hence your 'ABC.SITEUPLOAD.UPLOAD(2009102600000)' is invalid on two counts, the SITEUPLOAD and the 2009102600000. Try shortening the names and re-transferring.
Here's the proof:
C:\Documents and Settings\Pax> ftp bigiron
Connected to bigiron.box.com.
220-FTPD1 IBM FTP CS V1R9 at BIGIRON.BOX.COM, 02:15:23 on 2009-11-06.
220 Connection will close if idle for more than 5 minutes.
User (bigiron.box.com:(none)): pax
331 Send password please.
Password:
230 PAX is logged on. Working directory is "PAX.".
ftp> put test.txt 'pax.siteupload'
200 Port request OK.
501 Invalid data set name "'pax.siteupload'". Use MVS Dsname conventions.
ftp> put test.txt 'pax.siteupld'
200 Port request OK.
125 Storing data set PAX.SITEUPLD
250 Transfer completed successfully.
ftp: 177 bytes sent in 0.00Seconds 177000.00Kbytes/sec.
ftp> put test.txt 'pax.jcl(abcdefghi)'
200 Port request OK.
501 Invalid data set name "'pax.jcl(abcdefghi)'". Use MVS Dsname conventions.
ftp> put test.txt 'pax.jcl(abcdefgh)'
200 Port request OK.
125 Storing data set PAX.JCL(ABCDEFGH)
250 Transfer completed successfully.
ftp: 177 bytes sent in 0.00Seconds 177000.00Kbytes/sec.
ftp> bye
221 Quit command received. Goodbye.
Are you sure you don't need to store it as a generational dataset from the root directory? Like this:
'ABC.SITEUPLOAD.UPLOAD.2009102600000(+1)'