Lazarus
Free Pascal => Other OS => Topic started by: HermanR 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
-
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?
DeleteFile(fLogDir + sr.Name);
(Ps. Please use the code-tags around code in your posts. It read much easier)
-
For the Windows version try Windows.Deletefile()
-
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();
-
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.
-
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:-)
-
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:
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 ???)
-
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