ElektroStudios
ElektroStudios

Reputation: 20464

VB.NET - How to split a string?

In my form I store/create a commandline arguments inside a var to use it later with a control button, to launch a external app with that arguments stored inside my var, and the var contents is a tring like this:

ExternalAPP.exe "Argument 1" "argument 2" "argument 3" "more arguments"

PS: The arguments are references to folder paths.

And what i need to do is split the var to get all the folder paths (arguments), and make something like this:

' Pseudocode
For each folder in var.split(ControlChars.Quote)
    for each file in get.folder.files
     appendline.(new_text_file, file.Name)
  Next

Thanks if somebody helps me

UPDATE: the content of the VAR that i need to split is exactly this:

"F:-=Temporal=-\Documentos\DVD Temazos\Rock" "F:-=Temporal=-\Documentos\DVD Temazos\Techno" "F:-=Temporal=-\Documentos\DVD Temazos\Pop"

*NOTICE THAT THE FORUM DOES NOT PASTED THE SLASH \ AFTER DRIVE LETTER F:*

Upvotes: 0

Views: 1391

Answers (4)

NeverHopeless
NeverHopeless

Reputation: 11233

You should try something like this:

    Dim var As String = "Command.exe ""F:-=Temporal=-\Documentos\DVD Temazos\Rock"" ""F:-=Temporal=-\Documentos\DVD Temazos\Techno"" ""F:-=Temporal=-\Documentos\DVD Temazos\Pop"""
    var = var.Replace(":", ":\").Remove(0, var.IndexOf(" ") + 2)
    Dim dirs As New List(Of String)(var.Split(New String() {""" """}, StringSplitOptions.RemoveEmptyEntries))
    Dim files As New List(Of String)()

    For Each path As String In dirs
        Try
            If (Directory.Exists(path)) Then
                files.AddRange(Directory.GetFiles(path))
            Else
                Debug.Print(path & "path does not exists") ' check "Output Window"
            End If
        Catch ex As Exception
            Debug.Print(path & ": exception :" & ex.Message)  ' check "Output Window"
        End Try
    Next

EDIT:

I just noticed this in your question:

NOTICE THAT THE FORUM DOES NOT PASTED THE SLASH \ AFTER DRIVE LETTER F:

Upvotes: 1

Derek Tomes
Derek Tomes

Reputation: 4007

If what you're trying to accomplish is parsing command line arguments, try this:

Dim files() As String = System.Environment.GetCommandLineArgs()

http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx

This line:

Var = Command.exe "Argument 1" "argument 2" "argument 3" "more arguments"

Might not do what you want. The double quote character needs to enclose the entire string and be escaped:

Var = "Command.exe ""Argument 1"" ""argument 2"" ""argument 3"" ""more arguments"""

Upvotes: 1

SAJ14SAJ
SAJ14SAJ

Reputation: 1708

Your sample code doesn't make a lot of sense. Is it supposed to be a command line, or VB source code?

However, it seems that you want a regular expression to find each string inside quotes.

Regular expressions are tedious to build correctly. I recommend building a simple regular expression validator, or using one of the web based ones.

Your expression will be something LIKE this (I am not going to validate this):

.*(\"(?<filepath>[^"])\")*.*

You will want to get all of the match group values for filepath.

If you control your program input, making a friendlier syntax that is easier to parse seems appropriate.

IF these are command line parameters, the commmand shell will parse each quoted parameter for you, and the arguments to your main routine will have them.

I am a C# guy, so a quick google gave me this:

Sub Main()

  Dim s() As String = System.Environment.GetCommandLineArgs()

  Console.WriteLine(s(1))

End Sub

Upvotes: 1

codingbiz
codingbiz

Reputation: 26376

Try this

dim Var As String = Command.exe "Argument 1" "argument 2" "argument 3" "more arguments"

dim entries = Var.Split(" "c)

For i As Integer = 1 To entries.Length - 1
   dim files As String() = Directory.GetFiles(entries(i))
Next

Upvotes: 1

Related Questions