Solved

vb.net shell problem

Posted on 2009-05-15
14
662 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

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

Expert Comment

by:Seblue
Comment Utility
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
Comment Utility
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
Comment Utility
well 'command prompt' in that shell opens up a command prompt type window
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

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

Author Comment

by:sgaggerj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now