Solved

Deleting a file ( Windows NT)

Posted on 2001-06-20
17
181 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now