求救.一个cmd line小程序.
早年学过一点 pascal, 现在都不记得了.
现在想要一个小东西. 就是比较两个目录,
如果文件存在于目标目录并且size相同,而且修改时间相同,则跳过,否则拷贝.
如果目标目录文件不存在于源目录,则删除.
这样可以方便备份工作.
写了一点,写不下去了, 毕竟不熟悉.
program checkout;
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
SysUtils;
// isDir
function isDir(SearchRec:TSearchRec):Boolean;
begin
Result:= (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..');
end;
// check file
procedure checkfile(sFile:String;pFile:String);
begin
if not FileExists(pFile) then
begin
copyFile(pchar(sFile),pchar(pFile),true);
writeln("Copy ",sFile,' to ',pFile);
end;
end;
// checkout
procedure checkouter(sPath:String ; dPath:String);
var
SearchRec:TSearchRec;
subdir:TStrings;
i:integer;
begin
subdir:=TStringList.Create;
sPath := IncludeTrailingPathDelimiter(sPath);
dPath := IncludeTrailingPathDelimiter(dPath);
if (FindFirst( sPath + '*.*', faDirectory, SearchRec)=0) then
begin
if isDir(SearchRec) then
begin
subdir.Add(SearchRec.Name);
end;
while FindNext(SearchRec) = 0 do
begin
if isDir(SearchRec) then
begin
subdir.Add(SearchRec.Name);
end;
end;
end;
FindClose(SearchRec);
FindFirst(sPath + '*.*',faAnyFile - faDirectory,SearchRec);
if(not isDir(SearchRec))then checkfile(sPath + SearchRec.Name,dPath + SearchRec.Name);
while FindNext(SearchRec)=0 do checkfile(sPath + SearchRec.Name,dPath + SearchRec.Name);
FindClose(SearchRec);
for i:=0 to subdir.Count-1 do
begin
checkouter(sPath + subdir.Strings[i]+ '\', dPath + subdir.Strings[i] + '\');
end;
subdir.Free;
end;
begin
writeln;
if ParamCount < 2 then
begin
writeln(' Useage: checkout.exe source destination [/q]');
Halt;
end;
if not DirectoryExists(ParamStr(1)) then
begin
writeln('Source directory ',ParamStr(1),' is not a valid path!');
Halt;
end;
if not DirectoryExists(ParamStr(2)){ and not CreateDirectory(pchar(ParamStr(2))) }then
begin
writeln('Destination directory ',ParamStr(2),' is not a valid path!');
Halt;
end;
// check out
checkouter(ParamStr(1),ParamStr(2));
//DeleteFile
//removeDir
//rmDir
//DirectoryExists
//CreateDirectory
//FileAge 函数 返回文件已存在的时间
//FileGetDate 函数 返回文件的DOS日期时间标记
//FileSize 函数 返回当前文件的大小
{
GetFileAttributes 返回文件属性
GetFileInformationByHandle 返回文件信息
GetFileSecurity 获取文件或目录安全信息
GetFileSize 返回指定文件大小
GetFileTime 返回文件64位时间
GetFileTitle 返回文件名
GetFileType 返回文件类型
GetFileVersionInfo 返回文件的版本信息
GetFileVersionInfoSize 返回可用版本信息大小
}
Halt;
end.
有大侠如果能帮助完成,分可再开帖加,加几百都不成问题. 多谢了.