• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 711
  • Last Modified:

vb.net shell problem

Hi experts,

i'm using shell to execute a command line command

the code i'm using is below

if i run the code below i see a quickly closing shell window, and i can tell that mysql was run with incorrect parameters. (the mysql help/usage text scrolls before closing)

if i break on Shell(cmd,.....) and do
?cmd
in the immediate window i get a string
""C:\Program Files\MySql\MySQL Server 5.1\bin\mysql.exe" -uMyUser -pMyPass -h192.268.1.105 MyDB < "r:\visual studio 2008\projects\MyApp\sql\drop_system.sql""

if i copy the string that is inside the outer "", paste that in a command prompt it runs fine.

what am I missing?


Private Const MYSQL_APP as string = "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"
Dim dir as DirectoryInfo = new DirectoryInfo("r:\visual studio 2008\projects\MyApp\sql\"
dim File as FileInfo=dir.getFiles("*.sql")(0)
 
 
Dim cmd As String = """" & MYSQL_APP & """" & " -uMyUser -pMyPass -h192.168.1.105 MyDB < " & """" & file.DirectoryName & "\drop_system.sql" & """"
Shell(cmd, AppWinStyle.NormalFocus, True)

Open in new window

0
sgaggerj
Asked:
sgaggerj
  • 9
  • 5
1 Solution
 
SeblueCommented:
Hi sgaggerj,

It does not really answer your question, but personaly, I use the class Process to start a external program. The "Process" class has many functionnalities, very useful. You may try something like:

Process.Start(MYSQL_APP," -uMyUser -pMyPass -h192.168.1.105 MyDB < " & """" & file.DirectoryName & "\drop_system.sql" & """")

You are also able to monitor it, checking the result code, and many things:
Dim P As Process = Process.Start(...,...)
P.WaitForExit

or even define other credentials.

The StartProcess function has many overloads. Have a look at it.

Hope this helps.
Seblue.
0
 
sgaggerjAuthor Commented:
i had tried that method also, and it seems to do the same thing, open a window, scroll the text and close.
0
 
sgaggerjAuthor Commented:
I tried again with the method you suggested Seblue, and was able to pause the output of the window.
It doesn't seem like the arguments are getting passed for some reason

Here's the output I was able to pause:

C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe  Ver 14.14 Distrib 5.1.31,
 for Win32 (ia32)
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Usage: C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe [OPTIONS] [database
]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
  -A, --no-auto-rehash
                      No automatic rehashing. One has to use 'rehash' to get
                      table and field completion. This gives a quicker start of
                      mysql and disables rehashing on reconnect. WARNING:
                      options deprecated; use --disable-auto-rehash instead.
  -B, --batch         Don't
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
sgaggerjAuthor Commented:
i paused the output of the shell as well and come up with the same thing.
0
 
SeblueCommented:
sgaggerj,

Unfortunately, I am not able to have a try with SQL. But you may try another thing. You said it is running with the command prompt. Then try to run a prompt with your full shell command like this:

Shell("cmd","/c " & cmd)

This will run your shell command through the cmd.exe of the system. (That's just an idea for now).

Seblue.
0
 
sgaggerjAuthor Commented:
ran your code as is (using my creation of 'cmd' ) and received the following error

An error occurred creating the form. See Exception.InnerException for details.  The error is: Conversion from string "/c "C:\Program Files\MySQL\MySQL" to type 'Short' is not valid.
0
 
sgaggerjAuthor Commented:
well 'command prompt' in that shell opens up a command prompt type window
0
 
SeblueCommented:
That's right. CMD.EXE may not support parameters having filenames with space. Apologies.
0
 
sgaggerjAuthor Commented:
thats why i coded to have " " around the long file names.
i can re-try it with dos 8.1 names
0
 
sgaggerjAuthor Commented:
Error: "An error occurred creating the form. See Exception.InnerException for details.  The error is: Conversion from string "/c C:\Progra~1\MySQL\MySQLS~1.1\" to type 'Short' is not valid."
0
 
sgaggerjAuthor Commented:
apparently it didn't like the '<' in the argument list.
i created a batch file

@echo off
set logfile=log.log
echo arguments > %logfile%
echo %1 >> %logfile%
echo %2 >> %logfile%
echo %3 >> %logfile%
echo %4 >> %logfile%
echo %5 >> %logfile%

cls
mysql -u%1 -p%2 -h%3 %4 < %5
pause
exit

and it still didn't work.

i then realized there were actually 6 arguments getting passed (the '<' was one)
and i modified the batch file to

@echo off
set logfile=log.log
echo arguments > %logfile%
echo %1 >> %logfile%
echo %2 >> %logfile%
echo %3 >> %logfile%
echo %4 >> %logfile%
echo %6 >> %logfile%

cls
mysql -u%1 -p%2 -h%3 %4 < %6
pause
exit

it still didn't like it.

so i reverted back to the original version of the batch file, removed the '<' from the cmd string and it works, however there's a problem getting to the mysql server so that's another issue entirely.

is there an escape character that can be used in conjunction with the '<' so that dos doesn't think it's some pipe of sorts?
0
 
SeblueCommented:
I tried :

Shell("cmd.exe /c more < """"c:\sometext.txt""")

That works. So I am not sure the pb comes from the "<".
0
 
sgaggerjAuthor Commented:
i know it worked (the full string) if i went to the command prompt and used long file names with quotes.
it didn't work with a shell() or Process though.

i tried it with a batch file and the < was causing problems. (at least in my implementation).
without the < in the string, it's working fine via a batch file, which is not 100% the way i wanted to do it, but it seems to get the job done.
0
 
SeblueCommented:
Last comment. Remember when I asked you to tried Shell("cmd","/c " & cmd)? I mixed up both shell and process.start. My mistake. I think Shell("cmd /c " & cmd) would do the job.

I had some readings regarding the "<" and its seems that this char is only used correctly with cmd.exe.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now