Mega Code Archive

 
Categories / Delphi / Files
 

Synchronise a directory including all sub directories

Title: synchronise a directory including all sub directories Question: you want to have a directory that is always updated with the latest files from a source directory for instance a template directory Answer: uses filectrl procedure tform1.synchronisedirectory(frompath, topath : string ); var sr : tsearchrec ; r : integer ; begin application.processmessages; r := findfirst(frompath + '\*.*', faanyfile, sr); while r = 0 do begin if (sr.name '.') and (sr.name '..') then begin if (sr.attr 0) and (fadirectory 0) then begin synchronisedirectory(frompath + '\'+sr.name , topath + '\' + sr.name) end ; if not directoryexists(topath) then begin forcedirectories(topath); end; {Version control} if fileage(frompath + '\' + sr.name) fileage(topath + '\' + sr.name)then begin CopyFile(pchar(frompath + '\' + sr.name),pchar(topath + '\' + sr.name),FALSE); end; end; r := findnext(Sr) end; findclose(sr); end; just start the procedure with a source directory and a target directorie and the files that are different in the target directory wil be updated , directories will be created and stuff. this program will not however remove directories and files from the target directory if they are not present in the source directory. to implement that you could use the same procedure and and do it vica versa with remove file functions instead. this works in delphi 5 but should also work in lower versions