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
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
  • 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?
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

615 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