Recent

Author Topic: DeleteFile doesn't work  (Read 9567 times)

HermanR

  • New member
  • *
  • Posts: 9
DeleteFile doesn't work
« on: June 01, 2015, 07:57:11 pm »
Hello,

(Lazarus 1.4.0 / pfc 2.6.4 / Windows Embedded Handheld 6.5 Professional (CE OS 5.2.29153)
I have the following code:
<code>
  if SysUtils.FindFirst(fLogDir + '*.*', faAnyFile and not faDirectory, sr) = 0 then
    repeat
      FileAge(fLogDir + sr.Name, dt);
      if DaysBetween(Now, dt) > AAge then
      begin
        DeleteFile(PWideChar(fLogDir + sr.Name));
        --log fLogDir + sr.Name --here
      end;
    until SysUtils.FindNext(sr) <> 0;
  SysUtils.FindClose(sr);
</code>

In the log line al file older the 10 days are shown, but none is deleted. When i use FileExists, the file exists. I can delete the files manualy
I read and tested UTFToAnsi conversion, that didn't help.
can anyone help me on this?

Thanks in advance
Herman

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: DeleteFile doesn't work
« Reply #1 on: June 01, 2015, 08:31:43 pm »
What version (from what unit) of DeleteFile are you using?
(not http://www.freepascal.org/docs-html/rtl/sysutils/deletefile.html because that one just needs a string)

Are you sure you need to supply a PWideChar to that function?

And shouldn't you first convert "fLogDir + sr.Name" to widechar before casting it to a PWideChar?

Does this not work?
Code: [Select]
DeleteFile(fLogDir + sr.Name);


(Ps. Please use the code-tags around code in your posts. It read much easier)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: DeleteFile doesn't work
« Reply #2 on: June 01, 2015, 08:53:12 pm »
For the Windows version try Windows.Deletefile()

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: DeleteFile doesn't work
« Reply #3 on: June 01, 2015, 09:16:00 pm »
And please note that the Windows.DeleteFile() is linked with the DeleteFileA Windows-api. So it needs a PChar (AnsiChar) and not a PWideChar.

If you have unicode-characters in your filename you'll need to use FileUtil.DeleteFileUtf8();

rx3.fireproof

  • New Member
  • *
  • Posts: 49
Re: DeleteFile doesn't work
« Reply #4 on: June 01, 2015, 09:32:06 pm »
Hello

Yesterday I fought with a similar problem after upgrading to Lazarus 1.4.0. I have the names of files and folders can be Cyrillic.  I solved the problem after using the functions with utf8.

Example

var
myxml :TXMLConfig;
begin
myxml:=Txmlconfig.Create(systoutf8(ExtractFilePath(Application.exeName))+'myfile.xml');
end;

or

begin
if  savedialog1.Execute then begin
  if FileExistsUTF8(SaveDialog1.FileName) then DeleteFileUTF8(savedialog1.FileName);
end;

In version 1.2.6 I have worked functions without utf8.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: DeleteFile doesn't work
« Reply #5 on: June 01, 2015, 10:08:57 pm »
And please note that the Windows.DeleteFile() is linked with the DeleteFileA Windows-api.

There is no DeleteFileA  API on wince, and WinCE defines FPC_UNICODE_OS, so Windows unit is compiled with -W by default.

At least 3.x is. 2.x is too long ago for me:-)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: DeleteFile doesn't work
« Reply #6 on: June 01, 2015, 10:59:20 pm »
And please note that the Windows.DeleteFile() is linked with the DeleteFileA Windows-api.

There is no DeleteFileA  API on wince, and WinCE defines FPC_UNICODE_OS, so Windows unit is compiled with -W by default.

Ok... If that's the case the string would first need to be converted to Widestring.

So this should do the trick:
Code: [Select]
DeleteFile(PWideChar(UTF8Decode(fLogDir + sr.Name)));
(UTF8Decode for converting the UTF8 to Unicodestring/Widestring)

(Or is the string-type in lazarus/wince also already Unicode ???)

HermanR

  • New member
  • *
  • Posts: 9
Re: DeleteFile doesn't work
« Reply #7 on: June 02, 2015, 12:02:06 pm »
SOLVED

fLogDir = \My Documents\Log\
sr = TSearchRec

I tested all you guys adviced and reduced the solution to the next line.

DeleteFile(PWideChar(WideString(fLogDir + sr.Name)));

The conversion to WideString before making the pointer did the trick

Thanks all
Herman

 

TinyPortal © 2005-2018