Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Deleting a file ( Windows NT)

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
detempel
Asked:
detempel
  • 8
  • 5
  • 3
  • +1
1 Solution
 
EpsylonCommented:
Use the DeleteFile(filename) function....
0
 
detempelAuthor Commented:
That's the function I initially used and didn't work either. I tried 'deletefile' and 'erase' and both don't work.
0
 
EpsylonCommented:
Are you sure the file is deleted. Maybe the deletion fails.
0
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.

 
detempelAuthor Commented:
Apparently the file can't be deleted using the function 'deletfile' or 'erase'. That's why I asked this question.
0
 
MadshiCommented:
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
 
detempelAuthor Commented:
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
 
detempelAuthor Commented:
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
 
DraregCommented:
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
 
detempelAuthor Commented:
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
 
EpsylonCommented:
Try the SetFileTime function.....
0
 
MadshiCommented:
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
 
MadshiCommented:
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
 
detempelAuthor Commented:
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
 
EpsylonCommented:
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
 
detempelAuthor Commented:
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
 
EpsylonCommented:
> How do I pass the file to the function SetFileTime ?

See my previous comment...
0
 
detempelAuthor Commented:
Epsylon, Madshi and Drareq thanks for your replies !

Regards,
Erik Tempel.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 8
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now