Solved

Deleting a file ( Windows NT)

Posted on 2001-06-20
17
191 Views
Last Modified: 2010-04-06
I can't seem to be able to delete a file when running Windows NT. I've made a program with Delphi 3.01 running  Windows 98. When I delete and recreate a file the creation date changes to the system date. But when I run the same program running Windows NT the creation date doesn't change as if the file wasn't deleted.

Here's the codesnippet I use to test it:

procedure TForm1.Button1Click(Sender: TObject);
var Debugger,SaveSet : TextFile;
  SaveFile         : String;
  FileInfo         : TSearchRec;
  SysDatTijd       : TSystemTime;
  Gevonden         : Integer;

begin
  AssignFile(Debugger,'c:\debug2.txt');
  Rewrite(Debugger);

  SaveFile := FileListBox1.FileName;
  Gevonden := FindFirst(SaveFile,faArchive,FileInfo);

  FileTimeToSystemTime(FileInfo.FindData.ftCreationTime, SysDatTijd);
  FindClose(FileInfo);
  { Controleer of de SaveSet eerst gewist moet worden }
  { Vergelijk de systeemdatum met de creatiedatum van SaveFile }

  writeln(Debugger,'Systeemdatum: ',FormatDateTime('yyyymmdd',Now));
  writeln(Debugger,'Creatiedatum van ',FileInfo.Name,': ',FormatDateTime('yyyymmdd',SystemTimetoDateTime(SysdatTijd)));

  if ( FormatDateTime('yyyymmdd',Now) = FormatDateTime('yyyymmdd', SystemTimetoDateTime(SysdatTijd)) )
  then begin
    { Open de SaveSet voor vandaag }
    AssignFile(SaveSet,SaveFile);
    Append(SaveSet)
  end
  else begin

    writeln(Debugger,'Wissen maar ?');

    { Echt fysiek verwijderen zodat de CreatieDatum wordt gewijzigd }

    { DeleteFile(SaveFile); }

    AssignFile(SaveSet, SaveFile);
    Reset(SaveSet);
    CloseFile(SaveSet);
    Erase(SaveSet);

    { Open de SaveSet voor vandaag }

    { AssignFile(SaveSet, SaveFile); }

    { Opnieuw aanmaken SaveSet }
    Rewrite(SaveSet)
  end ;

  Writeln(Debugger);
  CloseFile(Debugger);

end;

Regards,
Erik Tempel.
0
Comment
Question by:detempel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 3
  • +1
17 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6211446
Use the DeleteFile(filename) function....
0
 

Author Comment

by:detempel
ID: 6211505
That's the function I initially used and didn't work either. I tried 'deletefile' and 'erase' and both don't work.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6211707
Are you sure the file is deleted. Maybe the deletion fails.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:detempel
ID: 6211745
Apparently the file can't be deleted using the function 'deletfile' or 'erase'. That's why I asked this question.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6211782
DeleteFile works fine normally, but only if the file is not open. The line where you commented DeleteFile out is too early, because at that location the file is still open. That doesn't work...

Regards, Madshi.
0
 

Author Comment

by:detempel
ID: 6211817
Why is the file still open at that moment the 'assignfile' is executed in the 'then'-branche or do you mean that the command 'findfirst' opens the file ? And the original code with 'deletefile' does work when running Windows 98.
0
 

Author Comment

by:detempel
ID: 6211971
I have reduced the problem to this codesnippet. I have tried this with Delphi 5 on Windows NT and it still doesn't work :-(

That is the file holds the same creation date and creation time as before the deletefile-command.

*********************************************************
procedure TForm1.Button1Click(Sender: TObject);
var  Oops  : TextFile;
begin

  if not DeleteFile('c:\myfiles\test.txt') then
  ShowMessage('Not Deleted');

  AssignFile(Oops, 'c:\myfiles\test.txt');
  Rewrite(Oops);
  CloseFile(Oops)

end;
*********************************************************

Regards,
Erik.
0
 
LVL 2

Expert Comment

by:Drareg
ID: 6212029
The function DeleteFile  will return False ( will not erase the file ) if the file ( path ) do not exist or the file is read only.
0
 

Author Comment

by:detempel
ID: 6212055
The file exists and is not read only.

Further testing learns that even only the rewrite which claims to delete the file first doesn't change the time of creation.

I hope this is not a bug.

Maybe there's a workaround to set the creation date and time of the file.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6212077
Try the SetFileTime function.....
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6212122
Does it show "Not deleted"? If yes: Please tell us the result of this:

if not DeleteFile('c:\myfiles\test.txt') then
  ShowMessage(IntToStr(GetLastError));

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6212128
P.S: Maybe you don't have enough rights to delete a file in that directory? Just guessing... I'm not that pro in NT security...
0
 

Author Comment

by:detempel
ID: 6212158
It doesn't show 'Not deleted' ... :-(

I've split the 'deletefile' and 'rewrite' using two buttons. When I push the first button ('deletefile') I see the file disappear in the explorer. When I push the second button ('rewrite') I see the file reappear. When I check the creation date and time it didn't change ... :-(

I can visualy delete the file in the explorer but I'm not sure if he 'really' deletes the file. I'm not an NT-expert too but I have all rights as administrator (I hope).
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 6212172
Try this to change the creation date:


function FileSetCreationDate(Handle: Integer; Age: Integer): Integer;
var
  LocalFileTime, FileTime: TFileTime;
begin
  Result := 0;
  if DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime) and
    LocalFileTimeToFileTime(LocalFileTime, FileTime) and
    SetFileTime(Handle, @FileTime, @FileTime, @FileTime) then Exit;
  Result := GetLastError;
end;

procedure TForm1.Button1Click(Sender: TObject);
var f: Integer;
begin
  f := FileOpen('c:\myfiles\test.txt', fmOpenReadWrite);
  FileSetCreationDate(f, DateTimeToFileDate(Now));
  FileClose(f);
end;
0
 

Author Comment

by:detempel
ID: 6212184
How do I pass the file to the function SetFileTime ?

I've constructed this:

procedure TForm1.Button2Click(Sender: TObject);
var  Oops  : TextFile;
     NewTime : SystemTime;
begin
  AssignFile(Oops, 'c:\myfiles\test.txt');
  Rewrite(Oops);

  GetSystemTime(NewTime);

  SetFileTime(Oops,NewTime,NewTime,NewTime)

  CloseFile(Oops);
end;

But SetFileTime doesn't accept 'Oops' it says 'Incompatible types 'Cardinal and 'Text'.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6212197
> How do I pass the file to the function SetFileTime ?

See my previous comment...
0
 

Author Comment

by:detempel
ID: 6212272
Epsylon, Madshi and Drareq thanks for your replies !

Regards,
Erik Tempel.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 184
TServerSocket - file via sendbuf or Text via sendtext ? 1 91
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 100
CheckListBox usage 3 97
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question