Exe & Com

Posted on 1998-10-31
Last Modified: 2010-04-30
How can I run all Exe and com files in the current dir?

Question by:marka64
  • 3
  • 3
LVL 12

Accepted Solution

mark2150 earned 20 total points
ID: 1442728
Seems kind of silly, but is not a real problem.

DIR command will give you files. Save into list and then execute each one with

SHELL( progname )

This will run all files found by the DIR command. Naturally you'll have to do two DIR's, one with *.COM and one with *.EXE as first argument. SHELL() runs async so will spawn several children at once. I have code for ExecCmd that waits for child to finish, but don't know if it will work on VB3 (is VB5).



Author Comment

ID: 1442729
Mark2150 Thanks but i still have a problem im new to  vb and i wondered if you could give me the sorce code or tell me in detail what to do?

Author Comment

ID: 1442730
Mark2150 Thanks but i still have a problem im new to  vb and i wondered if you could give me the sorce code or tell me in detail what to do?
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

LVL 12

Expert Comment

ID: 1442731
Ok, DIR command takes param on the *FIRST* call only that specifies what file it should look for.

Assume you have a list control:

List1.Clear       'Clear list
File = DIR( "*.EXE" )    'Search for first .EXE file
DO WHILE LEN( file ) > 0
    List1.Additem file
    File = DIR()     ' No param on 2nd & subsequient calls

Ok, List1 now has all .EXE files in current directory. Repeat above (except List1.Clear) for *.COM.

FOR ndx = 0 to List1.ItemCount -1   ' Go thru all elements of array (remember, list is 0 based)
    SHELL( List1.Item(ndx) ), 1    ' Spawn child task
NEXT ndx    'Repeat until all programs have been run.

The drawback of this is that the SHELL() command doesn't wait for any of the child tasks to complete. If you have a *LOT* of .EXE/.COM files in the directory you should see an *explosion* of windows as they *ALL* fire more-or-less at once. This can lead to your resources rapidly dropping and to an eventual crash.

End run around this problem is to use ExecCmd command instead of SHELL. This will *WAIT* for each child to finish before starting the next. ExecCmd is not an internal, rather it is a user function.

' These data type declarations are required to support the obscure windows
' function to spawn a child task and wait for it to finish before continuing
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" ( _
    ByVal lpApplicationName As Long, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, _
    ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const INFINITE = -1&
' I don't really understand the above declares, but they do work so DON'T MESS WITH THEM!

Public Sub ExecCmd(cmdline$)
' This executes an external windows program and waits for it to complete
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, 5000)
    If ret& <> 0 Then GoTo holdhere
ret& = CloseHandle(proc.hProcess)
End Sub



Author Comment

ID: 1442732
Thanks but there is still a problem when i try to run my program it gets to the shell command and says:
Excepted: Statement
Can you help?
LVL 12

Expert Comment

ID: 1442733
Dunno. Should work. List code was written off the top of my head. When you hover cursor over List1.List(n) statement does the program name show up?



Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 55
Validating VB6 Function 19 59
using web browser with BING 40 121
Using an encrypted  value to decrypt and display contents in vb6 9 52
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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

809 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