hey i'm trying to make console app with basic commands like copy,move the problem is that i cant get my code right
the command suppose to be like this:

= copy to
file directory>the copied file
im trying to split the : file directory, > , the copied file
to string or perhaps if there is an easier way to do so
thanks for the help:)

Dani AI

Generated

and opened the right lines of thought. For a console that accepts commands like copy <source> > <dest> a robust parser is preferable to a blind space split: it must accept quoted paths, ignore > inside quotes, and treat a destination that is a directory differently from a file path. The snippet below implements a small scanner to find the first > that is not inside quotes, trims surrounding quotes, validates the source, and performs File.Copy / File.Move with basic error handling.

Imports System.IO

Module CommandConsole
    Sub ExecuteCommand(input As String)
        input = input.Trim()
        If input = "" Then
            Console.WriteLine("No command provided.")
            Return
        End If

        Dim firstSpace As Integer = input.IndexOf(" "c)
        Dim cmd As String
        Dim rest As String
        If firstSpace = -1 Then
            cmd = input.ToLowerInvariant()
            rest = ""
        Else
            cmd = input.Substring(0, firstSpace).ToLowerInvariant()
            rest = input.Substring(firstSpace + 1).Trim()
        End If

        Dim inQuotes As Boolean = False
        Dim sep As Integer = -1
        For i As Integer = 0 To rest.Length - 1
            Dim ch As Char = rest(i)
            If ch = """"c Then inQuotes = Not inQuotes
            If ch = ">"c AndAlso Not inQuotes Then
                sep = i
                Exit For
            End If
        Next

        If sep = -1 Then
            Console.WriteLine("Invalid syntax: missing '>' separator.")
            Return
        End If

        Dim src As String = TrimQuotes(rest.Substring(0, sep).Trim())
        Dim dest As String = TrimQuotes(rest.Substring(sep + 1).Trim())

        If Directory.Exists(dest) OrElse dest.EndsWith(Path.DirectorySeparatorChar.ToString()) OrElse dest.EndsWith(Path.AltDirectorySeparatorChar.ToString()) Then
            dest = Path.Combine(dest, Path.GetFileName(src))
        End If

        Select Case cmd
            Case "copy"
                If Not File.Exists(src) Then
                    Console.WriteLine("Source file not found: " & src)
                    Return
                End If
                Try
                    File.Copy(src, dest, True)
                    Console.WriteLine("Copied: " & dest)
                Catch ex As Exception
                    Console.WriteLine("Copy failed: " & ex.Message)
                End Try
            Case "move"
                If Not File.Exists(src) Then
                    Console.WriteLine("Source file not found: " & src)
                    Return
                End If
                Try
                    File.Move(src, dest)
                    Console.WriteLine("Moved: " & dest)
                Catch ex As Exception
                    Console.WriteLine("Move failed: " & ex.Message)
                End Try
            Case Else
                Console.WriteLine("Unknown command: " & cmd)
        End Select
    End Sub

    Function TrimQuotes(s As String) As String
        If s.Length >= 2 AndAlso s(0) = """"c AndAlso s(s.Length - 1) = """"c Then
            Return s.Substring(1, s.Length - 2)
        End If
        Return s
    End Function
End Module

Notes and quick checks: handle permissions and long paths (UnauthorizedAccessException, PathTooLongException), treat destinations that are directories by appending the source filename, and test these cases:

  • unquoted paths with spaces (should fail unless quoted),
  • quoted paths containing spaces,
  • destination as an existing directory,
  • attempts to overwrite (current code overwrites for copy; change the boolean in File.Copy if needed).
    This approach keeps parsing deterministic and avoids surprises when paths contain spaces or > characters inside quotes.

Recommended Answers

All 3 Replies

So basically, you read a console command and need to parse it?
The command read, should be in some string and you can use the split function to obtain an array of strings between the designed separators.

IE:
copy ttt.txt>yyy.ddd

You read this into the inputCommandString.

Tehn you can obtain the order and the parameters usint the split function:

dim orderAndParameters as string() = inputCommandString.Split(" "c)

Then you can verify the order and parameters by using a select - case structure:

select orderAnParameters.Length
case 0 ' no order nor parameters
 ' send a message? or ignore it?
case 1 ' only an order
 ' verify if the order is one of the possible orders without parameters or show the order usage in case of wrong number of parameters
 case 2 ' the order and one parameter
  .
  .
  .
 case else ' not defined.
 end select

Also a regex can help to analyze if the command and parameters structure is right.

Hope this helps

Thank You for you'r help:)
I found another way to do so by using the command split.

Please, be so kind to share your howto here, and then mark the thread as solved.
Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.