[DUG] Renaming file in use
John Bird
johnkbird at paradise.net.nz
Fri Dec 2 14:54:13 NZDT 2005
Renaming files in use especially for programs is an area to avoid if
possible.
I have thought about this a lot and this is how I do it to be clean:
Program updates are loaded into a update folder on the server. Programs are
not run from there.
This folder also contains a list of the files that are part of the package.
This list can be updated with new releases of programs as new ones are
added.
The start up program is run from this update folder. It shows a splash
screen and also updates the programs if required from the update folder to
the program folder (it reads the list of files and checks the filedatetimes
of each in the update and the local program folders). The programs are
updated to a program folder on the local HD, then when done the main program
is executed from there, and all programs run locally after that. The splash
screen has a nice picture, and to the right of it a scrolling list of the
files being updated, so there is something nice for everyone to look at, its
neither just a static picture nor just technical lists - looks quite snazzy,
a bit like the splash screen for Acrobat reader.
Avoids updating anything that is in use, it is only in use after it has been
updated. There are a number of refinements included as well, such as
catering for differing file dates and time resolution between NTFS and FAT32
etc.
Happy to share the total code involved, its fairly simple, slick and fast.
Thanks to Russell B for some of the ideas used.
Here is a snippet of the main loop
frmJBUpdate.caption:='JBCL Getting Program Updates
'+Application.exename;
lstbxCopy.items.add(Application.exename);
lstbxCopy.setfocus;
if btnDone.visible = true then btnDone.setfocus;
for ptr:=0 to FileList.count-1 do
begin
SrcFile:=jkblJBCupdpathL+FileList[ptr];
DstFile:=jbclDirProg+FileList[ptr];
copystr:='Copying '+srcFile+' to '+DstFile;
WhichLater:=xfCopyFileIfLater(srcFile,dstFile);
if WhichLater=0 then status:=' OK' else
if WhichLater=1 then status:=' done' else
if WhichLater=2 then status:=' Later version already exists';
copystr:=copystr+status;
lstbxCopy.items.add(copystr);
lstbxCopy.TopIndex := lstbxCopy.Items.Count - 1;
application.processmessages;
end;
copystr:=jbclDirProg+'JBCLMenu.Exe';
lstbxCopy.items.add(' ');
lstbxCopy.items.add(copystr);
lstbxCopy.TopIndex := lstbxCopy.Items.Count - 1;
FileList.free;
lblDone.caption:='Updating done - starting menu';
application.processmessages;
if WaitatEnd=true then
begin
sleep(3000);
FinishandMenu;
end;
function xfCopyFileifLater(File1:string;File2:string):integer;
//returns 0=no copy needed (files same) 1=File1 copied 2=file2 is later so
not copied
var
WhichisLater:integer;
begin
WhichisLater:=xfFilesChanged(file1,file2);
if WhichisLater=1 then copyfile(Pchar(File1),Pchar(File2),false);
result:=Whichislater;
end;
function xfFilesChanged(file1:string;file2:string):integer;
//check 2 files modification date
//returns 0=both files same, 1=file1 later, 2=file2 later
//note if one file does not exist, returns other as later
var
fileage1,fileage2:integer;
dtdate1,dtdate2,dt10secs:TDateTime;
begin
result:=0;
fileage1:=FileAge(file1);
fileage2:=FileAge(file2);
if (fileage1<0) and (fileage2>0)then
begin
result:=2;
exit;
end;
if (fileage2<0) and (fileage1>0)then
begin
result:=1;
exit;
end;
if (fileage1<0) or (fileage2<0) then exit; //maybe both don't exist
dtdate1:=FileDatetoDateTime(fileage1);
dtdate2:=FileDatetoDateTime(fileage2);
if dtdate1=dtdate2 then
begin
result:=0;
exit;
end;
//now if not same, check if within 10 secs
//in case of different filesystems
dt10secs:=encodetime(0,0,10,0);
if (dtdate2>(dtdate1-dt10secs))
and (dtdate2<(dtdate1+dt10secs)) then
begin
result:=0;
exit;
end;
//still different
if dtdate1>dtdate2 then result:=1
else result:=2;
end;
John
-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] On
Behalf Of Robert martin
Sent: Friday, 2 December 2005 2:12 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Renaming file in use
This is no help but... How do you do it in Win2K and above?
Rob Martin
Software Engineer
phone +64 03 377 0495
fax +64 03 377 0496
web www.chreos.com
Wild Software Ltd
Jeremy Coulter wrote:
> Hi All. I am sure I have seen this mentioned before, but I has as part
> of an app. the ability to auto update. part of the update process is,
> it needs to rename the current running app. to .old.
> this this fin under win2K onwards, but on winnt and win98, it throws
> an error.
>
> Is there a work around? I did a search on Google, but not a lot came
> back....but that could have been how I was searching too.
>
> Jeremy
>
> --
> No virus found in this outgoing message.
> Checked by AVG Free Edition.
> Version: 7.1.362 / Virus Database: 267.13.10/190 - Release Date:
> 01/12/2005
>
>-----------------------------------------------------------------------
>-
>
>_______________________________________________
>Delphi mailing list
>Delphi at ns3.123.co.nz http://ns3.123.co.nz/mailman/listinfo/delphi
>
>
_______________________________________________
Delphi mailing list
Delphi at ns3.123.co.nz http://ns3.123.co.nz/mailman/listinfo/delphi
More information about the Delphi
mailing list