Solved

Execute Autoexec-like File(s)

Posted on 2001-07-23
8
287 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
 
LVL 27

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now