Command Line Input

D

David A. Osborn

I am current writing a console program that takes two file paths as command
line input. I get the command line input using the following code:

parameter = Split(Command())
input = parameter(0)
output = parameter(1)

Unforunately this method does not handle embedded spaces in the file paths,
for example C:\Documents and Settings\Administrator. Is there a simple way
to handle this? From my command prompt experience I know that paths like
this are usually passed in quotes, but the method above still breaks it
apart at the spaces. Does vb.net have a built method to handle this or do I
need to do some sort of character checking loop that looks for the quotes?
 
C

Chris Dunaway

Your command line parameters with spaces must be enclosed inside
quotation marks.
 
C

Chris, Master of All Things Insignificant

The way I think this is to be handled is as follows

Sub Main(Args() as String)
input = Args(0)
output = Args(1)
End Sub

Make sure you test to see how many arguments are there first otherwise
you'll throw an exception.

Chris
 
H

Herfried K. Wagner [MVP]

David,

David A. Osborn said:
I am current writing a console program that takes two file paths as command
line input. I get the command line input using the following code:

parameter = Split(Command())
input = parameter(0)
output = parameter(1)

Unforunately this method does not handle embedded spaces in the file
paths, for example C:\Documents and Settings\Administrator. Is there a
simple way to handle this?

\\\
Public Module Program
Public Sub Main(ByVal Args() As String)
For Each Arg As String In Args
Console.WriteLine(Arg)
Next Arg
End Sub
End Module
///

In the project properties, select 'Sub Main' as startup object.
 
C

Cor Ligthert

David,

Why not tell to do it as we self do.

path1,path2

And split than on the comma?

Just my thought,

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Why not tell to do it as we self do.

path1,path2

And split than on the comma?

Do you know any other application that uses "," as a path separator? How
would that work with a command-line like 'Goo.exe /file:"C:\Bla Bla\Bla.txt"
/output:"C:\Foo Foo\Foo.txt"'. I think that "," would confuse the user...
:).
 
C

Cor Ligthert

Herfried,

Do you know any other application that uses "," as a path separator? How
would that work with a command-line like 'Goo.exe /file:"C:\Bla
Bla\Bla.txt" /output:"C:\Foo Foo\Foo.txt"'. I think that "," would
confuse the user... :).

You probably still use a horse to go from A to B, there are more
alternatives now, that is because somebody started to do it in another way.

:)

The alternative is someting as
/I:'Inputpath' /O:'Outputpath'

However how many endusers are still commandline users so a simple

command or command /? or command /h or any one that does not be conform the
needed, can give a help file what exactly describes how it should be done
and than will in my opinion almost everybody understand it when the text is
"Inputpath , (include comma) Outputpath"

Just my idea

Cor
 
J

Jay B. Harlow [MVP - Outlook]

David,
In addition to the other comments:

You can use Sub Main(Args() as String) as the other suggested, or you can
use Environment.GetCommandLineArgs if you need the parameters far removed
from the Main routine.

Alternatively you can use the CommandLineToArgvW win32 api, unfortunately I
do not have the Declare statement. I would however only resort to using
CommandLineToArgvW directly where I needed to parse a string being input by
the user into a "command line". Searching groups.google.com brings up a
number of possibilities for samples.

Hope this helps
Jay
 

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