Search This Blog

Most Viewed

Monday, November 21, 2011

Fastest Folder Copy Vb.Net









  For making a fastest copier window 7 comes with robocopy, stands for the robust copy, is very fast copier than the other one. But the main problem with the robocopy is that it only works with command line interface.
  Here is solution of this epic problem. We are here with a full GUI robocopier. All you have to do is just follow the below procedure and make your own folder copier.

1. Make a windows form Project in vb.net 2010 and name it "Folder Copy"
2. Change the name of form from "form1.vb" to "FolderCopy.vb"
3. Add Two TextBoxes named as "SourceText" & "DestText"
4. Add a Button and Name it as "CopyBtn"
5. In the coding window make a function as below, for checking whether another copy is running or not.

    Public Function IsProcessRunning(ByVal name As String) As Boolean
        For Each cls As Process In Process.GetProcesses()
            If cls.ProcessName.StartsWith(name) Then
                Return True
            End If
        Next
        Return False
    End Function

6. Add a subroutine for disable and enable the application textboxes and buttons so that no one can change any thing in b/w by mistake.



    Public Sub enable_me(ByVal stat As Boolean)
        If stat = True Then
            CopyBtn.Text = "Copy"
            CopyBtn.Enabled = True
            SourceText.Enabled = True
            DestText.Enabled = True
        Else
            CopyBtn.Text = "Copying"
            CopyBtn.Enabled = False
            SourceText.Enabled = False
            DestText.Enabled = False
        End If
    End Sub


7. Make a global string variable to hold the current path of application.


    Dim own_path As String = Application.StartupPath


8. In the CopyBtn_Click event type the following code:



  Private Sub CopyBtn_Click(ByVal sender As System.Object,
                                            ByVal e As System.EventArgs) Handles CopyBtn.Click


        Dim robo As StreamWriter
        Dim robo_text As String = vbNullString




        robo = New StreamWriter(own_path + "\robo.bat")


        robo_text = "@echo off" +
                    vbNewLine +
                    "robocopy /e /R:1 /W:1 " +
                    SourceText.Text + " " +
                    DestText.Text +
                    vbNewLine


        robo.Write(robo_text)
        robo.Close()


        'Shell(own_path + "\robo.bat", AppWinStyle.NormalNoFocus)


        Process.Start(own_path + "\robo.bat", AppWinStyle.MinimizedNoFocus)
        System.Threading.Thread.Sleep(1000)




        While (IsProcessRunning("Robocopy") = True)
            Application.DoEvents()
            enable_me(False)
        End While
        enable_me(True)


    End Sub


9. In the Form Closing event type the following code:



    Private Sub FolderCopy_FormClosing(ByVal sender As Object,
                     ByVal e As System.Windows.Forms.FormClosingEventArgs)
                              Handles Me.FormClosing
       
         Dim del As StreamWriter


        If File.Exists(own_path + "\robo.bat") Then


            del = New StreamWriter(own_path + "\d.bat")


            del.Write("@echo off" + vbNewLine +
                            "del robo.bat" + vbNewLine +
                             "del d.bat")
            del.Close()
            'Process.Start(own_path + "\d.bat", AppWinStyle.MaximizedFocus)
            Shell(own_path + "\d.bat", AppWinStyle.MaximizedFocus)
        End If
        MsgBox("Thank you For Using the Folder Copy" + vbNewLine + "Pushpkant Yadav")
    End Sub


10. In form load event type the following code:



    Private Sub FolderCopy_Load(ByVal sender As System.Object,
                  ByVal e As System.EventArgs) Handles MyBase.Load
        DestText.Text = Application.StartupPath
       
        While (IsProcessRunning("Robocopy") = True)
            Application.DoEvents()
            enable_me(False)
        End While
        enable_me(True)


    End Sub


that is all, now run your application, copy the path of folder of source and destination with double quotes and press copy button the application will make a batch file and copy all the files from source folder to destination folder. Remember robocopy can't copy individual files.  So never try to copy single files without the folder.


if all the above work has been done correctly the final code will look like:







'copy all files from one folder to another




Imports System.IO


Public Class FolderCopy


    Dim own_path As String = Application.StartupPath


    Private Sub CopyBtn_Click(ByVal sender As System.Object,
                              ByVal e As System.EventArgs) Handles CopyBtn.Click


        Dim robo As StreamWriter
        Dim robo_text As String = vbNullString




        robo = New StreamWriter(own_path + "\robo.bat")


        robo_text = "@echo off" +
                    vbNewLine +
                    "robocopy /e /R:1 /W:1 " +
                    SourceText.Text + " " +
                    DestText.Text +
                    vbNewLine


        robo.Write(robo_text)
        robo.Close()


        'Shell(own_path + "\robo.bat", AppWinStyle.NormalNoFocus)


        Process.Start(own_path + "\robo.bat", AppWinStyle.MinimizedNoFocus)
        System.Threading.Thread.Sleep(1000)




        While (IsProcessRunning("Robocopy") = True)
            Application.DoEvents()
            enable_me(False)
        End While
        enable_me(True)


    End Sub




    Private Sub FolderCopy_FormClosing(ByVal sender As Object,
                                       ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim del As StreamWriter


        If File.Exists(own_path + "\robo.bat") Then


            del = New StreamWriter(own_path + "\d.bat")


            del.Write("@echo off" + vbNewLine +
                            "del robo.bat" + vbNewLine +
                             "del d.bat")
            del.Close()
            'Process.Start(own_path + "\d.bat", AppWinStyle.MaximizedFocus)
            Shell(own_path + "\d.bat", AppWinStyle.MaximizedFocus)
        End If
        MsgBox("Thank you For Using the Folder Copy" + vbNewLine + "Pushpkant Yadav")
    End Sub






    Private Sub FolderCopy_Load(ByVal sender As System.Object,
                                ByVal e As System.EventArgs) Handles MyBase.Load
        DestText.Text = Application.StartupPath
        'DestText.Text = "d:\pankaj"


        While (IsProcessRunning("Robocopy") = True)
            Application.DoEvents()
            enable_me(False)
        End While
        enable_me(True)


    End Sub


    Public Sub enable_me(ByVal stat As Boolean)
        If stat = True Then
            CopyBtn.Text = "Copy"
            CopyBtn.Enabled = True
            SourceText.Enabled = True
            DestText.Enabled = True
        Else
            CopyBtn.Text = "Copying"
            CopyBtn.Enabled = False
            SourceText.Enabled = False
            DestText.Enabled = False
        End If
    End Sub


    Public Function IsProcessRunning(ByVal name As String) As Boolean
        For Each cls As Process In Process.GetProcesses()
            If cls.ProcessName.StartsWith(name) Then
                Return True
            End If
        Next
        Return False
    End Function


End Class


and the designer class will like:





<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class FolderCopy
    Inherits System.Windows.Forms.Form


    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub


    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer


    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SourceText = New System.Windows.Forms.TextBox()
        Me.DestText = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.CopyBtn = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'SourceText
        '
        Me.SourceText.Location = New System.Drawing.Point(70, 12)
        Me.SourceText.Name = "SourceText"
        Me.SourceText.Size = New System.Drawing.Size(202, 20)
        Me.SourceText.TabIndex = 0
        Me.SourceText.Text = """I:\music\songs other0"""
        '
        'DestText
        '
        Me.DestText.Location = New System.Drawing.Point(70, 38)
        Me.DestText.Name = "DestText"
        Me.DestText.Size = New System.Drawing.Size(202, 20)
        Me.DestText.TabIndex = 1
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(12, 19)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(41, 13)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Source"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(12, 41)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(60, 13)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Destination"
        '
        'CopyBtn
        '
        Me.CopyBtn.Location = New System.Drawing.Point(12, 64)
        Me.CopyBtn.Name = "CopyBtn"
        Me.CopyBtn.Size = New System.Drawing.Size(260, 23)
        Me.CopyBtn.TabIndex = 4
        Me.CopyBtn.Text = "Copy"
        Me.CopyBtn.UseVisualStyleBackColor = True
        '
        'FolderCopy
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 99)
        Me.Controls.Add(Me.CopyBtn)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.DestText)
        Me.Controls.Add(Me.SourceText)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
        Me.MaximizeBox = False
        Me.Name = "FolderCopy"
        Me.Text = "Folder Copy Using Batch"
        Me.ResumeLayout(False)
        Me.PerformLayout()


    End Sub
    Friend WithEvents SourceText As System.Windows.Forms.TextBox
    Friend WithEvents DestText As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents CopyBtn As System.Windows.Forms.Button


End Class








No comments:

Post a Comment

If the contents is insufficient or if there any error, please write here....


Suggestion are welcome:

Popular Posts