writing commands to command prompt in VB.NET

S

Shooter4Life8

Hi, I am having trouble figureing out the best way to open a command
prompt then write lines to it in VB.NET. Currently I have this code,
but it execute's too fast I think because

Dim psi As Object = New ProcessStartInfo
psi.FileName = "cmd.exe"
System.Diagnostics.Process.Start(psi)
SendKeys.Send("abcdefg" & "{enter}")

SendKeys.Send("12345678" & "{enter}")

SendKeys.Send("!@#$%^^&" & "{enter}")

The SendKeys.Send()s seem to be executing so fast that the command
prompt only catches say efg 78 and & in the commmand prompt. And
another thing it seems to do is execute this code twice once for the
down mouse click and one for the up maybe? The effect of the program
running its code twice varies with how long you hold the button down. I
think i need some sort of delays added in but I don't know the function
to do that. And I have no clue how to fix the code running twice. Any
help reguarding any of these issues is much appreciated!

Thank you very much!
 
S

Shooter4Life8

I figured out how to avoid two coming up by using the Mousedown event
rather than the click event. Although now all of the code is executed
before the command prompt is window is created.

How do you add delays in VB.NET aside from loops?
 
R

rowe_newsgroups

So what exactly are you trying to accomplish? Maybe there's an easier
way...

Thanks,

Seth Rowe
 
M

Miro

Is this what ur looking for?

System.Threading.Thread.Sleep(30000) ' 30 seconds 1000 for every second.

u need a pause in between your commands ?

Miro
 
C

Chris Dunaway

Hi, I am having trouble figureing out the best way to open a command
prompt then write lines to it in VB.NET. Currently I have this code,
but it execute's too fast I think because

Dim psi As Object = New ProcessStartInfo
psi.FileName = "cmd.exe"
System.Diagnostics.Process.Start(psi)
SendKeys.Send("abcdefg" & "{enter}")

SendKeys.Send("12345678" & "{enter}")

SendKeys.Send("!@#$%^^&" & "{enter}")

Is abcdefg and 12345678 and the other command batch files that you are
trying to run? If so, look at starting the cmd.exe and passing in the
name of the batch file at the argument. Like this: This code will
start a batch file and waits for it to exit

Private Sub ExecuteBatFile(ByVal batchfilename As String)
Using m_Process As New Process()
With m_Process.StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.CreateNoWindow = True
.Arguments = "/C " & batchfilename"
End With

m_Process.Start()
m_Process.WaitForExit(5000)
End Using
End Sub


Hope this helps
 
S

Shooter4Life8

I was trying to avoid using a batch file just due to some of my feilds
having sensitive information. That isn't the case anymore, but still
i'd like to pull this off just using a standard form and pulling up a
command prompt and doin what I need to get done.

I fixed my problems up there, but have run into a far stranger one.

Now im trying to use this statment to copy a couple files from one
directory to another through dos with VB.NET.

copy \\(server name goes here)\(file name goes here)\ ut* c:\data

the ut* is asking for all files with the prefix ut (if you didn't know
that)

when I send this command to the command prompt using the following
statment

SendKeys.Send(strPath & "{enter}")

where strPath = "copy \\(servername goes here)\(file name goes
here)\ut" & ChrW(42) & " c:\data"

the only thing that shows up in the command prompt is * c:\data

Ive tried it several different ways to varying effect... not sure what
is causing the front part to be cut off.

Thankyou sooo much for your help :)
 
C

Chris Dunaway

I was trying to avoid using a batch file just due to some of my feilds
having sensitive information. That isn't the case anymore, but still
i'd like to pull this off just using a standard form and pulling up a
command prompt and doin what I need to get done.

My example showed how to call a batch file, but if you start cmd.exe as
a process, you can then use the redirected StandardInput to send
commands to the command processor:

Public Sub ExecuteDOSCommand()
Using m_Process As New Process()
With m_Process.StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
End With

Dim start As DateTime = DateTime.Now

m_Process.Start()
m_Process.StandardInput.WriteLine("copy /Y
\\server\folder\ut* c:\data")

m_Process.Close()
End Using
End Sub
Now im trying to use this statment to copy a couple files from one
directory to another through dos with VB.NET.

copy \\(server name goes here)\(file name goes here)\ ut* c:\data

the ut* is asking for all files with the prefix ut (if you didn't know
that)

The copy command would be copy \\servername\ut* c:\data

You have a space just before the ut*.

But why on earth would you want to use the command prompt to copy the
files when you can use the classes in the System.IO namespace?

Imports System.IO

Public Sub CopyFiles()
Dim destination As String = "c:\data"
Dim filesToCopy() As String =
Directory.GetFiles("\\servername\folder", "ut*")

For Each filename As String In filesToCopy
File.Copy(filename, Path.Combine(destination,
Path.GetFileName(filename)))
Next
End Sub

You'll probably want to add some exception handling to handle any
errors.
when I send this command to the command prompt using the following
statment

SendKeys.Send(strPath & "{enter}")

where strPath = "copy \\(servername goes here)\(file name goes
here)\ut" & ChrW(42) & " c:\data"
the only thing that shows up in the command prompt is * c:\data

I cannot answer this as I do not use SendKeys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top