Question Moving files with console application?

formlesstree4

Well-known member
Joined
Jul 17, 2008
Messages
61
Programming Experience
1-3
VB.NET:
Module Module1

    Sub Main(ByVal File, ByVal Location)

        If System.IO.File.Exists(File) = True Then
            System.IO.File.Move(File, Location)
            Console.WriteLine("File Moved")
        End If

    End Sub

End Module

Error:
VB.NET:
Error 1: No accessible 'Main' method with an appropriate signature was found in 'ConsoleApplication1.Module1'.

What's wrong?
 
To use the Main method for a program you need either a sub with no parameters or one that accepts a single string array (the var name can be whatever you want).

It also helps if you specify the scope of the method as well, Public's the recommended one.

These work:
VB.NET:
Module Module1

    Public Sub Main()

    End Sub

End Module

Module Module1

    Public Sub Main(ByVal Args() As String)

    End Sub

End Module
Now all you'd need to do is get the info from the either the Args() var or you can always use Environment.GetCommandLineArgs() to get the source and dest file info
 
Scratch what I had in the first post.
VB.NET:
Dim Args() As String = Command.Split(",")
        If Args(0) = Nothing Then
            Console.Write("File Copier 0.2 Syntax: copy.exe <old>:<new>")
            Exit Sub
        End If
        If Args(1) = Nothing Then
            Console.Write("File Copier 0.2 Syntax: copy.exe <old>:<new>")
        End If
        If System.IO.File.Exists(Args(0)) = True Then
            Try
            Catch ex As Exception
                System.IO.File.Copy(Args(0), Args(1), True)
                Console.Write("File Copied")
                Console.Write(ex)
            End Try
        End If

Why does this error! It's driving me crazy!!
 
Scratch what I had in the first post.
VB.NET:
Dim Args() As String = Command.Split(",")
        If Args(0) = Nothing Then
            Console.Write("File Copier 0.2 Syntax: copy.exe <old>:<new>")
            Exit Sub
        End If
        If Args(1) = Nothing Then
            Console.Write("File Copier 0.2 Syntax: copy.exe <old>:<new>")
        End If
        If System.IO.File.Exists(Args(0)) = True Then
            Try
            Catch ex As Exception
                System.IO.File.Copy(Args(0), Args(1), True)
                Console.Write("File Copied")
                Console.Write(ex)
            End Try
        End If

Why does this error! It's driving me crazy!!
Post the code for the entire module
 
For those interested:
VB.NET:
Option Explicit On
Option Strict On
Option Infer Off

Imports System.IO
Module Module1
    Private _FileMode As FileModes = FileModes.Invalid

    Private Enum FileModes
        Invalid = -1I
        Copy = 0I
        Delete = 1I
        Move = 2I
        Rename = 3I
    End Enum

    Public Sub Main()
        Dim Args() As String = Environment.GetCommandLineArgs
        If Args.Length > 2I Then
            Select Case Args(1I).Trim.ToLower.Replace("/", String.Empty).Replace("\", String.Empty).Replace("-", String.Empty)
                Case "d", "delete" : _FileMode = FileModes.Delete
                Case "c", "copy" : _FileMode = FileModes.Copy
                Case "m", "move" : _FileMode = FileModes.Move
                Case "r", "rename" : _FileMode = FileModes.Rename
                Case Else : _FileMode = FileModes.Invalid
            End Select
            Select Case _FileMode
                Case FileModes.Delete
                    File.Delete(Args(2I).Trim)
                    Call OutputMessage("File '" & Path.GetFileName(Args(2I).Trim) & "' deleted successfully")
                Case FileModes.Copy, FileModes.Move
                    If File.Exists(Args(2I).Trim) AndAlso Directory.Exists(Args(3I).Trim) Then
                        Select Case _FileMode
                            Case FileModes.Copy
                                File.Copy(Args(2I).Trim, Args(3I).Trim & Path.DirectorySeparatorChar & Path.GetFileName(Args(2I).Trim), True)
                                Call OutputMessage("File '" & Path.GetFileName(Args(2I).Trim) & "' copied successfully")
                            Case FileModes.Move
                                File.Move(Args(2I).Trim, Args(3I).Trim & Path.DirectorySeparatorChar & Path.GetFileName(Args(2I).Trim))
                                Call OutputMessage("File '" & Path.GetFileName(Args(2I).Trim) & "' moved successfully")
                        End Select
                    Else
                        Call OutputMessage("Something's wrong with the parameters")
                    End If
                Case FileModes.Rename
                    If File.Exists(Args(2I).Trim) Then
                        File.Move(Args(2I).Trim, Path.GetDirectoryName(Args(2I).Trim) & Path.DirectorySeparatorChar & Args(3I).Trim)
                        Call OutputMessage("File '" & Args(2I).Trim & "' renamed successfully")
                    Else
                        Call OutputMessage("Error with source file")
                    End If
                Case FileModes.Invalid : Call OutputMessage("Error with parameters")
            End Select
        Else
            Call OutputMessage("Not enough parameters")
        End If
    End Sub

    Private Sub OutputMessage(ByVal Msg As String)
        Console.WriteLine(Msg)
        Console.Read() 'Pause so user sees message
    End Sub

End Module
 
Back
Top