Link to home
Start Free TrialLog in
Avatar of diegomirner
diegomirnerFlag for Switzerland

asked on

Syncronize directories

Hi Experts

i have to  2 directories ( fedora 4) , each of one haves all most  the same files and directories  , but i need jest one of them with the newest version of all files and subdirectories , what i should do ?

Thanks
Avatar of 2266180
2266180
Flag of United States of America image

well .. which one do you want?

if you know the name of the file that you want, then just find it within the directories (findfirst/findnext) and compare the dates (comparedate or something similar from dateutils)
if you don't know the name of the file then you nees some constraints: you do the same findfirstfindnext but then you apply your restrictions (like size, attributes, etc)

if none of the above, then you need to give an actual example, since I don't quite undestand your request.
Avatar of diegomirner

ASKER

Ok, sorry i will explain you better:

Directory A: files and folder (40 GB) lets say 33040 files and folder + files wich are not in Directory B

Directory B : all most the same of Directory A but with newers version off the same files + new files

i need to feet all in jest one directory overwrite to the new version in the case of have the same files this folders , but the problem its i have to be really carefully because all this files are in the file server and im concern to take all the resources if i run a big process to do it , i will have a lot of user coming to kill me , jejej


thanksssssss
well ... let's see if I got it correctly:

copy over from B to A all files adn directories and overwrite only the files which in A are older than in B.
the result will be the one you want. right or wrong?
Yes perfect
well .. something like this should do it then:

function CopyDirectory(source,destination:string):boolean;
var r,s:TSearchRec;
begin
  result:=true;
  if source[length(source)]<>'\' then
    source:=source+'\';
  if destination[length(destination)]<>'\' then
    destination:=destination+'\';
  if findfirst(source+'*.*',faAnyFile {$WARNINGS OFF}
                                  -faVolumeID-faSymLink {$WARNINGS ON}
               ,r)=0 then
  begin
    repeat
      if (r.name<>'.') and (r.name<>'..') then
      begin
        if r.Attr and faDirectory = faDirectory then result:= result and copyDirectory(source+r.Name, destination+r.Name)
                                                else
        begin
          if fileexists(destination+r.name) then
          begin
            assert(findfirst(destination+r.name,faAnyFile,s)=0);// if this fails, big problem
            if r.Time>s.Time then//if source (r) is newer then destination(s)
              deletefile(destination+r.name);
            findclose(s);
          end;
          result:= result and CopyFile(pansichar(source+r.Name),pansichar(destination+r.name),false);
        end;
      end;
    until findnext(r)<>0;
    findclose(r);
  end;
end;


modified this code: https://www.experts-exchange.com/questions/21833058/Delete-a-folder-recursivly.html (accepted answer)

you should test this prior to using it as I am at work and don't have the time to set up a similar evironment.
The line I am not exactly sure about is this one:
            if r.Time>s.Time then//if source (r) is newer then destination(s)
since the times are integers and not tdatetimes; I do hope the comparison is done correctly.

if it is not correct, the replace it with a call to:
FileDateToDateTime
and then use
function CompareDate(const A, B: TDateTime): TValueRelationship;
to compare the dates
Thanks for your answer , but i dont unthertand all this  , i was lookng mach easy for me like using rsync , becouse all that you jest write its like chines forme .
Thanks
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok , thanks an im realy sorry
no problem :) it is a good idea to alwasy double check;)