2

For some reason it seems that ampersands are not working like they should when I attempt to use them in shell commands in VB. When I attempt to link two commands together on the same line using an ampersand i receive the error: "filenotfoundexception was unhandled file not found"

The command I am trying to run is:

 Shell("cd " & TextBox2.Text.ToString & " & adb -s " & TextBox15.Text.ToString & " shell monkey -p " & TextBox1.Text.ToString & " -v 1", AppWinStyle.Hide) 

I tried breaking it down to a more simplistic form, but im still receiving the error:

 Shell("cd C:\ & adb shell monkey -p com.android.system -v 1", AppWinStyle.Hide)

If I get rid of the ampersand and just use:

 shell(adb shell monkey -p com.android.system -v 1", AppWinStyle.Hide)

everything works just fine. Are ampersands not available in vb shell commands?


*My edit

Actually I am still having trouble. So what i have is:

    psi.WorkingDirectory = TextBox2.Text.ToString
    psi.FileName = "adb"
    psi.WindowStyle = ProcessWindowStyle.Hidden

then I have a little bit of code, and then I assign an argument and execute the argument:

    psi.Arguments = "-s " & TextBox15.Text.ToString & " shell monkey -p " & TextBox1.Text.ToString & " -v  1"
    Process.Start(psi)

then I have a little bit of code, and then I try running the process again with a different argument:

    psi.Arguments = "-s " & TextBox15.Text.ToString & " shell input keyevent 3"
    Process.Start(psi)

First one seems to work, all the subsequent ones do not. Is there any reason why this shouldnt work? is there a process refresh or something that I am missing?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Nefariis
  • 3,451
  • 10
  • 34
  • 52

3 Answers3

4

Deleted my other answer, found a simpler way to do this.

This is what you want...

Shell("cmd.exe /c cd C:\ & adb shell monkey -p com.android.system -v 1", AppWinStyle.Hide)

Inserting it into your original code...

Shell("cmd.exe /c cd " & TextBox2.Text.ToString & " & adb -s " & TextBox15.Text.ToString & " shell monkey -p " & TextBox1.Text.ToString & " -v 1", AppWinStyle.Hide) 

I tested the first example and it seemed to work.

JohnFx
  • 34,542
  • 18
  • 104
  • 162
3

Have you considered using the Process object to start ADB with the CommandLine options being set

Dim psi As New ProcessStartInfo

psi.WorkingDirectory = "c:\"
psi.Arguments = "shell monkey -p com.android.system -v 1"
psi.FileName = "ADB"
psi.WindowStyle = ProcessWindowStyle.Hidden
return Process.Start(psi)

in the event that your ADB program only allows a single instance to run, maybe you need to add the following

Dim ps As Process = Process.Start(psi)
ps.WaitForExit()

psi.Arguments = 'new arguments
Process.Start(psi)
Paul Farry
  • 4,730
  • 2
  • 35
  • 61
  • actually, can you look at the edit i posted above. Im still having some trouble – Nefariis Jan 10 '12 at 00:02
  • @Nefarii maybe your ADB only allows a single instance to run, and if you don't wait between, the subsequent instance just open and immediately close.. See edit – Paul Farry Jan 10 '12 at 03:31
2

The Shell command expects a file name, so command line extensions won't work.

There are a couple of options:

1) Start cmd.exe with process.start and pass the parameters (I have not tested this, so am unsure if it will work.

2) Create your commands in a .cmd or .bat file and then shell that file (this seems like it might be the easiest approach).

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • yup, that was the first thing I tried. Its weird, '&' and '&&' both work in cmd.exe and from the Microsoft site it says that it should work in shell. But when I try either in VS2010, i get the error message. – Nefariis Jan 09 '12 at 22:11
  • I was running a batch file previous to this. It just seemed taxing to constantly create and delete a unique batch file for each command the needs to be run ... ill look into process.start – Nefariis Jan 09 '12 at 22:30