Running DOS commands (with arguments) from VB6

I'm building a GUI for some useful DOS commands I often use. They may look like this:

saxon file1.xml file2.xsl > file3.html

The problem is that VB's Shell() function won't eat the arguments, only program name. Is there any way to solve this? I don't need any return information from the DOS program.
liljegrenAsked:
Who is Participating?
 
vinnyd79Connect With a Mentor Commented:
I don't know of a saxon command,but have you tried something like this?
Dim A
A = Shell("c:\command.com /c netstat -an > C:\file.txt", vbMinimizedNoFocus)
0
 
PaulHewsCommented:
Capturing the Output of a MS-DOS Program
http://www.vb2themax.com/HtmlDoc.asp?Table=Articles&ID=40
0
 
liljegrenAuthor Commented:
Oh, sorry all for the copies. Just wanted to refresh this page... I'll delete the others.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Chandramouli kArchitectCommented:
i have executed a batch file in a specified path from
shell command. what is the exact DOS command(s) u want to execute from VB?
can u give sample commands u often use.
0
 
PaulHewsCommented:
If you use the link at the top of the page to reload the question you avoid the resubmission of data that results in duplicates.
0
 
PaulHewsCommented:
The article I listed above describes how to reliably use redirection with DOS commands from VB.
0
 
liljegrenAuthor Commented:
Thanks Paul. Time to look at the real problem. :-)
0
 
lyonstCommented:
Can you do it something like this

Example Program
This program shows how you can use the Shell function and the GetNumTasks function to execute an MS-DOS program. This program assumes that you have the PKUNZIP program stored in the UTILS directory and that you have previously created a destination directory called DESTDIR on your hard drive.

Create a new project in Visual Basic. Form1 is created by default.
Add the following statements to the General Declarations section of Form1:
Dim ActiveApps As Integer
Private Declare Function GetNumTasks Lib "Kernel" () As Integer

Add a Command Button control to Form1. Command1 is created by default.
Add the following code to the Click event for Command1:
Private Sub Command1_Click()
    Dim AppDir As String
    Dim Zip As String
    Dim Y As Integer
    Dim X As Integer
   
    AppDir = "c:\destdir"
    ActiveApps = GetNumTasks()
   
    Zip = "c:\utils\pkunzip " & "c:\destdir\" & "test.zip" & " " & AppDir
    X = Shell(Zip, 2)
    SendKeys "%{enter}EXIT%{ }n"
   
    Do While GetNumTasks() <> ActiveApps
        Y = DoEvents()
    Loop
    MsgBox "Pkunzip is finished", 0, "Demo Program"
   
End Sub

http://msdn.microsoft.com/archive/default.asp?url=/ARCHIVE/en-us/dnarvbtips/html/msdn_msdn92.asp
0
 
liljegrenAuthor Commented:
Kcm76, one real-life example is already shown. Some others include batch files with other commands inside.

The GUI is able to build command from input values, but that's another story. I only need to know how to execute them.
0
 
liljegrenAuthor Commented:
Paul, thanks but I don't need to capture the output. The DOS commands I use only generate new text files, so I only have to check if the file is there, after a 5 sec delay or something.
0
 
PaulHewsCommented:
You are definitely using redirection in the example you gave.  The article gives a shell replacement function that should work with any arguments passed to a dos program.
0
 
liljegrenAuthor Commented:
Paul. Have read the article carefully, but it won't work. I think I have found all the code that was *not* included in the example, but VB says that "user-defined types may not be passed ByVal". This is about the SECURITY_ATTRIBUTES and other Types from the WIN32 API.

Here's the line that fails first:

hFile = CreateFile(sTempFile, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, CREATE_ALWAYS, 0, ByVal 0&)

The user-defined type here is at "ByVal 0&".
0
 
PaulHewsCommented:
There is a link to the full source code for the article on the main article page:

http://www.vb2themax.com/Articles/StdOut/Code.zip
0
 
PaulHewsCommented:
The declaration for CreateFile will be slightly different in the source code here than the one you are using.
0
 
liljegrenAuthor Commented:
Okay, I get no errors now, but nothing happens either. The command quoted earlier is supposed to take two files (a document + a stylesheet) and turn into a another file, but no such file appears.

When I run the program step by step, I notice that the call to CreateProcess fails, and so far I have no idea why.

I read about the CreateProcess function in other sources now.
0
 
liljegrenAuthor Commented:
Lyonst, thanks for that link. I don't find any GetNumTasks function in the WIN32 API. Moreover, VB says there's no file called "Kernel", like in that declaration. I tried "Kernel32" too...
0
 
PaulHewsCommented:
Have you tried using shell preceding it with command.com?

Shell "command.com /c saxon file1.xml file2.xsl > file3.html", vbNormalFocus
0
 
liljegrenAuthor Commented:
Funny, I was just going to report that I succeeded by using the solution vinnyd79 suggested, when Paul suggests the same, even closer to the target. I didn't dare to try it first since I didn't know the /c flag. Sorry and thanks for all effort spent on this matter Paul. Since the solution already was there I can only reward vinnyd79 for it. :-)
0
 
PaulHewsCommented:
No prob.  Happy you got it working. :)
0
 
vinnyd79Commented:
The /c will close the dos window.Instead of using vbMinimizedNoFocus like I suggested or vbNormalFocus as Paul suggested,you might want to use vbhide to hide the window.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.