?
Solved

Deleting a file ( Windows NT)

Posted on 2001-06-20
17
Medium Priority
?
199 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
Industry Leaders: 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 800 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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