Solved

Deleting a file ( Windows NT)

Posted on 2001-06-20
17
179 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

14 Experts available now in Live!

Get 1:1 Help Now