
Since the two procedures are almost identical, only one procedure can be used and the variable passed as a parameter can be used to differentiate between actions
TEST Mode
- Add /L to the initial parameter line
- Count the lines
RUN mode
- Delete /L from the parameter line
- Advance the progress bar (Using the number of lines counted by the test)
Code: Select all
EnableExplicit
CompilerIf Not #PB_Compiler_Thread
MessageRequester("Info", "Enable Thread-Safe in compiler options!")
End
CompilerEndIf
; By default all gadgets will use proportional font (Consolas 9)
SetGadgetFont(#PB_Default, FontID(LoadFont(#PB_Any, "Consolas", 9)))
; Load GUI form
; XIncludeFile "RoboCopy_GUI.pbf"
; -----------------------------------------------------------------------------------
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;
Enumeration FormWindow
#Window_0
EndEnumeration
Enumeration FormGadget
#Btn_Verify
#Btn_Quit
#ListView_0
#Btn_Stop
#Btn_Run
#Btn_Clear
EndEnumeration
Declare ResizeGadgetsWindow_0()
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
OpenWindow(#Window_0, x, y, width, height, "RoboCopy PureGUI", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
CreateStatusBar(0, WindowID(#Window_0))
AddStatusBarField(350)
StatusBarText(0, 0, "Ready")
AddStatusBarField(#PB_Ignore)
StatusBarProgress(0, 1, 0)
ButtonGadget(#Btn_Verify, 10, 10, 100, 25, "Verify")
ButtonGadget(#Btn_Quit, 490, 10, 100, 25, "Quit")
ListViewGadget(#ListView_0, 10, 40, 580, 330)
ButtonGadget(#Btn_Stop, 120, 10, 100, 25, "Stop")
DisableGadget(#Btn_Stop, 1)
ButtonGadget(#Btn_Run, 230, 10, 100, 25, "Run")
DisableGadget(#Btn_Run, 1)
ButtonGadget(#Btn_Clear, 340, 10, 100, 25, "Clear Report")
EndProcedure
Procedure ResizeGadgetsWindow_0()
Protected FormWindowWidth, FormWindowHeight
FormWindowWidth = WindowWidth(#Window_0)
FormWindowHeight = WindowHeight(#Window_0)
ResizeGadget(#Btn_Quit, FormWindowWidth - 110, 10, 100, 25)
ResizeGadget(#ListView_0, 10, 40, FormWindowWidth - 20, FormWindowHeight - StatusBarHeight(0) - 47)
EndProcedure
; -----------------------------------------------------------------------------------
; Source and destination passed by parameters (add in compiler option)
Global Source$ = ProgramParameter(0)
Global Destination$ = ProgramParameter(1)
; If not, ask user
If CountProgramParameters() <> 2
Source$ = PathRequester("Source", "")
Destination$ = PathRequester("Destination", "")
EndIf
Global Parameters$ = " /S " + Source$ + " " + Destination$
Global ID_Thread_List, ID_Thread_Run
Global Files_Folders
; Application Start Here
OpenWindow_0()
StatusBarText(0, 0, "Source: " + Source$ + " - Destination: " + Destination$)
Procedure RoboCopy_Do(*Value)
If *Value = 1 : Debug "Testing"
Files_Folders = 0
Parameters$ + " /L"
Debug Parameters$
EndIf
If *Value = 2 : Debug "Run"
Files_Folders - 26 ; = Nb lines - header and resume
Parameters$ = RemoveString(Parameters$, " /L")
Debug Parameters$
EndIf
Protected Run = RunProgram("robocopy", Parameters$, "",
#PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
If Run
Protected i
DisableGadget(#Btn_Stop, 0)
StatusBarText(0, 0, "Please Wait...")
While ProgramRunning(Run)
If AvailableProgramOutput(Run)
Protected stdout$ = ReadProgramString(Run)
AddGadgetItem(#ListView_0, -1, stdout$)
If *Value = 1
Files_Folders + 1
EndIf
If *Value = 2
i + 1
StatusBarProgress(0, 1, i, #PB_StatusBar_Raised, 0, Files_Folders)
EndIf
Protected stderr$ = ReadProgramError(Run, #PB_Ascii)
If stderr$
StatusBarText(0, 0, "ERROR: " + stderr$)
EndIf
EndIf
Wend
DisableGadget(#Btn_Stop, 1)
StatusBarText(0, 0, "Done.")
; Activate Run button
DisableGadget(#Btn_Run, 0)
If *Value = 1 : StatusBarText(0, 0, "Files & Folders : " + Str(Files_Folders)) : EndIf
; Fill 100% (if progressbar not full updated)
If *Value = 2 : StatusBarProgress(0, 1, Files_Folders) : EndIf
EndIf
EndProcedure
; Main loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case #Btn_Quit
End
Case #Btn_Verify
ID_Thread_List = CreateThread(@RoboCopy_Do(), 1)
Case #Btn_Run
ID_Thread_List = CreateThread(@RoboCopy_Do(), 2)
Case #Btn_Stop
If IsThread(ID_Thread_List) : KillThread(ID_Thread_List) : EndIf
Case #Btn_Clear
ClearGadgetItems(#ListView_0)
EndSelect
Case #PB_Event_CloseWindow
End
Case #PB_Event_SizeWindow
ResizeGadgetsWindow_0()
EndSelect
ForEver
End
Very aerated clean for readability.
Use
CTRL+A followed by
CTRL+I often, this restores indentation and allows you to quickly see nesting errors or loops that are not properly closed.
There is still work to be done:
- Check boxes or options for the user
- Check the parameters
- Create a log file (Robocopy has an option for this (/TEE /unilog:Backup.txt) that allows you to duplicate the output.
- Add a TimeStamp to the log file so that it is not deleted every time
- Make an "About" box with the credits (PureBasic.com, Robocopy.exe (MS)) and a very nice PureBasic logo!
"Think Simple"
Enjoy
