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
Solved

Execute Autoexec-like File(s)

Posted on 2001-07-23
8
298 Views
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.
0
Comment
Question by:MaxLen
8 Comments
 
LVL 3

Expert Comment

by:andysalih
ID: 6308978
yes

do you mean shell out and execute some .exes

shell "c:\command.com /e file.exe",,vbshow
shell "c:\command.com /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
Andy
0
 
LVL 4

Expert Comment

by:VincentLawlor
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_ACCESSDENIED = 5&
     Const SE_ERR_OOM = 8&
     Const SE_ERR_DLLNOTFOUND = 32&
     Const SE_ERR_SHARE = 26&
     Const SE_ERR_ASSOCINCOMPLETE = 27&
     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

Vin.
0
 
LVL 8

Expert Comment

by:Dave_Greene
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...
 Next
End Sub
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 28

Expert Comment

by:Ark
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
        Wend
    Close #1
End Sub

Cheers
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6311388
Hi!

Here's a file over the net for you:

Download...
http://www.planetsourcecode.com/upload/ftp/CODE_UPLOAD14742292001.zip

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 ; )
0
 
LVL 8

Accepted Solution

by:
glass_cookie earned 100 total points
ID: 6311392
Hi!

This is a better example:

Download...
http://www.planetsourcecode.com/upload/ftp/CODE_UPLOAD3851372000.zip

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 : )
0
 
LVL 4

Expert Comment

by:vindevogel
ID: 6311701
MaxLen,
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.
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 6529344
MaxLen,

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.

Thanks.

Wileecoy.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

861 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