CopyDirScript

Recommended Posts

Karl1983 0

Karl1983 0

I am new to development. What I am sharing here is a first cut draft of a programme to copy files which I am writing to build up my skills (no real other purpose).

Some parts are not yet complete but are in working order. By this I mean that they need to be enhanced to provide better functionality/features and less risk of not doing what you want.

I will update as I make changes but thought I would share now in case the current version is of use.

I am not expecting anyone else to do any work on this but at the same time as this seems like a community for people helping and building up so if people want to contribute feel free and you will of course have the appropriate mention

I am considering this an alpha release on the basis that whilst it functions it is not yet complete. Before I work on the missing features I will look to optimise the code as I am sure I am writing things in a long winded and illogical way to some extent (not to mention messy). So feel free to offer constructive criticism.

Programme Name: DirCopier

Version: V0.1 (alpha)

Author: Karl Griffiths

Description: This programme will copy files from one location to another. It will replace all files with the same name in the destination folder currently. It has a very basis check function which compares the size of the source and destination folders to see if they match in size, it uses this same logic to confirm when copy is complete. It creates a CSV file in the same location as the compiled script and uses this to store the last used source and destination locations incase you want to use it as a regular back up tool.

Further Development: See script

Special thanks: Wayfarer, Universalist

#include <ButtonConstants.au3>#include <EditConstants.au3>#include <GUIConstantsEx.au3>#include <ProgressConstants.au3>#include <StaticConstants.au3>#include <WindowsConstants.au3>#Include<Array.au3>#include<file.au3>;### Programme Name: DirCopier;### Version: V0.1 (alpha);### Author: Karl Griffiths;### Description: This programme will copy files from one location to another. It will replace all files;### with the same name in the destination folder currently. It has a very basis check;### function which compares the size of the source and destination folders to see if they ;### match in size, it uses this same logic to confirm when copy is complete. It creates;### a CSV file in the same location as the compiled script and uses this to store the ;### last used source and destination locations incase you want to use it as a regular back up tool. ;### Further Development: See script;### Special thanks: Wayfarer, Universalist Global$srcbutton,$destbutton,$src1,$src2,$dest1,$dest2,$destinput,$srcsize,$destsize,$file;#### GUI ####;Main GuiGUICreate("DirCopier",623,233,267,178)GUISetFont(8,800,0,"MS Sans Serif");Location InputsGUICtrlCreateGroup("Locations",8,16,601,97)GUICtrlCreateLabel("Source",16,48,44,17)GUICtrlCreateLabel("Destination",16,88,68,17)$srcinput=GUICtrlCreateInput("",94,46,393,21)GUICtrlSetState($srcinput,$GUI_DISABLE)$destinput=GUICtrlCreateInput("",93,83,393,21)GUICtrlSetState($destinput,$GUI_DISABLE)$srcbutton=GUICtrlCreateButton("Src",495,45,75,25)$destbutton=GUICtrlCreateButton("Dest",494,80,75,25);File Management radiosGUICtrlCreateGroup("File Management",8,128,281,89)$replace_off=GUICtrlCreateRadio("Do not override existing files (Default)",16,152,249,17)GUICtrlSetState($replace_off,$GUI_DISABLE)$replace_on=GUICtrlCreateRadio("Override existing files",16,176,241,17)GUICtrlSetState(-1,$GUI_CHECKED)GUICtrlSetState($replace_on,$GUI_DISABLE)GUICtrlCreateGroup("",-99,-99,1,1);Pre scan and go buttonsGUICtrlCreateGroup("Execution",312,128,297,89)$go=GUICtrlCreateButton("Copy",496,152,75,25)$check=GUICtrlCreateButton("Pre-Scan",328,152,97,25);The progress GUI should not be included until code is provided to support progress;GUICtrlCreateProgress(392, 192, 174, 17);GUICtrlCreateLabel("Progress", 328, 192, 53, 17);####Functions####; Set initial filepath and create if file not already presentFunc_FilePathCall()$file=FileOpen(@ScriptDir&"\filepath.csv",0);Open file to call the filepathsIf$file=-1Then;If file is not found then do somethingFileWrite(@scriptdir&"\filepath.csv","C:\"&@CRLF);create file, write to first line and then move to next lineFileWrite(@scriptdir&"\filepath.csv","C:\");continue to write to file on next lineMsgBox(0,"Notification","This is the first time you have run this programme."&@CRLF&_"Initially the copy cource and destination folders are the same."&@CRLF&_"You can change this by clicking the Src and Dest buttons")EndIf$file=FileOpen(@ScriptDir&"\filepath.csv",0);Open file to call the filepaths$src1=FileReadLine($file,1);make first line of file the variable$dest1=FileReadLine($file,2);make second line of file the variableFileClose($file);close off file for later useGUICtrlSetData($srcinput,$src1);Set input box for the source so the assigned variableGUICtrlSetData($destinput,$dest1);Set input box for the source so the assigned variableEndFunc;==>_FilePathCall();Setting a new default input(done everytime the Dir location is amended)Func_SetNewDir($inputname,$csvLine)$tmp=FileSelectFolder("Please Select Backup Source","");user prompt to select a folderIf@errorThenReturnSetError(1,0,'Error')_FileWriteToLine(@ScriptDir&"\filepath.csv",$csvLine,$tmp,1);Writes the new location to the filepath csv used in 'Func _FilePathCall()'$file=FileOpen(@ScriptDir&"\filepath.csv",0);Opens file with read access$DirSelect=FileReadLine($file,$csvLine);Reads the new location that has been setFileClose($file);Closes file for later useGUICtrlSetData($inputname,$DirSelect);Sets the input box to reflect the new locationEndFunc;==>_SetNewDir;Checks the size of a folder - script taken from autoit forums but in my haste I didnt take down detail of who produced it and cannot find postFunc_GetFolderSize($dir,$units="b")$obj=ObjCreate("Scripting.FileSystemObject")If@error==0Then$folder=$obj.GetFolder($dir)If@error==0ThenSelectCase$units=="b"Return$folder.SizeCase$units=="kb"Return$folder.Size/1024Case$units=="mb"Return$folder.Size/1024/1024Case$units=="gb"Return$folder.Size/1024/1024/1024Case$units=="tb"Return$folder.Size/1024/1024/1024/1024EndSelectElseMsgBox(0,"","An error occurred, check that "&$dir&" exists.")EndIfElseMsgBox(0,"","An error occurred, check that "&$dir&" exists.")EndIfEndFunc;==>_GetFolderSize(); Checks if two folders are the same size - not the most accurate way to confirm two folders are the same ; ##### this feature needs to be improved to check files and version numbers #####Func_Compare ()Ifguictrlread($srcinput)=""ThenMsgBox(0,"Error","Please choose a source directory")ElseIfguictrlread($destinput)=""ThenMsgBox(0,"Error","Please choose a destination directory")Else$srcsize=_GetFolderSize(guictrlread($srcinput),"mb")$destsize=_GetFolderSize(guictrlread($destinput),"mb")If$srcsize=$destsizeThenMsgBox(0,"Folder Check","Each folder is the same size"&@CRLF&_"This would suggest no back up is required")ElseMsgBox(0,"Folder Check","Folders sizes do not match."&@CRLF&_"This would suggest you should run a back up")EndIfEndIfEndFunc;==>_Compare();Checks the folder sizes every 30 seconds until they match - a weak way of veryfying copy has completed (assumes directories would be different sizes before copy);##### this feature needs to be improved to calculate progress and provide %complete #####Func_CheckProgress()While1Sleep(1000)$srcsize=_GetFolderSize(guictrlread($srcinput),"b")$destsize=_GetFolderSize(guictrlread($destinput),"b")If$srcsize=$destsizeThenExitLoopWEndMsgBox(0,"Notification","Folders are now of equal size."&@CRLF&_"This would suggest the copy process is now complete")EndFunc;==>_CheckProgress;This function carries out the copy of files from source location to destination location;##### this feature needs to be improved to check which files need copying based on version (last modified) rather than copying all files #####Func_CopyFiles()Ifguictrlread($srcinput)=guictrlread($destinput)ThenMsgBox(0,"Error","The source and destination directory must not be the same")ElseIfguictrlread($replace_on)=1Then;Checks to if the radio to replace files with the same name is onDirCopy(guictrlread($srcinput),guictrlread($destinput),1);Copies - allows files to be replaced_CheckProgress()ElseIfguictrlread($replace_on)=4Then;Checks to if the radio to replace files with the same name is offDirCopy(guictrlread($srcinput),guictrlread($destinput),0);Copies - does not allow files to be replacedEndIfEndIfEndFunc;==>_CopyFile; ########################## Function Calls ########################GUISetState(@SW_SHOW);Shows the GUI_FilePathCall()While1;Calls various functions depending on certain button presses$msg=GUIGetMsg()SelectCase$msg=$GUI_EVENT_CLOSEExitLoopCase$msg=$srcbutton_SetNewDir($srcinput,"1")Case$msg=$destbutton_SetNewDir($destinput,"2")Case$msg=$check_Compare ()Case$msg=$go_CopyFiles()EndSelectWEnd

Edited 20130903: Version 0.3 script attached below. Takes into account adjustment from AZJIO and label fix following comment from JohnOne

#include <GUIConstantsEx.au3>#include<file.au3>;### Programme Name: DirCopier;### Version: V0.1 (alpha);### Author: Karl Griffiths;### Description: This programme will copy files from one location to another. It will replace all files;### with the same name in the destination folder currently. It has a very basis check;### function which compares the size of the source and destination folders to see if they;### match in size, it uses this same logic to confirm when copy is complete. It creates;### a CSV file in the same location as the compiled script and uses this to store the;### last used source and destination locations incase you want to use it as a regular back up tool.;### Further Development: See script;### Special thanks: Wayfarer, UniversalistGlobal$srcbutton,$destbutton,$src1,$src2,$dest1,$dest2,$destinput,$srcsize,$destsize,$fileGlobal$sFileCSV=@ScriptDir&"\filepath.csv";#### GUI ####;Main Gui$hGUI=GUICreate("DirCopier",623,233,267,178)GUISetFont(8,800,0,"MS Sans Serif");Location InputsGUICtrlCreateGroup("Locations",8,16,601,97)GUICtrlCreateLabel("Source",16,48,44,17)GUICtrlCreateLabel("Destination",16,88,68,17)$srcinput=GUICtrlCreateInput("",94,46,393,21)GUICtrlSetState($srcinput,$GUI_DISABLE)$destinput=GUICtrlCreateInput("",93,83,393,21)GUICtrlSetState($destinput,$GUI_DISABLE)$srcbutton=GUICtrlCreateButton("Src",495,45,75,25)$destbutton=GUICtrlCreateButton("Dest",494,80,75,25);File Management radiosGUICtrlCreateGroup("File Management",8,128,281,89)$replace_off=GUICtrlCreateRadio("Do not override existing files (Default)",16,152,249,17)GUICtrlSetState($replace_off,$GUI_DISABLE)$replace_on=GUICtrlCreateRadio("Override existing files",16,176,241,17)GUICtrlSetState(-1,$GUI_CHECKED)GUICtrlSetState($replace_on,$GUI_DISABLE)GUICtrlCreateGroup("",-99,-99,1,1);Pre scan and go buttonsGUICtrlCreateGroup("Execution",312,128,297,89)$go=GUICtrlCreateButton("Copy",496,152,75,25)$check=GUICtrlCreateButton("Pre-Scan",328,152,97,25);The progress GUI should not be included until code is provided to support progress;GUICtrlCreateProgress(392, 192, 174, 17);GUICtrlCreateLabel("Progress", 328, 192, 53, 17);####Functions####; Set initial filepath and create if file not already presentFunc_FilePathCall()$file=FileOpen($sFileCSV,0);Open file to call the filepathsIf$file=-1Then;If file is not found then do somethingFileWrite($sFileCSV,"C:\"&@CRLF);create file, write to first line and then move to next lineFileWrite($sFileCSV,"C:\");continue to write to file on next lineMsgBox(0,"Notification","This is the first time you have run this programme."&@CRLF&_"Initially the copy cource and destination folders are the same."&@CRLF&_"You can change this by clicking the Src and Dest buttons",0,$hGUI)EndIf$file=FileOpen($sFileCSV,0);Open file to call the filepaths$src1=FileReadLine($file,1);make first line of file the variable$dest1=FileReadLine($file,2);make second line of file the variableFileClose($file);close off file for later useGUICtrlSetData($srcinput,$src1);Set input box for the source so the assigned variableGUICtrlSetData($destinput,$dest1);Set input box for the source so the assigned variableEndFunc;==>_FilePathCall;Setting a new default input(done everytime the Dir location is amended)Func_SetNewDir($inputname,$csvLine)$tmp=FileSelectFolder("Please Select Backup Source","");user prompt to select a folderIf@errorThenReturnSetError(1,0,'Error')_FileWriteToLine($sFileCSV,$csvLine,$tmp,1);Writes the new location to the filepath csv used in 'Func _FilePathCall()'$file=FileOpen($sFileCSV,0);Opens file with read access$DirSelect=FileReadLine($file,$csvLine);Reads the new location that has been setFileClose($file);Closes file for later useGUICtrlSetData($inputname,$DirSelect);Sets the input box to reflect the new locationEndFunc;==>_SetNewDir;Checks the size of a folder - script taken from autoit forums but in my haste I didnt take down detail of who produced it and cannot find postFunc_GetFolderSize($dir,$units="b")$obj=DirGetSize($dir)IfNot@errorThenSwitch$unitsCase"b"Return$objCase"kb"Return$obj/1024Case"mb"Return$obj/1024/1024Case"gb"Return$obj/1024/1024/1024Case"tb"Return$obj/1024/1024/1024/1024EndSwitchElseMsgBox(0,"","An error occurred, check that "&$dir&" exists.",0,$hGUI)EndIfEndFunc;==>_GetFolderSize; Checks if two folders are the same size - not the most accurate way to confirm two folders are the same; ##### this feature needs to be improved to check files and version numbers #####Func_Compare()Local$sSrcPath=GUICtrlRead($srcinput)Local$sDestPath=GUICtrlRead($destinput)If$sSrcPath=""ThenMsgBox(0,"Error","Please choose a source directory",0,$hGUI)ElseIf$sDestPath=""ThenMsgBox(0,"Error","Please choose a destination directory",0,$hGUI)Else$srcsize=_GetFolderSize($sSrcPath,"mb")$destsize=_GetFolderSize($sDestPath,"mb")If$srcsize=$destsizeThenMsgBox(0,"Folder Check","Each folder is the same size"&@CRLF&_"This would suggest no back up is required",0,$hGUI)ElseMsgBox(0,"Folder Check","Folders sizes do not match."&@CRLF&_"This would suggest you should run a back up",0,$hGUI)EndIfEndIfEndFunc;==>_Compare;Checks the folder sizes every 30 seconds until they match - a weak way of veryfying copy has completed (assumes directories would be different sizes before copy);##### this feature needs to be improved to calculate progress and provide %complete #####Func_CheckProgress()While1Sleep(1000)$srcsize=_GetFolderSize(GUICtrlRead($srcinput),"b")$destsize=_GetFolderSize(GUICtrlRead($destinput),"b")If$srcsize=$destsizeThenExitLoopWEndMsgBox(0,"Notification","Folders are now of equal size."&@CRLF&_"This would suggest the copy process is now complete")EndFunc;==>_CheckProgress;This function carries out the copy of files from source location to destination location;##### this feature needs to be improved to check which files need copying based on version (last modified) rather than copying all files #####Func_CopyFiles()Local$sSrcPath=GUICtrlRead($srcinput)Local$sDestPath=GUICtrlRead($destinput)If$sSrcPath=$sDestPathThenMsgBox(0,"Error","The source and destination directory must not be the same",0,$hGUI)ElseIfGUICtrlRead($replace_on)=1Then;Checks to if the radio to replace files with the same name is onDirCopy($sSrcPath,$sDestPath,1);Copies - allows files to be replaced_CheckProgress()ElseIfGUICtrlRead($replace_on)=4Then;Checks to if the radio to replace files with the same name is offDirCopy($sSrcPath,$sDestPath,0);Copies - does not allow files to be replacedEndIfEndIfEndFunc;==>_CopyFiles; ########################## Function Calls ########################GUISetState(@SW_SHOW);Shows the GUI_FilePathCall()While1;Calls various functions depending on certain button pressesSwitchGUIGetMsg()Case$GUI_EVENT_CLOSEExitLoopCase$srcbutton_SetNewDir($srcinput,"1")Case$destbutton_SetNewDir($destinput,"2")Case$check_Compare()Case$go_CopyFiles()EndSwitchWEnd