how to delete a file? lazarus / 39-Graphics ---------------------------------------------------------------------- klausbits · 2016-08-05 08:52 Hi, I want to save a large amount of pictures like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for n=0 to nmax-1 dobegin    Str(n:4,nstring);    fbstring:=files+'b'+nstring+'.jpg';    picture2[n].SaveToFile(fbstring);... but it does not overwrite the images which exist already. Therefore I wrote Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Uses sysutils;    Str(n:4,nstring);    fbstring:=files+'b'+nstring+'.jpg';    DeleteFile(fbstring);    picture2[n].SaveToFile(fbstring); But DeleteFile wants as argument a UnicodeString. So I changed it in Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uses sysutils;var ufbsring: UnicodeString;     Str(n:4,nstring);    fbstring:=files+'b'+nstring+'.jpg';    ufbstring:=fbstring;    DeleteFile(ufbstring);    picture2[n].SaveToFile(fbstring); But in vain: the old images remain. Even when I write Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uses sysutils;var ufbsring: UnicodeString;     Str(n:4,nstring);    fbstring:=files+'b'+nstring+'.jpg';    ufbstring:=fbstring;    if DeleteFile(ufbstring) then writeln(fbstring,' deleted');    picture2[n].SaveToFile(fbstring); it writes ' ...deleted' but it is a lie. What can I do? ---------------------------------------------------------------------- howardpc · 2016-08-05 09:40 It is always best when dealing with files to use only filenames the system recognizes (and not assume a filename you construct represents a file the system can find). So check if the file you think exists is really there. I would use the LazFileUtils unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses LazFileUtils;var  ns: unicodestring;begin  Str(n,ns);  ns:=files+'b'+ns+'.jpg';  if FileExists(ns) then    begin      if DeleteFile(ns) then writeln(ns,' deleted')      else WriteLn(ns,' not deleted');    end  else writeln(ns,' does not exist');  picture2[n].SaveToFile(ns);         ... ---------------------------------------------------------------------- klausbits · 2016-08-05 10:32 to howardpc: many thanks for your explicite program piece. I used it (with exception n:4 to reach the old files). But the result ist the same as before. It writes d\multifilter\b   0.jpg deleted etc. for all 420 files. But, when I look into the file system, there are only the files from yesterday and no new ones. So I have to delete the files manually every time before I make a new test run. I suspect that with the delete-function there is something wrong. ---------------------------------------------------------------------- klausbits · 2016-08-05 11:49 In the LazFileUtils there is a function deletefileUTF8. I tried to run the program with this file. Again the output reads ... d:\multifilter\b 318.jpg deleted etc. but the files remain and were not deleted. ---------------------------------------------------------------------- Bart · 2016-08-05 12:04 Without the full code (not just a snippet) there is not much we can do. Break up the problem into smaller parts. Write a new test program, discarding all the other code you have. First of all, just write a test program that deletes the files. Harcode the filenames. If that succeeds, then try the algorithm to get the filenames and try to delete the files. All the time do not run the code that writes the new images! In every step use as much debug output (like HowardPC did) as possible. If any of that fails, post the full program code. Use fully qualified filenames (that is drive:\full\path\to\filename), at all time. Use ExpandFilename function for that. It may be the case that the program writes/deletes files in another directory then where you are looking. (The mere fact that you find old files kind of suggests this to me) Q: The location of the files is on a local disk, or is it a mounted network share on a server or e.g. a SAMBA share that has been assigne a drive letter? B.t.w. I would have chosen to have the numbers in the filename like b0012.jpg (left pad with zero's, not spaces), for which you can use the Format() function. Bart ---------------------------------------------------------------------- shobits1 · 2016-08-05 13:31 Quote from: klausbits on August 05, 2016, 10:52:25 amHi, I want to save a large amount of pictures like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for n=0 to nmax-1 dobegin    Str(n:4,nstring);    fbstring:=files+'b'+nstring+'.jpg';    picture2[n].SaveToFile(fbstring);... but it does not overwrite the images which exist already. Therefore I wrote Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uses sysutils;var ufbsring: UnicodeString;     Str(n:4,nstring);    fbstring:=files+'b'+nstring+'.jpg';    ufbstring:=fbstring;    DeleteFile(ufbstring);    picture2[n].SaveToFile(fbstring); But in vain: the old images remain. Even when I write just tried the (TPicture).SaveToFile function it works just fine and it overwrite any existing file (throws an exception when destination file is readonly); this fact combined with DeleteFile (not working for you)... it draws the same assumption as Bart's, so you clearly have a problem with file path. BTW: what's the type of files. ---------------------------------------------------------------------- klausbits · 2016-08-05 20:37 I am trying to write a small test program... files:=d:\multifilter\ as I showed already. So you can see, it is on my second hard drive. Also I found out that d:\multifilter is readonly. When I change that, it will be readonly again when I look into it next time. Is there a remedy? ---------------------------------------------------------------------- Bart · 2016-08-05 20:51 AFAIK the readonly attribute of a folder does not apply to it's contents. Anyhow DeleteFile should return False upon failure. To get all the filenames: Open a console (Start->cmd) then Code: [Select]d: cd \multifilter dir *.jpg /b > files.txt This will create a text file with just the filenames in it. For your test you can use something like this to get all filenames you can do something like this: (Untested) Code: [Select]uses classes, sysutils; var   SL: TStringList;   i: Integer;   Fn: String;   Res: Boolean; const   Path = 'd:\multifilter\'; begin   SL := TStringList.Create;   SL.LoadFromFile(Path+'files.txt');   for i := 0 to SL.Count - 1 do   begin     Fn := Path + SL.Strings[i];     Res := DeleteFile(Fn);     writeln('Fn: ',Fn,' DeleteFile = ',Res);   end;   SL.Free; end. Bart ---------------------------------------------------------------------- klausbits · 2016-08-05 21:15 ah, nice method to delete files ;-) better, I do it by hand, because there are files which should not be deleted. I also tested the WindowsDeleteFile() with the same negative result. Anyway, it is late in Germany and I have to go to bed... ---------------------------------------------------------------------- Bart · 2016-08-05 21:43 Quote from: klausbits on August 05, 2016, 11:15:01 pmah, nice method to delete files ;-) better, I do it by hand, because there are files which should not be deleted. You can edit the files.txt file by hand and remove anything that must stay. Anyway, you better make a backup of the files, so you can put them back for further testing! Quote from: klausbits on August 05, 2016, 11:15:01 pmI also tested the WindowsDeleteFile() with the same negative result. Not really surprising since DeleteFile will eventually call: Code: [Select]Function DeleteFile (Const FileName : UnicodeString) : Boolean; begin   Result:=Windows.DeleteFileW(PWidechar(FileName)); end; Quote from: klausbits on August 05, 2016, 11:15:01 pmAnyway, it is late in Germany and I have to go to bed... Same time here in the Netherlands  Bart ---------------------------------------------------------------------- User137 · 2016-08-06 14:45 As shobits1 said, savefile overwrites. You don't need to delete the file as that is extra work. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var p: TPicture;    s: string;begin  s:='D:\pascal\test\NewVeryLongFilename.jpg';  p:=TPicture.Create;  p.LoadFromFile('D:\pascal\test\60x60pixels.jpg');  p.SaveToFile(s);  p.LoadFromFile('D:\pascal\test\100x100pixels.jpg');  p.SaveToFile(s);  p.Free;end;After this code the new jpg is 100x100 as expected. If it's filename problem, make it print the file name in a TMemo for example. If it's attribute problem, set all existing jpg's to non-readonly. ---------------------------------------------------------------------- klausbits · 2016-08-06 21:06 nice that we are neighbours ;-) I checked the readonly property of the files. The directory d:\multifilter was write-protected (I could not change this) but all files in it could be manipulated ad libitum. So I checked if with inclusion of Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i deletefile(d:\multifilter\b0.jpg); at the beginning of the program b0.jpg was deleted, ok. Also Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i files:='d:\multifilter\');deletefile(files+'b1.jpg'); worked. Also Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   files:='d:\multifilter\');  n:=2;  Str(n,nstring);  fbstring:=files+'b'+nstring+'.jpg';  deletefile(fbstring); worked! Also Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for n:=0 to nmax-1 do  begin  Str(n,nstring);  fbstring:=files+'b'+nstring+'.jpg';  deletefile(fbstring);  end; worked!!! But the deletefile() directly before the savefile() (as discussed before) later on in the program does not work. Very rare! (The savefile() alone did not overwrite the old files - this was the cause of the whole problem). So, I put the deletes at the first possible place (when it is clear if and how many files have to be deleted). And it works! Finally - many thanks to all who helped me to get on the right way!