Execute Autoexec-like File(s)

Posted on 2001-07-23
Last Modified: 2006-11-17
Hi Experts,
Can VB executes (or acts as) a autoexec-like file, which could execute line per line, for each command line? I need some examples for this questions.

Thank you.
Question by:MaxLen

Expert Comment

ID: 6308978

do you mean shell out and execute some .exes

shell "c:\ /e file.exe",,vbshow
shell "c:\ /e file1.exe",,vbshow

end sub

otherwise if you are wanting this to execute when you are booting the computer the only way to do it is to create and autoexec.bat file which points to your compiled .exe VB app.

but what would the point of the be because you will be able just to do the same in a batch file.

id still answer no you cannot do a vb - autoexec.bat file. without loading the exe file of the vb app first.

hope this helps

Expert Comment

ID: 6309012
Copy and Paste this code
It should do the trick

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
     "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
     String, ByVal lpszFile As String, ByVal lpszParams As String, _
     ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

     Private Declare Function GetDesktopWindow Lib "user32" () As Long

     Const SW_SHOWNORMAL = 1

     Const SE_ERR_FNF = 2&
     Const SE_ERR_PNF = 3&
     Const SE_ERR_OOM = 8&
     Const SE_ERR_DLLNOTFOUND = 32&
     Const SE_ERR_SHARE = 26&
     Const SE_ERR_DDETIMEOUT = 28&
     Const SE_ERR_DDEFAIL = 29&
     Const SE_ERR_DDEBUSY = 30&
     Const SE_ERR_NOASSOC = 31&
     Const ERROR_BAD_FORMAT = 11&

     Function StartDoc(DocName As String) As Long
         Dim Scr_hDC As Long
         Scr_hDC = GetDesktopWindow()
         StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
         "", "C:\", SW_SHOWNORMAL)
     End Function

Private Sub Command1_Click()

Dim r As Long, msg As String
         r = StartDoc("Name of Program")
         If r <= 32 Then
             'There was an error
             Select Case r
                 Case SE_ERR_FNF
                     msg = "File not found"
                 Case SE_ERR_PNF
                     msg = "Path not found"
                 Case SE_ERR_ACCESSDENIED
                     msg = "Access denied"
                 Case SE_ERR_OOM
                     msg = "Out of memory"
                 Case SE_ERR_DLLNOTFOUND
                     msg = "DLL not found"
                 Case SE_ERR_SHARE
                     msg = "A sharing violation occurred"
                 Case SE_ERR_ASSOCINCOMPLETE
                     msg = "Incomplete or invalid file association"
                 Case SE_ERR_DDETIMEOUT
                     msg = "DDE Time out"
                 Case SE_ERR_DDEFAIL
                     msg = "DDE transaction failed"
                 Case SE_ERR_DDEBUSY
                     msg = "DDE busy"
                 Case SE_ERR_NOASSOC
                     msg = "No association for file extension"
                 Case ERROR_BAD_FORMAT
                     msg = "Invalid EXE file or error in EXE image"
                 Case Else
                     msg = "Unknown error"
             End Select
             MsgBox msg
         End If
end sub


Expert Comment

ID: 6309079
Private Sub Form_Load()
 Dim arrParams() as string
 arrParams = Split(Command$," ")
 For i = 1 To UBound(arrParams)
   msgbox arrParams(i)  'do you line items here...
End Sub
LVL 27

Expert Comment

ID: 6310952
VB works exactly same way - execute code line-by-line.

Private Sub Command1_Click()
    Dim sCmd As String
    Open "c:\autoexec.bat" For Input As #1
        While Not EOF(1)
          Line Input #1, sCmd
          Shell "start " & sCmd, vbHide
    Close #1
End Sub

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Expert Comment

ID: 6311388

Here's a file over the net for you:


Description: This code will show you how to open an .ini file into a listbox, then take a random line from that listbox. This code is useful for programs like Hangman or Magic 8-Ball

You may want to use this code to load your file, into a listbox, then read the listbox line by line and then using the instr method to get the command or whatever then shell whatever application that you want.

That's it!

glass cookie ; )

Accepted Solution

glass_cookie earned 100 total points
ID: 6311392

This is a better example:


Descrition: This routine will open an file, read it line by line and put each line in a listbox. When the text stops (there is no text anymore in the file)the program will also stop. So each line in ur file will come in a listbox

That's it!

glass cookie : )

Expert Comment

ID: 6311701
It can, as described above.

But, may I suggest something else ?

Why don't you write your code into a .VBS file.
That's pure Visual Basic, and you can simply run it from the command prompt by typing "yourfile.vbs", since VBS files are automatically linked to the scripting environment just like a .exe, .bat or .com are executables.

Expert Comment

ID: 6529344

Status update?  Are these the things you are looking for?

If I understand, you want to provide a method for processing commands line by line, similar to a batch file (which the autoexec acts like).

One option is a batch file.  If you want it to initiate at startup, you can call for it in the autoexec.bat.

Conversely, if you want to process other code, you can create a VB.exe that will process the code you want, and place the exe in the StartUp folder so that it will always run on start-up.

If you want the user to be able to dynamically change what is processed, you could use a text file.  Your program would look to the text file for the commands to process.

Is this what you are looking for?? If not, what do you need that is not posted.



Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending a email via excel using vba 6 72
VB6 - Convert HH:MM into Decimal 8 51
Protecting vb6 & .Net code Obfuscation 18 95
VB6 ListBox Question 4 33
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

910 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

17 Experts available now in Live!

Get 1:1 Help Now