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

x
?
Solved

Running DOS commands (with arguments) from VB6

Posted on 2001-09-17
20
Medium Priority
?
338 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:liljegren
[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
  • 8
  • 8
  • 2
  • +2
20 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 600 total points
ID: 6487649
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487651
Capturing the Output of a MS-DOS Program
http://www.vb2themax.com/HtmlDoc.asp?Table=Articles&ID=40
0
 

Author Comment

by:liljegren
ID: 6487661
Oh, sorry all for the copies. Just wanted to refresh this page... I'll delete the others.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:kcm76
ID: 6487668
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487670
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487675
The article I listed above describes how to reliably use redirection with DOS commands from VB.
0
 

Author Comment

by:liljegren
ID: 6487678
Thanks Paul. Time to look at the real problem. :-)
0
 
LVL 15

Expert Comment

by:lyonst
ID: 6487688
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
 

Author Comment

by:liljegren
ID: 6487692
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
 

Author Comment

by:liljegren
ID: 6487700
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487722
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
 

Author Comment

by:liljegren
ID: 6488035
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488053
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488064
The declaration for CreateFile will be slightly different in the source code here than the one you are using.
0
 

Author Comment

by:liljegren
ID: 6488198
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
 

Author Comment

by:liljegren
ID: 6488206
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488236
Have you tried using shell preceding it with command.com?

Shell "command.com /c saxon file1.xml file2.xsl > file3.html", vbNormalFocus
0
 

Author Comment

by:liljegren
ID: 6488348
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488360
No prob.  Happy you got it working. :)
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 6488408
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

705 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