Solved

Running DOS commands (with arguments) from VB6

Posted on 2001-09-17
20
312 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
  • 8
  • 8
  • 2
  • +2
20 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 200 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now