Using DOS commands in VB code? 
Author Message
 Using DOS commands in VB code?

Can anyone give me some xsample how to use MS  DOS commands like
- ping
- nslookup
- net use
and so on, in VB code?

I would be very greatefull


Wed, 18 Jun 1902 08:00:00 GMT  
 Using DOS commands in VB code?
Here is a piece of the code I am using in an app for Windows NT using Net
Send.  I have a listbox of userid's one or more can be highlighted.  If no
id is hightlighted comes back with a msgbox stating no ID is highlighted.

lstUser.List =  listbox of UserId's or workstation names
MyMsg = Typed message to send
MyName = Optional put your name at end of message.

One of the things I like about this over Win95 Winpopup or NT's Chat is that
I can store a list of ID's or workstation names.  All I have to do is
highlight the ones to send a message to and type a message.  Windows 95/98
users don't get the message unless they have WinPopUp running.

'start loop to find the highlighted UserID.
For y = 0 To lstUsers.ListCount - 1

    If lstUsers.Selected(y) Then
        tUser = "Yes"

        x = Shell("Net Send" & " " & lstUsers.List(y) & " " & MyMsg & vbCrLf
& vbCrLf & MyName)

    End If

Next y

> Can anyone give me some xsample how to use MS  DOS commands like
> - ping
> - nslookup
> - net use
> and so on, in VB code?

> I would be very greatefull

> Kristoffer

Fri, 23 May 2003 11:30:05 GMT  
 Using DOS commands in VB code?
The following is a compilation of several posts I've given in the past
regarding the Shell command. Although it doesn't address your command needs
specifically, it gives you more information than you asked about. Somewhere
in there is the answer to your question -- the rest is for your future



You can use the Shell command. To execute internal DOS command (Dir, Copy,
etc. as well as redirection of screen output), the command processor must be
specified. Specifying the command processor is safe & generic and will work
with non-internal commands also. That syntax, using an XCopy command as an
example is:

Shell  Environ("comspec") & " /c xcopy """ & _
         Source & """ """ & Destination & """ " & Option, vbHide

You set the Source and Desination (string variables) to the appropriate
paths and the Option (string variable), if any, which can be found by
opening an MSDOS prompt and typing xcopy /?. One more example would be to
list all the files in a directory including subdirectories and
subdirectories of subdirectories and all of their files.

  CommandLine = "dir """ & FileSpec & _
                             """ /s/b > """ & RedirectTo & """"
  Shell Environ("comspec") & " /c " & CommandLine, vbHide

Here, the output of a Dir command is redirected to a file-path you specify
in the RedirectTo (string variable). The /s/b are options to the Dir command
that tell it to recurse throught its subdirectories and not to include
header or summary information.

I used a variable for the file name so that I could more easily explain the
benefit of encasing it in quotemarks. If you redirect to a file that has
spaces in its name, or if there are spaces in the path specification itself,
then the filename *must* be quoted to protect the spaces from DOS's desire
to use them as delimiters. (That's what all those quotemarks in the Shell
statement are for.) If the filename doesn't have spaces in it, the quotes
aren't necessary BUT they don't hurt either. Hence, the above will work with

As for your PING question, something like the following should work:

     strIP = ""
     Shell Environ("comspec") & " /c ping " & _
              strIP & " > """ & RedirectFile & """", vbHide

Although you didn't specify it in your original post, I assume you want to
use vbHide for the optional 2nd parameter to Shell. This hides the DOS
window so that your user doesn't see it. If you want the DOS window to
remain visible, you would use the vbNormalFocus BUT you must use a /k
instead of a /c for the command processor argument.

The above assumes you do NOT have to wait for this file to be completely
written before your code continues executing. If you have to work with this
file right after it is created, consider one of these (which makes your
program wait until the DOS process is finished):

See this link

Note: This method doesn't use Shell -- it uses CreateProcessA

Paste these lines in the (General)(Declarations) section of the form where
the Shell is being called (or remove the Private keywords and put them in a
BAS module if more than one form will use them):

Private Declare Function OpenProcess _
        Lib "kernel32" _
        (ByVal dwDesiredAccess As Long, _
         ByVal bInheritHandle As Long, _
         ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle _
        Lib "kernel32" _
        (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject _
        Lib "kernel32" _
        (ByVal hHandle As Long, _
         ByVal dwMilliseconds As Long) As Long

Call your Shell command in this form with the appropriate Shell arguments
placed in the parentheses:

PID = Shell( <<Put Shell Arguments Here>> )

And finally, paste the following IMMEDIATELY after the PID=Shell statement
above (making sure to handle the possible error where indicated; i.e. stop
the code from falling through to your other commands if the Shell failed):

If PID = 0 Then
     'Handle Error, Shell Didn't Work
     hProcess = OpenProcess(&H100000, True, PID)
     WaitForSingleObject hProcess, -1
     CloseHandle hProcess
End If

One note -- there are some NT systems (those with VERY tight security
measures in place) where this call won't work. No problem at all on 95/98

> Can anyone give me some xsample how to use MS  DOS commands like
> - ping
> - nslookup
> - net use
> and so on, in VB code?

> I would be very greatefull

> Kristoffer

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

Wed, 18 Jun 1902 08:00:00 GMT  
 [ 3 post ] 

 Relevant Pages 

1. Using Dos commands without dos window

2. executing DOS command using VB 5

3. executing dos command using VB

4. Run a DOS command and Get output of a DOS command in VB5

5. Running DOS Shell in hidden window, or using vb command instead

6. vb shell command to replace dos command

7. How to code Voice AT Commands using VB 4.0 MSComm

8. Voice AT Commands Using VB 4.0 MSComm Code

9. DOS Commands Using VBA

10. Problem executing DOS command using Shell function

11. Problem using sendkeys command to send keystrokes to DOS application

12. Sending Restore Command to DOS Window using VB4 and SendMessage API


Powered by phpBB® Forum Software