How To Excute the DOS Commands in Visual Basic

1
Maybe the Batch file thing will help the person who posted the original thread but... What I need is to be able to open the command prompt and then have my next command actually type text (i.e. "ping /t " & IPBox1.Text). I have been searching but EVERYONE, seriously everyone wants to push the "Batch File" thing. I have not found one post to tell how to do it without using a batch file. I can get the cmd window to open but my next line will not add the text I want it to ping. I need my text box window's data to be used in the command window.

Could you or someone help me with that?

FYI: I try to do all my scripts and programs to be dynamic like "DHCP". Static is nice for Web page IP addresses but... I work with a lot of computers and I don't have time to edit the code for every server.

Thank you.
 
C

Curt Wuollet

Why don't you write your own ping? The way to do DOS things not in the book is to write a command to a buffer set some register variables and call an interrupt routine. Another way might be to call ping with shell parameters. substitution) after writing them to the environment. Or investigate the possible use of pipes.

Don't dismiss using the shell, because you don't have source for the commands and thus no way of knowing how they operate, you are going to have to use the meager facilities provided. This would be no
problem at all in Linux. For that matter, there is a strong possibility that there is a ping routine that is not a command line
tool as many network programs need to check if there is a listener on a port.

You could also investigate VB networking code to write a ping into your code. Which gets us back to the start...........

There you go, several ideas, none of which involve a batch file.

Regards
cww
 
K

Ken Emmons Jr.

I believe people have already answered this in the thread? I am not an expert at visual basic, but the shell function call others have mentioned looks like the type of thing I would use.

- You build up your string that represents the "typed text" and then pass it to the shell function. The string can be dynamically built based on runtime variables, files, etc.

I'd type an example but its been a while since using VB.

KEJR
 
J

J|A|Mer Delmar

hey guys,

i'm trying to build a program in VB 2008 express edition that will run the command prompt, type: format /FS:NTFS X:
where X is the drive, and NTFS is the file system.

then, i want the program to hit enter in the command prompt, so that the drive is formatted. so far, i have the code necessary to run the cmd.exe, and my program, just not how to input the above, and how to hit enter...

reply to me at delmar1992 [at] hotmail.com
thanks
 
the only way i know that should be working (but is not working) is this:

shell("cmd.exe")
sendkeys.send(" (put anything you want between the quotes) ")

this simply sends a command but you should have a delay after the shell if your computer is slow simply here i will change the color of the cmd

shell("cmd.exe")
sendkeys.send("color fc")

that's all (but its not working)
 
sir...how can you execute this code directly in vb.net?

attrib -s -h /s /d


can you help me sir???...tnx!!!
 
You would need 2 timers for this method...

timer1.interval = 5000
timer2.interval = 500

Private Sub Timer1_Timer()
Shell "C:\Windows\system32\cmd.exe", vbNormalFocus 'opens CMD
Timer2.Enabled = True 'timer2 gives cmd a moment to start up
End Sub

Private Sub Timer2_Timer()
SendKeys "format /FS:NTFS X:" 'format code
SendKeys "{Enter}" 'enter code
Timer2.Enabled = False 'resets timer2
End Sub

This is probably not exactly what you need but you get the idea.
 
>shell("cmd.exe")
>sendkeys.send("color fc")
>
>that's all (but its not working)
-------------------------------------------------
reply

try sendkeys.send(" color fc {ENTER} ")
it works
 
M
Hi - So if I understand this right, I can use Shell to run an instance of devcon to disable and enable a usb device that has 'locked up'? MW
 
M
To answer my own question (with the help of TheDirector's answer above) a command button called Command1 with the following code attached...

Private Sub Command1_Click()
Dim sYourCommand As String
sYourCommand = "C:\devcon DISable " & """@HID\VID_05AC&PID_0304\6&306D5220&0&0000"""
Shell "CMD /c" & sYourCommand
End Sub

... will disable the USB mouse whose Device Instance ID (from (XP) Device Manager > relevant device > Properties > Details tab > Device Instance ID) is HID\VID_05AC&PID_0304\6&306D5220&0&0000. This has exactly the same effect as highlighting the relevant device in Device Manager and clicking on the 'Disable' icon. You can re-enable it replacing 'disable' in the code with 'enable' or use any of the other DEVCON commands.

For testing, instead of using the /c you can use /K which does not close the command prompt window so you can see what has happened. MW
 
I know this post is really old but this might be helpful for some people who need to transfer what is in the textbox to a command prompt.

You could create a form with a textbox and button in VB so a user would just have to type in a PC name and click OK to ping the computer.

Dim strComputer, strCommand As String

strComputer = Computer.Text

strCommand = "ping /t" & " " & strComputer

Shell("CMD /k" & strCommand, AppWinStyle.NormalFocus)

THERE YA GO! a CMD prompt should appear and start pinging the remote PC.
 
<pre>>Dim strComputer, strCommand As String
>
>strComputer = Computer.Text
>
>strCommand = "ping /t" & " " &
>strComputer
>
>Shell("CMD /k" & strCommand,
>AppWinStyle.NormalFocus)</pre>
Hiya kane,

where would I put the above code in a module on the form?? sorry very new to VB
 
R
> To answer my own question (with the help of TheDirector's answer above) a
> command button called Command1 with the following code attached...

>Private Sub Command1_Click()
>Dim sYourCommand As String
>sYourCommand = "C:\devcon DISable " &
>"""@HID\VID_05AC&PID_0304\6&306D5220&0&0000"""
>Shell "CMD /c" & sYourCommand
>End Sub
>
>... will disable the USB mouse whose Device Instance ID (from (XP) Device Manager

How do I pass a value from a combobox to a shell/dos command.. for example in VB I selected the c:\ drive and I want to pass that value into shell(the dos portion to get it to work).. how can I do that?
 
> Shell("calc.exe") to run calculator
> Shell("cmd.exe") to run command prompt

Shell() works but its so limited -

(1) run asynchronous and no way to set it to wait as in the VBScript Run function for example:
WshShell.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

(2) not supply ExitCode

(3) nor yet access to stdErr/stdIn/stdOut
In other words can't get as well the output from my command.

The Exec() function in VBScript allows this, but I can't find similar method in VB6 (not VB.Net), did someone have idea or pointer where to look?

Thanks
 
Top