Jump to content

LanMultiCopy


rasim
 Share

Recommended Posts

Hello! It`s my first project. If anyone find any bug in this script, or have any idea for improvement this script, then post message. :)

Below script intend for computer clubs or offices in which often needs to copy same file or folder on all computer.

;=========================================================
;Program version: 1.0
;Program name: LanMultiCopy
;Description: Copyed files or folders to remote computers
;Autor: Rasim (aka Maza Faka)
;=========================================================


#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
#include <DllCallBack.au3>

Opt("GuiOnEventMode", 1)

Global $FileArray, $Flag
Global $pTimerProc, $uiTimer
Global $Element1, $Element2

$Gui = GUICreate("LanMultiCopy", 600, 400)
GUISetBkColor(0x004B97)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")

;Select file section
;==================================================
GUICtrlCreateLabel("Select file(s)", 80, 30, 100, 15)
GUICtrlSetState(-1, $GUI_FOCUS)
GUICtrlSetFont(-1, 10, 800, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)

$InputFile = GUICtrlCreateInput("", 10, 50, 200, 18, BitOR($ES_AUTOHSCROLL, $ES_CENTER, $ES_OEMCONVERT))
GUICtrlSetTip(-1, "Enter path to copying files", "", 0, 1)

$BtnFiles = GUICtrlCreateButton("...", 210, 49, 50, 21)
GUICtrlSetCursor(-1, 0)
GUICtrlSetOnEvent($BtnFiles, "SelectFiles")
;==================================================

;Select folder section
;==================================================
GUICtrlCreateLabel("Select folder", 80, 100, 100, 15)
GUICtrlSetFont(-1, 10, 800, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)

$InputFolder = GUICtrlCreateInput("", 10, 120, 200, 18, BitOR($ES_AUTOHSCROLL, $ES_CENTER, $ES_OEMCONVERT))
GUICtrlSetTip(-1, "Enter path to copying folder", "", 0, 1)

$BtnFolder = GUICtrlCreateButton("...", 210, 119, 50, 21)
GUICtrlSetCursor(-1, 0)
GUICtrlSetOnEvent(-1, "SelectFolder")
;==================================================

;Select destination section
;==================================================
GUICtrlCreateLabel("Select destination", 80, 170, 120, 15)
GUICtrlSetFont(-1, 10, 800, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)

$InputDest = GUICtrlCreateInput("", 10, 190, 200, 18, BitOR($ES_AUTOHSCROLL, $ES_CENTER, $ES_OEMCONVERT))
GUICtrlSetTip(-1, "Enter destination path", "", 0, 1)

GUICtrlCreateButton("...", 210, 189, 50, 21)
GUICtrlSetCursor(-1, 0)
GUICtrlSetOnEvent(-1, "SelectDest")
;==================================================

;Checkbutton section
;==================================================
$CheckOver = GUICtrlCreateCheckbox("", 10, 220, 20, 15)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetTip(-1, "Overwrites existing files", "", 0, 1)

GUICtrlCreateLabel("Overwrite", 30, 220, 100, 15)
GUICtrlSetFont(-1, 10, 800, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)
;==================================================

;LAN IP-address
;==================================================
GUICtrlCreateLabel("LAN IP-address", 360, 30, 90, 16)
GUICtrlSetFont(-1, 8.5, 400, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)

$InputIP = GUICtrlCreateInput(StringRegExpReplace(@IPAddress1, "[^.].$", "0"), 360, 50, 100, 18, BitOR($ES_AUTOHSCROLL, $ES_CENTER))
GUICtrlSetTip(-1, "Example: 192.168.0.0", "Enter the IP-address of your LAN", 0, 1)
;==================================================

;Computers numbers
;==================================================
GUICtrlCreateLabel("Computers numbers", 480, 30, 150, 16)
GUICtrlSetFont(-1, 8.5, 400, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)

$InputNumber = GUICtrlCreateInput("", 480, 50, 50, 18, BitOR($ES_AUTOHSCROLL, $ES_CENTER))
GUICtrlSetTip(-1, "Example: 1-10", "Enter range of remote computers", 0, 1)

$BtnAdd = GUICtrlCreateButton("Add", 545, 49, 50, 20)
GUICtrlSetOnEvent(-1, "AddComputers")
GUICtrlSetCursor(-1, 0)
;==================================================

;ListView group
;==================================================
GUICtrlCreateLabel("Selected computers", 400, 100, 150, 16)
GUICtrlSetFont(-1, 10, 800, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)

$listview = GUICtrlCreateListView("Computer IP-address|Status|Result", 360, 120, 235, 275, BitOR($LVS_EDITLABELS, $LVS_NOSORTHEADER),$LVS_EX_GRIDLINES + $WS_EX_CLIENTEDGE)
GUICtrlSetTip(-1, "To delete a item click right mouse button", "", 0, 1)
_GUICtrlListViewSetColumnWidth($listview, 0, 124)
_GUICtrlListViewSetColumnWidth($listview, 2, 50)
;==================================================

;ListBox group
;==================================================
GUICtrlCreateList("", 5, 250, 260, 149)
GUICtrlSetBkColor(-1, 0x0071E1)
GUICtrlSetState(-1, $GUI_DISABLE)

$LabelCurPrgr = GUICtrlCreateLabel("Current progress 0%", 77, 260, 200, 15)
GUICtrlSetFont(-1, 9, 800, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

$CurProgress = GUICtrlCreateProgress(10, 280, 250, 15)

$LabelTotPrgr = GUICtrlCreateLabel("Total progress 0%", 77, 310, 200, 15)
GUICtrlSetFont(-1, 9, 800, 0, "Lucidia Console")
GUICtrlSetColor(-1, 0xFFFF00)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

$TotalProgress = GUICtrlCreateProgress(10, 330, 250, 15)

$StartButton = GUICtrlCreateButton("Copy", 100, 360, 50, 25)
GUICtrlSetCursor(-1, 0)
GUICtrlSetOnEvent(-1, "PrepareCopy")
;==================================================

GUISetState()

While 1
    Sleep(200)
WEnd

Func Quit()
    Exit
EndFunc

Func SelectFiles()
    Local $Files = FileOpenDialog("Select file(s)", @HomePath, "All (*.*)", 4)
    If @error Then
        Return SetError(1)
    ElseIf StringInStr($Files, "|") Then
        $FileArray = PathParsing($Files)
    EndIf
    GUICtrlSetState($InputFile, $GUI_ENABLE)
    GuiCtrlSetData($InputFile, $Files)
    GUICtrlSetData($InputFolder, '')
    GUICtrlSetState($InputFolder, $GUI_DISABLE)
EndFunc

Func SelectFolder()
    Local $Folder = FileSelectFolder("Select folder", "", 2)
    If @error Then
        Return SetError(1)
    Else
        GUICtrlSetState($InputFolder, $GUI_ENABLE)
        GUICtrlSetState($InputFile, $GUI_DISABLE)
        GUICtrlSetData($InputFolder, $Folder)
        GuiCtrlSetData($InputFile, '')
        $FileArray = 0
    EndIf
EndFunc

Func SelectDest()
    Local $Out = FileSelectFolder("Select output directory", "::{208D2C60-3AEA-1069-A2D7-08002B30309D}", 2)
    If @error Then
        Return SetError(1)
    Else
        GUICtrlSetData($InputDest, StringRegExpReplace($Out, "^\\*[^\\]*\\", ""))
    EndIf
EndFunc

Func AddComputers()
    $Element1 = ''
    $Element2 = ''
    Local $ReadLANIP = GUICtrlRead($InputIP)
    Local $ReadCpuRange = GUICtrlRead($InputNumber)
    
    If Not $ReadLANIP Then
        $Element1 = $InputIP
        Alarm()
        MsgBox(16, "Error", "Enter the IP-address of your LAN")
        GUICtrlSetState($InputIP, $GUI_FOCUS)
        Return SetError(1)
    ElseIf Not $ReadCpuRange Then
        $Element1 = $InputNumber
        Alarm()
        MsgBox(16, "Error", "Enter computer(s) number(s)")
        GUICtrlSetState($InputNumber, $GUI_FOCUS)
        Return SetError(1)
    EndIf
    
    Local $LanIP = StringRegExpReplace($ReadLANIP, "[^.]*$", "")
    Local $CpuFirst = StringRegExpReplace($ReadCpuRange, "-.*$", "")
    Local $CpuLast = StringRegExpReplace($ReadCpuRange, "^.*-", "")
    
    For $i = $CpuFirst To $CpuLast
        $var = GUICtrlCreateListViewItem($LanIP & $i, $listview)
        Local $ConMenu = GUICtrlCreateContextMenu($var)
        GUICtrlCreateMenuItem("Delete", $ConMenu)
        GUICtrlSetOnEvent(-1, "DelListItem")
        GUICtrlCreateMenuItem("Delete all", $ConMenu)
        GUICtrlSetOnEvent(-1, "DelAllListItem")
    Next
EndFunc

Func PrepareCopy()
    $Element1 = ''
    $Element2 = ''
    ;Check input files, folder and output path
    Local $ReadInputFile = GUICtrlRead($InputFile)
    Local $ReadInputFolder = GUICtrlRead($InputFolder)
    Local $ReadInputDest = StringRegExpReplace(GUICtrlRead($InputDest), ":", "")
    Local $LV_iCount = _GuiCtrlListViewGetItemCount($listview)
    
    If $ReadInputFile = '' And $ReadInputFolder = '' Then
        $Element1 = $InputFile
        $Element2 = $InputFolder
        Alarm()
        MsgBox(16, "Error", "Please select file(s) or folder to copy")
        GUICtrlSetState($InputFile, $GUI_FOCUS)
        Return SetError(1)
    ElseIf $ReadInputDest = '' Then
        $Element1 = $InputDest
        $Element2 = ''
        Alarm()
        MsgBox(16, "Error", "Please select output directory")
        GUICtrlSetState($InputDest, $GUI_FOCUS)
        Return SetError(1)
    ElseIf $LV_iCount = 0 Then
        MsgBox(16, "Error", "Please add LAN IP-address and computers range")
        GUICtrlSetState($BtnAdd, $GUI_FOCUS)
        Return SetError(1)
    EndIf
    
    Global $LV_TXT_ARRAY[1]
    
    ReDim $LV_TXT_ARRAY[$LV_iCount+1]
    $LV_TXT_ARRAY[0] = $LV_iCount
    
    For $i = 1 To $LV_TXT_ARRAY[0]
        $LV_TXT_ARRAY[$i] = _GUICtrlListViewGetItemText($listview, $i-1, 0)
    Next
    Copy($ReadInputFile, $ReadInputFolder, $ReadInputDest, $LV_TXT_ARRAY)
EndFunc

Func Copy($File, $Folder, $Dest, $CPU_ARRAY)
    PingComp($CPU_ARRAY)
    Local $curPercent, $totPercent, $RemFile, $check, $copy
    ;If selected few files
    If IsArray($FileArray) Then
        For $i = 1 To $CPU_ARRAY[0]
            _GUICtrlListViewSetItemText($listview, $i - 1, 2, "progress")
            TrayTip("Copying in progress", "Copying", 1, 1)
            $curPercent = 0
            GUICtrlSetData($CurProgress, 0)
            GUICtrlSetData($LabelCurPrgr, "Current progress 0%")
            
            For $a = 1 To $FileArray[0]
                TrayTip("Copying in progress", "Copying " & $FileArray[$a], 1, 1)
                If BitAND(GUICtrlRead($CheckOver), $GUI_CHECKED) Then
                    $copy = FileCopy($FileArray[$a], "\\" & $CPU_ARRAY[$i] &"\"& $Dest & "\", 9)
                    If $copy = 0 Then
                        TrayTip("", "", 0)
                        If CheckWrong("\\" & $CPU_ARRAY[$i] &"\"& $Dest, $FileArray[$a]) Then
                            ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent)
                            Return SetError(1)
                            EndIf
                        EndIf
                    Else
                        TrayTip("Copying in progress", "Copying " & $FileArray[$a], 1, 1)
                        $RemFile = "\\" & $CPU_ARRAY[$i] &"\"& $Dest &"\"& StringRegExpReplace($FileArray[$a], "^.*\\", "")
                        If FileExists($RemFile) Then
                            $quote = MsgBox(68, "Warning", "File " & $RemFile & " is exist, overwrite?")
                            If $quote = 6 Then
                                $copy = FileCopy($FileArray[$a], "\\" & $CPU_ARRAY[$i] &"\"& $Dest & "\", 9)
                                If $copy = 0 Then
                                    TrayTip("", "", 0)
                                    If CheckWrong("\\" & $CPU_ARRAY[$i] &"\"& $Dest, $FileArray[$a]) Then
                                        ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent)
                                        Return SetError(1)
                                    EndIf
                                EndIf
                            EndIf
                        Else
                            TrayTip("Copying in progress", "Copying " & $FileArray[$a], 1, 1)
                            $copy = FileCopy($FileArray[$a], "\\" & $CPU_ARRAY[$i] &"\"& $Dest & "\", 9)
                            If $copy = 0 Then
                                TrayTip("", "", 0)
                                If CheckWrong("\\" & $CPU_ARRAY[$i] &"\"& $Dest, $FileArray[$a]) Then
                                    ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent)
                                    Return SetError(1)
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                    $curPercent += Round(100 / $FileArray[0])
                    GUICtrlSetData($LabelCurPrgr, "Current progress " & $curPercent & "%")
                    GUICtrlSetData($CurProgress, $curPercent)
                Next
                $totPercent += Round(100 / $CPU_ARRAY[0])
                GUICtrlSetData($TotalProgress, $totPercent)
                GUICtrlSetData($LabelTotPrgr, "Total progress " & $totPercent & "%")
                _GUICtrlListViewSetItemText($listview, $i - 1, 2, "Done")
                TrayTip("", "", 0)
            Next
            ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent, "Files")
    ;If selected folder                 
    ElseIf $Folder Then
        For $i = 1 To $CPU_ARRAY[0]
            _GUICtrlListViewSetItemText($listview, $i - 1, 2, "progress")
            TrayTip("Copying in progress", "Copying " & $Folder, 1, 1)
            $curPercent = 0
            GUICtrlSetData($LabelCurPrgr, "Current progress " & $curPercent & "%")
            GUICtrlSetData($CurProgress, $curPercent)
            If Not DirCopy($Folder, "\\" & $CPU_ARRAY[$i] &"\"& $Dest &"\"& StringRegExpReplace($Folder, "^.*\\", ""), 1) Then
                TrayTip("", "", 0)
                MsgBox(16, "Error", "Folder " & $Folder & " not found!" & "Or indicate output path wrong!")
                Return SetError(1)
            EndIf
            $totPercent += Round(100 / $CPU_ARRAY[0])
            $curPercent += Round(100 / 1)
            GUICtrlSetData($LabelTotPrgr, "Total progress " & $totPercent & "%")
            GUICtrlSetData($TotalProgress, $totPercent)
            _GUICtrlListViewSetItemText($listview, $i - 1, 2, "Done")
            GUICtrlSetData($LabelCurPrgr, "Current progress " & $curPercent & "%")
            GUICtrlSetData($CurProgress, $curPercent)
            TrayTip("", "", 0)
        Next
        ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent, "Folder")
    ;If selected one file
    Else
        $curPercent = 0
        For $i = 1 To $CPU_ARRAY[0]
            _GUICtrlListViewSetItemText($listview, $i - 1, 2, "progress")
            TrayTip("Copying in progress", "Copying " & $File, 1, 1)
            $curPercent += Round(100 / 1)
            GUICtrlSetData($LabelCurPrgr, "Current progress " & $curPercent & "%")
            GUICtrlSetData($CurProgress, $curPercent)
            If BitAND(GUICtrlRead($CheckOver), $GUI_CHECKED) Then
                $copy = FileCopy($File, "\\" & $CPU_ARRAY[$i] &"\"& $Dest & "\", 9)
                If $copy = 0 Then
                        TrayTip("", "", 0)
                        If CheckWrong("\\" & $CPU_ARRAY[$i] &"\"& $Dest, $File) Then
                            ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent)
                            Return SetError(1)
                        EndIf
                    EndIf
                Else
                    TrayTip("Copying in progress", "Copying " & $File, 1, 1)
                    $RemFile = "\\" & $CPU_ARRAY[$i] &"\"& $Dest &"\"& StringRegExpReplace($File, "^.*\\", "")
                        If FileExists($RemFile) Then
                            $quote = MsgBox(68, "Warning", "File " & $RemFile & " is exist, overwrite?")
                            If $quote = 6 Then
                                $copy = FileCopy($File, "\\" & $CPU_ARRAY[$i] &"\"& $Dest & "\", 9)
                                If $copy = 0 Then
                                    TrayTip("", "", 0)
                                    If CheckWrong("\\" & $CPU_ARRAY[$i] &"\"& $Dest, $File) Then
                                        ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent)
                                        Return SetError(1)
                                    EndIf
                                EndIf
                            Else
                                _GUICtrlListViewSetItemText($listview, $i - 1, 2, "Done")
                                $totPercent += Round(100 / $CPU_ARRAY[0])
                                GUICtrlSetData($TotalProgress, $totPercent)
                                GUICtrlSetData($LabelTotPrgr, "Total progress " & $totPercent & "%")
                                _GUICtrlListViewSetItemText($listview, $i - 1, 2, "Done")
                                $curPercent = 0
                                GUICtrlSetData($CurProgress, 0)
                                GUICtrlSetData($LabelCurPrgr, "Current progress 0%")
                                ContinueLoop
                            EndIf
                        EndIf
                    EndIf
                    
                    $totPercent += Round(100 / $CPU_ARRAY[0])
                    GUICtrlSetData($TotalProgress, $totPercent)
                    GUICtrlSetData($LabelTotPrgr, "Total progress " & $totPercent & "%")
                    _GUICtrlListViewSetItemText($listview, $i - 1, 2, "Done")
                    $curPercent = 0
                    GUICtrlSetData($CurProgress, 0)
                    GUICtrlSetData($LabelCurPrgr, "Current progress 0%")
                Next
                ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent, "Files")
            EndIf
        EndFunc

Func PingComp(ByRef $CPU_Ping_Array)
    For $i = $CPU_Ping_Array[0] To 1 Step -1
        _GUICtrlListViewSetItemText($listview, $i - 1, 2, '')
        Ping($CPU_Ping_Array[$i], 1000)
        If @error Then
            _ArrayDelete($CPU_Ping_Array, $i)
            $CPU_Ping_Array[0] -= 1
            _GUICtrlListViewSetItemText($listview, $i - 1, 1, "Offline")
            _GUICtrlListViewSetItemText($listview, $i - 1, 2, "Fail")
        Else
            _GUICtrlListViewSetItemText($listview, $i - 1, 1, "Online")
        EndIf
    Next
    Return $CPU_Ping_Array
EndFunc

Func PathParsing($retString)
    Local $strSplit = StringSplit($retString, "|")
    Local $ParsingArray[$strSplit[0]]
    Local $Path = StringRegExpReplace($strSplit[1], "\\$", "")
    
    For $i = 2 To $strSplit[0]
        $ParsingArray[$i-1] = $Path &"\"& $strSplit[$i]
    Next
    
    $ParsingArray[0] = $strSplit[0] -1
    
    Return $ParsingArray
EndFunc

Func Alarm()
    $pTimerProc = _DllCallBack("ChangeColor")
    $uiTimer = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 500, "ptr", $pTimerProc)
    $uiTimer = $uiTimer[0]
EndFunc

Func ChangeColor()
    If WinExists("Error") Then
        $Flag = Not $Flag
        If $Flag Then
            GUICtrlSetBkColor($Element1, 0xFF0000)
            GUICtrlSetBkColor($Element2, 0xFF0000)
        Else
            GUICtrlSetBkColor($Element1, $GUI_BKCOLOR_TRANSPARENT)
            GUICtrlSetBkColor($Element2, $GUI_BKCOLOR_TRANSPARENT)
        EndIf
    Else
        _DllCallBack_Free($pTimerProc)
        DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $uiTimer)
        GUICtrlSetBkColor($Element1, $GUI_BKCOLOR_TRANSPARENT)
        GUICtrlSetBkColor($Element2, $GUI_BKCOLOR_TRANSPARENT)
    EndIf
EndFunc

Func DelListItem()
    _GUICtrlListViewDeleteItemsSelected($listview)
EndFunc

Func DelAllListItem()
    _GUICtrlListViewDeleteAllItems($listview)
EndFunc

Func ClearValues($LabelTotPrgr, $LabelCurPrgr, $TotalProgress, $CurProgress, $totPercent, $sMsg = '')
    If $sMsg = '' Then
        MsgBox(48, "Canceled", "Copying canceled")
        GUICtrlSetData($LabelTotPrgr, "Total progress 0%")
        GUICtrlSetData($LabelCurPrgr, "Current progress 0%")
        GUICtrlSetData($TotalProgress, 0)
        GUICtrlSetData($CurProgress, 0)
        $totPercent = 0
    Else
        GUICtrlSetData($LabelTotPrgr, "Total progress 100%")
        GUICtrlSetData($TotalProgress, 100)
        GUICtrlSetData($LabelCurPrgr, "Current progress 100%")
        GUICtrlSetData($CurProgress, 100)
        MsgBox(64, "Done", $sMsg & " copying complete")
        GUICtrlSetData($LabelTotPrgr, "Total progress 0%")
        GUICtrlSetData($LabelCurPrgr, "Current progress 0%")
        GUICtrlSetData($TotalProgress, 0)
        GUICtrlSetData($CurProgress, 0)
        $totPercent = 0
    EndIf
EndFunc

Func CheckWrong($sPath, $sFile)
    $question = MsgBox(20, "Error", "File " & $sFile & " not copyed!" & @CR _
    & "Possible you indicate wrong (" & $sPath & ") output path!" & @CR _
    & "Continue anyway?")
    If $question <> 6 Then Return 1
EndFunc
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...