Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / Visual Basic

Synchro

Rate me:
Please Sign up or sign in to vote.
4.43/5 (13 votes)
20 May 2009GPL35 min read 74.3K   1K   75  
A control to synchronize folder contents.
Imports System.Resources

Module Start

    ' Global declarations
    Friend frmSynchro As frmMain                ' The main form
    Friend Const conRegistryBaseSubKey As String = "Software\E.Marcon\Synchro"  ' Registry keys (HKCU and HKLM) in which all values are written.
    Friend cmdArguments() As String             ' Array of the command line arguments
    Public ErrorStatus As Byte                  ' The status the application returns when it leaves

    ' Declare a Resource Manager instance
    Friend LocRM As New ResourceManager("Synchro.WinFormStrings", GetType(frmMain).Assembly)

    ' Declare the command line options object
    Friend CommandLineArgs As CommandLine.Utility.Arguments

    ' Variables
    Friend gintDocNb As Integer                 ' Number for the default document name
    Friend bytRecentFileListLength As Byte = 4  ' Number of recorded files in the MRU list (File menu)
    Friend intRefreshFrequency As Integer = 200 ' Frequency of the timer used to display the status (ms)

    Friend Function Main(ByVal CmdArgs() As String) As Integer
        ' Start the application, read the command line arguments

        ' Debug purpose only: set the culture to invariant
        'System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture
        'System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture

        Dim blnOptionsOK As Boolean = True
        Dim blnConsoleMode As Boolean

        ' Initialize variables
        ErrorStatus = 0

        ' Read the command line arguments
        cmdArguments = CmdArgs
        ' And interpret it
        If cmdArguments.GetLength(0) > 0 Then
            ' If arguments were sent
            CommandLineArgs = New CommandLine.Utility.Arguments(cmdArguments)
        End If

        ' Choose between form and console application
        If Not (CommandLineArgs Is Nothing) Then
            If Not (CommandLineArgs.Item("go") Is Nothing And CommandLineArgs.Item("help") Is Nothing) Then
                ' Console mode only if option /go
                blnConsoleMode = True
            End If
        End If

        ' Run the application
        If blnConsoleMode Then
            ' No form will be opened. Create a standalone synchro control.
            Dim CtlSynchro1 As New ctlSynchro
            CtlSynchro1.blnConsoleMode = True

            ' Eventually read the file
            If CommandLineArgs.Item("help") Is Nothing Then
                ' No /? in the options
                Dim strFile As String = cmdArguments(0)
                If (strFile.Chars(0) <> "-") And (strFile.Chars(0) <> "/") Then
                    ' The first argument is not an option, so it is a file name. Open it.
                    Dim sr As System.IO.StreamReader
                    Try
                        ' Open the file
                        sr = System.IO.File.OpenText(strFile)
                        ' Read the source, the target and the options
                        CtlSynchro1.strSource = sr.ReadLine()
                        CtlSynchro1.strTarget = sr.ReadLine()
                        CtlSynchro1.synOptions = CType(sr.ReadLine(), SyncOptions)
                        CtlSynchro1.strLogFile = sr.ReadLine()
                        CtlSynchro1.strExcludedStrings = sr.ReadLine()
                        sr.Close()
                    Catch
                        blnOptionsOK = False
                    Finally
                        ' Close the file
                        If Not sr Is Nothing Then
                            sr.Close()
                        End If
                    End Try
                End If
            Else
                ' Help requested /help
                blnOptionsOK = False
            End If

            ' Continue with the options. They may change those from the file
            If blnOptionsOK Then
                ' Read options
                If Not (CommandLineArgs.Item("Source") Is Nothing) Then
                    CtlSynchro1.strSource = CommandLineArgs.Item("Source")
                End If
                If Not (CommandLineArgs.Item("Target") Is Nothing) Then
                    CtlSynchro1.strTarget = CommandLineArgs.Item("Target")
                End If
                If Not (CommandLineArgs.Item("s") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncCopySubDirectories
                End If
                If Not (CommandLineArgs.Item("e") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncCopyEmptySubDirectories
                End If
                If Not (CommandLineArgs.Item("r") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncOverWriteReadOnly
                End If
                If Not (CommandLineArgs.Item("d") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncPreserveNewerFiles
                End If
                If Not (CommandLineArgs.Item("u") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncCopyOnlyExistingFiles
                End If
                If Not (CommandLineArgs.Item("z") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncSynchronize
                End If
                If Not (CommandLineArgs.Item("n") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncDontPromptBeforeCreatingFiles
                End If
                If Not (CommandLineArgs.Item("y") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncDontPromptBeforeDeletingFiles
                End If
                If Not (CommandLineArgs.Item("l") Is Nothing) Then
                    CtlSynchro1.synOptions = CtlSynchro1.synOptions Or SyncOptions.SyncLog
                    CtlSynchro1.strLogFile = CommandLineArgs.Item("l")
                End If
                If Not (CommandLineArgs.Item("x") Is Nothing) Then
                    CtlSynchro1.strExcludedStrings = CommandLineArgs.Item("x")
                End If

                ' Run. Single thread.
                CtlSynchro1.SyncStart()
                ' Get the exit status
                Select Case CtlSynchro1.ExitStatus
                    Case SyncStatus.SyncSucceeded
                        ErrorStatus = 0
                    Case SyncStatus.SyncNothingToDo
                        ErrorStatus = 1
                    Case SyncStatus.SyncFailed
                        ErrorStatus = 2
                End Select

            Else
                ' Show the syntax
                Console.WriteLine(LocRM.GetString("strSyntaxTitle") & vbCrLf & LocRM.GetString("strSyntaxDetail") _
                & vbCrLf & vbCrLf & LocRM.GetString("strSyntaxExample"))
            End If


        Else
            ' Windows form mode. Open the main window
            frmSynchro = New frmMain
            Application.Run(frmSynchro)

        End If

        ' Return the error status. May not be 0 if run in console mode.
        Return ErrorStatus
    End Function


End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Engineer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions