Autor Beitrag
smepal
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP, Linux Mandbrake,SUSE 10.1, SUSE 9 PE, DEBIAN, Win 2000 Pro, Win XP Pro
Delphi
BeitragVerfasst: Mi 30.04.08 15:18 
Hallo,
ich schreibe gerade ein kleines Tool zum abgleichen eines Ordners auf dem PC mit einem auf dem Server.
Funktioniert auch bis jetzt alles wie gewünscht.
Stehe jetzt vor dem Problem, dass ich das Datum der letzten Bearbeitung der Datei auf dem Server ermitteln muss.
Wenn ich sie erst kopiere, dann wird das ja auf den Momentanen Zeitpunkt gesetzt! Genau das will ich nicht!

Hier mal der Code:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdExplicitTLSClientServerBase, IdFTP, CheckLst, FileCtrl;

type
  TForm1 = class(TForm)
    ftp: TIdFTP;
    syncbt: TButton;
    Start: TButton;
    chronik: TListBox;
    sync_dirs: TCheckListBox;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure syncbtClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure StartClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  data: Tstringlist;
  pcpath: String;
implementation

{$R *.dfm}

function GetFileLastAccessTime(Path: string): TDateTime;
var
  hFile: THandle;
  rStructur: TWin32FindData;
  rFileTime: TFileTime;
  dwLastAccess: cardinal;

begin
  Result := 0;
  hFile := Windows.FindFirstFile(pchar(Path), rStructur);
  if INVALID_HANDLE_VALUE <> hFile
  then begin
    Windows.FindClose(hFile);
    { FILETIME in lokales FILETIME-Format konvertieren }
    FileTimeToLocalFileTime(rStructur.ftLastAccessTime, rFileTime);
    { lokales FILETIME-Format ins DOS-Format konvertieren }
    FileTimeToDosDateTime(rFileTime, LongRec(dwLastAccess).Hi, LongRec(dwLastAccess).Lo);
    { DOS-Format in ein Delphi-Format konvertieren }
    Result := FileDateToDateTime(dwLastAccess);
  end;

end{function GetFileLastAccessDate() ...}

procedure getfilesinserverdir(adir,async_dir: String; aftp: Tidftp; Alist, Alistcl: TStringlist; Achronik: TListbox);
var e: Integer;
aseclist: TStringlist;
begin
    aseclist := TStringlist.Create;
    aftp.list(alist,'',false);
    achronik.Items.add('ServerlistCL befüllen');

    for e:=0 to alist.Count-1 do
      begin
      if ((alist[e] <> '.'and (alist[e] <> '..')) then
        begin
        if ansipos('.',alist[e])=0 then
          begin
          aftp.ChangeDir(alist[e]);
          getfilesinserverdir(adir+alist[e]+'/',async_dir,aftp,aseclist,alistcl,achronik);
          if not (directoryexists(pcpath+'/'+async_dir+'/'+ adir+alist[e]+'/')) then forcedirectories(pcpath+'/'+async_dir+'/'+adir+ alist[e]+'/');
          aftp.ChangeDirUp;
          end
          else
          begin
          if not (adir = ''then
            begin
            alistcl.Add(adir+alist[e]);
            achronik.Items.add('      ServerlistCL hinzufügen: '+adir+ alist[e]);
            end
          else
            begin
            alistcl.Add(alist[e]);
            achronik.Items.add('      ServerlistCL hinzufügen: '+ alist[e]);
            end;
          end;
        end;
      end;
end;

procedure GetFilesInDirectory(ADirectory,ADir: string; AMask: String; AList: TStringlist; ARekursiv: Boolean);
var
  SR: TSearchRec;
begin
  if (ADirectory<>''and (ADirectory[length(ADirectory)]<>'/'then
    ADirectory:=ADirectory+'/';

  if (FindFirst(ADirectory+AMask,faAnyFile-faDirectory,SR)=0then begin
    repeat
      if (SR.Name<>'.'and (SR.Name<>'..'and (SR.Attr<>faDirectory) then
        begin
        Alist.Add(ADir+SR.Name);
        end;
    until FindNext(SR)<>0;
    FindClose(SR);
  end;

  if ARekursiv then
    if (FindFirst(ADirectory+'*.*',faDirectory,SR)=0then
    begin
      repeat
        if (SR.Name<>'.'and (SR.Name<>'..'then
          GetFilesInDirectory(ADirectory+SR.Name,adir+SR.Name+'/',AMask,AList,True);
      until FindNext(SR)<>0;
      FindClose(SR);
    end;
end;

procedure TForm1.StartClick(Sender: TObject);
var i: Integer;
begin
data := Tstringlist.Create;
ftp.Connect;
chronik.Items.add('Verbunden mit '+ftp.Host+':'+ftp.Username+':**********');
ftp.ChangeDir('htdocs/downs/');
chronik.items.add('Ordner gewechselt: "htdocs/downs/"');
ftp.list(data,'',false);
for i:=0 to data.Count-1 do
begin
if ((data[i] <> '.'and (data[i] <> '..'and (ansipos('.',data[i])=0)) then
sync_dirs.Items.add(data[i]);
end;
chronik.Items.Add('Ordnerliste geladen!');

data.Free;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ftp.Disconnect;
end;

procedure TForm1.syncbtClick(Sender: TObject);
var
i,e, v,r,p: Integer;
serverlist, serverlistcl, pclist, pclistcl: TStringlist;
f : TSearchRec;
s : Integer;
within: Boolean;
begin
serverlist:= Tstringlist.Create;
chronik.Items.add('Serverlist erstellt!');
pclist:= Tstringlist.Create;
chronik.Items.add('PClist erstellt!');
serverlistcl:= Tstringlist.Create;
chronik.Items.add('ServerlistCL erstellt!');
pclistcl:= Tstringlist.Create;
chronik.Items.add('PClistCL erstellt!');
for i:= 0 to sync_dirs.Count-1 do
  begin
  if sync_dirs.Checked[i] then
    begin
//----------Laden der beiden Dateilisten----------------//
    ftp.ChangeDir(sync_dirs.Items[i]);
    chronik.Items.add('Ordner: '+ sync_dirs.Items[i]);
    getfilesinserverdir('',sync_dirs.Items[i],ftp,serverlist,serverlistcl,chronik);

//----------HIER MUSS PCLIST NOCH EINGELESEN WERDEN!!!!!!!!!!!!!!!!!---//
      if not (directoryexists(pcpath+'/'+sync_dirs.Items[i])) then forcedirectories(pcpath+'/'+sync_dirs.Items[i]);
      GetFilesInDirectory(pcpath+'/'+ sync_dirs.Items[i],'','*.*',pclist,true);
     chronik.Items.add('PClistCL befüllen');
     for e:=0 to pclist.Count-1 do
      begin
      if ((pclist[e] <> '.'and (pclist[e] <> '..')) then
        begin
        pclistcl.Add(pclist[e]);
        chronik.Items.add('      PClistCL hinzufügen: '+ pclist[e]);
        end;
      end;
//Vergleichen der PC mit der Serverliste und holen fehlender Dateien//
    chronik.Items.add('Start Vergleich');
    for v:=0 to serverlistcl.Count-1 do
      begin
      r :=0;
      within:=false;
      if pclistcl.Count<>0 then
        begin
        repeat
          begin
          if serverlistcl[v] <> pclistcl[r] then within:= false else within := true;
          chronik.Items.Add('         Vergleiche: '+serverlistcl[v]+' mit ' + pclistcl[r]);
          P:=r;
          inc(r);
          end;
        until
          (within =true) OR (pclistcl.Count = r);
        end
        else
        within := false;

      if within = false then
        begin
        ftp.Get(serverlistcl[v],pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v]);
        chronik.Items.add('Kopiere: '+serverlistcl[v]);
        end
      else
        begin
        ftp.Get(serverlistcl[v],pcpath+'/'+'Temp_Sync/'+serverlistcl[v],false);
        if (getfilelastaccesstime(pcpath+'/'+'Temp_Sync/'+serverlistcl[v]) < getfilelastaccesstime(pcpath+'/'+serverlistcl[v]))then
          begin
          chronik.Items.Add('              Serverversion ist neuer!');
          deletefile(pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v]);
          ftp.Get(serverlistcl[v],pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v]);
          chronik.Items.add('Kopiere: '+serverlistcl[v]);
          end;
          deletefile(pcpath+'/'+'Temp_Sync/'+serverlistcl[v]);
        end;
      end;
    chronik.Items.add('Ende Vergleich');
    serverlist.Clear;
    pclist.clear;
    pclistcl.Clear;
    serverlistcl.Clear;
    ftp.ChangeDirUp;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
pcpath:= inputbox('Pfad zur Datenbank auf deinem PC','Hier bitte den Pfad zu dem Ordner angeben, in dem die Datenbank auf deinem PC gespeichert werden soll!','c:/KLJB-Bechen/MDb');

end;

procedure TForm1.FormActivate(Sender: TObject);
begin
forcedirectories(pcpath);
end;

end.


Genauer geht es um diesen Teil:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
      else
        begin
        ftp.Get(serverlistcl[v],pcpath+'/'+'Temp_Sync/'+serverlistcl[v],false);
        if (getfilelastaccesstime(pcpath+'/'+'Temp_Sync/'+serverlistcl[v]) < getfilelastaccesstime(pcpath+'/'+serverlistcl[v]))then
          begin
          chronik.Items.Add('              Serverversion ist neuer!');
          deletefile(pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v]);
          ftp.Get(serverlistcl[v],pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v]);
          chronik.Items.add('Kopiere: '+serverlistcl[v]);
          end;
          deletefile(pcpath+'/'+'Temp_Sync/'+serverlistcl[v]);
        end;
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Mi 30.04.08 15:51 
Theoretisch per FTP-Datum, welches im Dateilisting steht.

Praktisch gar nicht. Da Server und Client nicht unbedingt in der gleichen Zeitzone sind, und selbst wenn sie es sind, nicht unbedingt die Uhren gleich gehen, weiß man das nie so genau, was jetzt neuer ist.
Ich hab das bei FTPVer so gemacht, dass ich direkt nach dem Hochladen ein Listing hole, und dieses Datum als Vergleichswert lokal speichere. Dagegen kann ich dann das neue Listing vergleichen, sprich ich vergleiche Daten aus der gleiche Quelle.

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
smepal Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP, Linux Mandbrake,SUSE 10.1, SUSE 9 PE, DEBIAN, Win 2000 Pro, Win XP Pro
Delphi
BeitragVerfasst: Mi 30.04.08 16:51 
Dann werde ich wohl zusätzlich ein ini-File auf dem Server hinterlegen, in welchem die Erstellungsdaten stehen, da bei meinem Tool nachher von mehreren PCs auf den Server zugegriffen werden soll!
smepal Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP, Linux Mandbrake,SUSE 10.1, SUSE 9 PE, DEBIAN, Win 2000 Pro, Win XP Pro
Delphi
BeitragVerfasst: Fr 02.05.08 16:46 
Stehe da noch vor einem kleinen Problem:
Arbeite mit dieser Funktion:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
function GetFileLastAccessTime(Path: string): TDateTime;
var
  hFile: THandle;
  rStructur: TWin32FindData;
  rFileTime: TFileTime;
  dwLastAccess: cardinal;

begin
  Result := 0;
  hFile := Windows.FindFirstFile(pchar(Path), rStructur);
  if INVALID_HANDLE_VALUE <> hFile
  then begin
    Windows.FindClose(hFile);
    { FILETIME in lokales FILETIME-Format konvertieren }
    FileTimeToLocalFileTime(rStructur.ftLastAccessTime, rFileTime);
    { lokales FILETIME-Format ins DOS-Format konvertieren }
    FileTimeToDosDateTime(rFileTime, LongRec(dwLastAccess).Hi, LongRec(dwLastAccess).Lo);
    { DOS-Format in ein Delphi-Format konvertieren }
    Result := FileDateToDateTime(dwLastAccess);
  end;

end{function GetFileLastAccessDate() ...}


Und das ist die Problemstelle:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
if (GetFileLastAccessTime(pcpath+'/'+ serverlistcl[v]) < dates.ReadDateTime('Files',serverlistcl[v], strtodatetime('02.05.2008 00:00:00')))    then
          begin
          chronik.Items.Add('              Serverversion ist neuer!');
          chronik.Items.Add('              '+datetimetostr(GetFileLastAccessTime(pcpath+'/'+ serverlistcl[v]))+' < '+ datetimetostr(dates.ReadDateTime('Files',serverlistcl[v], strtodatetime('02.05.2008 00:00:00'))));
          deletefile(pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v]);
          ftp.Get(serverlistcl[v],pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v]);
          chronik.Items.add('Kopiere: '+serverlistcl[v]);
         end
        else if (GetFileLastAccessTime(pcpath+'/'+ serverlistcl[v]) > dates.ReadDateTime('Files',serverlistcl[v], strtodatetime('02.05.2008 00:00:00')))then
          begin
          chronik.Items.Add('              PCversion ist neuer!');
          dates.WriteDateTime('Files',serverlistcl[v], now);
          ftp.Delete(serverlistcl[v]);
          ftp.Put(pcpath+'/'+sync_dirs.Items[i]+'/'+ serverlistcl[v],serverlistcl[v],false);
          chronik.items.add('Lade hoch: '+serverlistcl[v]);
          end
        else
        chronik.items.add('Versionen sind gleich!');
        end;

Liefert als Ergebnis für getfilelastacesstim() immer nur das:

"30.12.1899"

Wodran liegt das ?
smepal Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP, Linux Mandbrake,SUSE 10.1, SUSE 9 PE, DEBIAN, Win 2000 Pro, Win XP Pro
Delphi
BeitragVerfasst: Do 08.05.08 15:43