Sign in to follow this  
Followers 0
jimmyman

Function Recursion and Files

4 posts in this topic

#1 ·  Posted (edited)

Hello, I'm new to the forum, anyway, heres my problem.

I regularly backup my external hard-drive and find it annoying just copying and pasting it, so I decided to create a program the deletes the old files and checks modified dates to make it an exact copy of the folder. However I could not think of a way to do this without using this method: function recursion. However it seems it will only copy the first folder of each directory (hard to explain, see for yourself), the only reason I can think of this happening is because the variables for the file search and fins next file are messing with the function that called it (itself). Anyone see the problem? I appreciate the help.

CODE
#include <GUIConstants.au3>

$window=GUICreate("Robert's Folder Updater",375,115, -1, -1, -1, $WS_EX_ACCEPTFILES)

$Source_Label = GUICtrlCreateLabel ("Source:", 10, 17, 50)

$Destination_Label = GUICtrlCreateLabel ("Destination:", 10, 52, 60)

$Source_Input = GUICtrlCreateInput ("", 70, 13, 215)

GUICtrlSetState(-1,$GUI_DROPACCEPTED)

$Destination_Input = GUICtrlCreateInput ("", 70, 48, 215)

GUICtrlSetState(-1,$GUI_DROPACCEPTED)

$Source_Button = GUICtrlCreateButton ("Browse", 295, 10, 70)

$Destination_Button = GUICtrlCreateButton ("Browse", 295, 45, 70)

$Run = GUICtrlCreateButton ("Run", 152, 80, 70)

GUISetState (@SW_SHOW)

$notdeleted=""

$notcopied=""

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $Source_Button

$var=FileSelectFolder("Choose a source folder", "", 7, GUICtrlRead($Source_Input))

If $var <> "" Then

GUICtrlSetData($Source_Input, $var)

EndIf

Case $msg = $Destination_Button

$var=FileSelectFolder("Choose a destination folder", "", 7, GUICtrlRead($Destination_Input))

If $var <> "" Then

GUICtrlSetData($Destination_Input, $var)

EndIf

Case $msg = $Run

If GUICtrlRead($Source_Input)="" Then

MsgBox(0, "Error!", "Please fill out all areas")

ElseIf GUICtrlRead($Destination_Input)="" Then

MsgBox(0, "Error!", "Please fill out all areas")

Else

If GUICtrlRead($Destination_Input) = GUICtrlRead($Source_Input) Then

MsgBox(0, "Error!", "The destination is the same as the source folder")

ElseIf StringInStr(GUICtrlRead($Destination_Input),GUICtrlRead($Source_Input)) Then

MsgBox(0, "Error!", "The destination is a sub-folder the source folder")

Else

If FileExists(GUICtrlRead($Source_Input)) Then

$search=FileFindFirstFile(GUICtrlRead($Source_Input) & "\*")

If @error Then

MsgBox(0, "Error!", "The source folder is empty!")

Else

If FileExists(GUICtrlRead($Destination_Input)) Then

Start(GUICtrlRead($Source_Input), GUICtrlRead($Destination_Input))

Else

MsgBox(0, "Error!", "The destination folder doesnt exist")

EndIf

EndIf

Else

MsgBox(0, "Error!", "The source folder doesnt exist")

EndIf

EndIf

EndIf

EndSelect

WEnd

Func Start($source, $destination)

GUICtrlSetData($Run, "Deleting")

Delete($source, $destination)

If $notdeleted <> "" Then

MsgBox(0, "error!", "The following file(s) could'nt be deleted" & $notdeleted)

$notdeleted=""

EndIf

GUICtrlSetData($Run, "Copying")

Copy($source, $destination)

If $notcopied <> "" Then

MsgBox(0, "error!", "The following file(s) failed to copy" & $notcopied)

$notcopied=""

EndIf

MsgBox(0, "Success", "The folder has been updated")

GUICtrlSetData($Run, "Run")

EndFunc

Func Delete($source, $destination)

$search=FileFindFirstFile($destination & "\*")

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

If StringInStr(FileGetAttrib($destination & "\" & $file), "D") Then

If FileExists($source & "\" & $file) Then

Delete($source & "\" & $file, $destination & "\" & $file)

Else

$error = DirRemove($destination & "\" & $file, 1)

If $error=0 Then

$notdeleted = $notdeleted & @CRLF & $destination & "\" & $file

EndIf

EndIf

Else

If FileExists($source & "\" & $file) Then

Else

$error = FileDelete($destination & "\" & $file)

If $error=0 Then

$notdeleted = $notdeleted & @CRLF & $destination & "\" & $file

EndIf

EndIf

EndIf

WEnd

FileClose($search)

EndFunc

Func Copy($source, $destination)

$search=FileFindFirstFile($source & "\*")

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

If StringInStr(FileGetAttrib($source & "\" & $file), "D") Then

Copy($source & "\" & $file, $destination & "\" & $file)

Else

If FileExists($destination & "\" & $file) Then

If FileGetTime($source & "\" & $file, 0, 1) > FileGetTime($destination & "\" & $file, 0, 1) Then

$error = FileCopy($source & "\" & $file, $destination & "\" & $file, 9)

If $error=0 Then

$notcopied = $notcopied & @CRLF & $source & "\" & $file

EndIf

Else

EndIf

Else

$error = FileCopy($source & "\" & $file, $destination & "\" & $file, 9)

If $error=0 Then

$notcopied = $notcopied & @CRLF & $source & "\" & $file

EndIf

EndIf

EndIf

WEnd

FileClose($search)

EndFunc

Edited by jimmyman

Share this post


Link to post
Share on other sites

Thanks weaponx, but neither of those do the same thing. Unstoppable Copier is for recovering destroyed data and xcopy doesn't have some of the main features this does (deleting files that are in the destination folder that arent in the source folder).

But even if I can find a program that does exactly what I want I still want to get this working, I'm not the type to give up on code.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0