Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net shell problem

Posted on 2009-05-15
14
Medium Priority
?
690 Views
Last Modified: 2012-05-07
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
Comment
Question by:sgaggerj
  • 9
  • 5
14 Comments
 
LVL 4

Accepted Solution

by:
Seblue earned 2000 total points
ID: 24397721
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24397817
i had tried that method also, and it seems to do the same thing, open a window, scroll the text and close.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24397873
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sgaggerj
ID: 24397946
i paused the output of the shell as well and come up with the same thing.
0
 
LVL 4

Expert Comment

by:Seblue
ID: 24397973
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398043
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398048
well 'command prompt' in that shell opens up a command prompt type window
0
 
LVL 4

Expert Comment

by:Seblue
ID: 24398084
That's right. CMD.EXE may not support parameters having filenames with space. Apologies.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398092
thats why i coded to have " " around the long file names.
i can re-try it with dos 8.1 names
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398169
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398328
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
 
LVL 4

Expert Comment

by:Seblue
ID: 24398795
I tried :

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

That works. So I am not sure the pb comes from the "<".
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24399040
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
 
LVL 4

Expert Comment

by:Seblue
ID: 24399399
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question