Link to home
Start Free TrialLog in
Avatar of MaxLen
MaxLen

asked on

Execute Autoexec-like File(s)

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.
Avatar of andysalih
andysalih

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
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.
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
Avatar of Ark
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
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 ; )
ASKER CERTIFIED SOLUTION
Avatar of glass_cookie
glass_cookie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.