<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[My Visual Database — An academic question on file deletions.]]></title>
		<link>https://myvisualdatabase.com/forum/viewtopic.php?id=6559</link>
		<atom:link href="https://myvisualdatabase.com/forum/extern.php?action=feed&amp;tid=6559&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in An academic question on file deletions..]]></description>
		<lastBuildDate>Sun, 14 Jun 2020 06:05:16 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: An academic question on file deletions.]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=36823#p36823</link>
			<description><![CDATA[<p>OK, for anyone who wants to be able to delete a folder and files contained within, here is the code I used to get it working. The difficulty was because my folders are 4 folders deep and RemoveDir will not delete folders that are not empty.</p><br /><p>Parameter &#039;F&#039; contains the file path when the function is called.</p><div class="codebox"><pre><code>function DeleteDB(f: string): boolean;
var
 fileDelete :TStringList;  //will contain the list of files in the folder
 filename : string;          // This will be assigned each file name found in the stringlist. 
 indx, cnt: integer;
begin

    result := True;
    fileDelete := TStringList.Create;
    fileDelete.Text := GetFilesList(f, &#039;*.db&#039;,True);  //finds all the *.db files in the filepath
    indx :=  fileDelete.Count -1;   // number of files in the folder

   for cnt := 0 to indx  do
   //with filedelete do    -- &#039;with&#039; will only work if folder is root. As we are 4 folders deep each file in filelist has to become individulised so we have to use a FOR loop

    begin
        filename := filedelete[cnt]; // take each file in the list and assign it to the string one file at a time
        DeleteFile(filename);  //delete the file
    end;

//Once the folder is empty  the directory/folder can now be deleted

    if DirectoryExists(f) then
    begin  
       result := RemoveDir(f);
         //showmessage(&#039;Z after delete &#039; + boolToStr(z));
    end
    else
    begin  //error message we should never get here 
        messageBox(&#039;Unable to delete file &#039;+ f, &#039;Delete File&#039;, 0);
        result := False;
    end;

   fileDelete.Free;  // release the stringlist from memory

end;</code></pre></div><p>The calling code</p><div class="codebox"><pre><code> dtToday := FormatDateTime(&#039;dd-mm-yyyy&#039;,now);
   dtYesterday := SQLExecute(&#039;SELECT dateBackup FROM backups WHERE id = 1&#039;); // note the last date of a backup could be stored in a text file instead of a table

y := ExtractFilePath(application.ExeName);  // y is declared as a string. This gets the folder path of wherever your executable is living

 y := y + &#039;backup\backup &#039; + dtYesterday; // filepath to executable + folder &#039;Backup&#039; + subfolder name = backup + date

if y &lt; b then    // b contains &#039;today&#039;s&#039; date so if the backup date in the database is less than today&#039;s date call the delete function
     DeleteDB(y);</code></pre></div><br /><p>Hope that is of some help to somebody</p>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Sun, 14 Jun 2020 06:05:16 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=36823#p36823</guid>
		</item>
		<item>
			<title><![CDATA[Re: An academic question on file deletions.]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=36822#p36822</link>
			<description><![CDATA[<p>An update.</p><br /><p>1. I have solved the date checking by having a backup table with the last date accessed. Read this on opening and then the last directory created can be found no matter how many days have elapsed since running the program.</p><br /><p>2. RemoveDir cannot delete a directory when the folder contains files.</p>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Sun, 14 Jun 2020 04:02:52 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=36822#p36822</guid>
		</item>
		<item>
			<title><![CDATA[Re: An academic question on file deletions.]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=36819#p36819</link>
			<description><![CDATA[<p>Sadly back to the drawing board!&nbsp; &nbsp;I recoded everything to create and delete folders and then after it was working perfectly, I realised it wasn&#039;t practical for weekends or bank holidays, as obviously there would be more than 1 days break.</p><br /><p>I think I&#039;ll have to go to counting files in the folder and deleting when files in the folder are greater than a number to be decided.</p><br /><p>It will give me something to ponder as I wander off to bed.</p><br /><p>The mere thought of bed has solved the problem - run a loop in the delete file function incrementing a days prior counter.</p><p>So first iteration is Day -1, second is Day-2, third is Day-3, etc.</p>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Sat, 13 Jun 2020 13:24:47 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=36819#p36819</guid>
		</item>
		<item>
			<title><![CDATA[Re: An academic question on file deletions.]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=36801#p36801</link>
			<description><![CDATA[<p>Good thinking, I&#039;ll give it a whirl! Thanks</p>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Fri, 12 Jun 2020 21:22:34 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=36801#p36801</guid>
		</item>
		<item>
			<title><![CDATA[Re: An academic question on file deletions.]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=36787#p36787</link>
			<description><![CDATA[<p>Hi Colin,<br />Not sure if I&#039;ve understood the question correctly, but couldn&#039;t you, instead of storing all your backups in a single backup folder, create a sub-folder within the backup folder and name each sub-folder with the date.&nbsp; Then you can simply use the &#039;createdir&#039; and &#039;removedir&#039; commands (along with any other stuff you want to do) to create and remove the appropriate sub-folders when your program starts and closes and circumvent the issue of the timestamps.<br />Please see the attached as a basic approach (obviously you&#039;ll want to change the directory paths).<br />Derek.</p>]]></description>
			<author><![CDATA[null@example.com (derek)]]></author>
			<pubDate>Fri, 12 Jun 2020 16:04:06 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=36787#p36787</guid>
		</item>
		<item>
			<title><![CDATA[An academic question on file deletions.]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=36779#p36779</link>
			<description><![CDATA[<p>I have managed to write some code that <strong>is successful</strong> in deleting backup files, but I had hoped to delete files to a pattern. In this I have failed. I had thought of using the TRIM function, but of course that won&#039;t help with the actual names that are on the disk. Perhaps the only answer is to make my own equivalent of the Delphi class TSearchRec which of course is not available in Pascal script.</p><br /><p>The files in the backup folder are named as so:</p><br /><div class="codebox"><pre><code>WOS_backup &#039;+ FormatDateTime(&#039;dd-mm-yyyy hh-nn-ss&#039;,now)+&#039;.db&#039;</code></pre></div><br /><p>The backups occur on opening the program, every 3.5 hours and on close.&nbsp; My thought was on opening the program a backup is made and then all the files with yesterdays date are deleted. In other words I need the file name to ignore the time content and delete the files with the day before&#039;s date.</p><br /><p>The code below does not delete any files in the directory. </p><div class="codebox"><pre><code>function DeleteDB(f: string): boolean;
var
    fileDelete : TStringList;
    fileName : string;
    cnt, indx: integer;
    Date : TDateTime;
begin

    result := True;
    fileDelete := TStringList.Create;
    fileDelete.Text := GetFilesList(f, &#039;*.db&#039;,False);

    cnt :=  fileDelete.Count -1;
    
    date := Now - 1;
    fileName := DateToStr(date);
    fileName := &#039;WOS_backup &#039;+ fileName + &#039;*.db&#039;;
    
   // Timer.Enabled := False;

    for   indx := 0 to cnt do
      if fileDelete[indx] = filename then  // If I delete this line then all files in the directory get deleted.
        begin
       // DeleteFile(fileDelete[indx]);
        if not DeleteFile(fileDelete[indx]) then
        begin
           messageBox(&#039;Unable to delete file &#039;+fileDelete[indx], &#039;Delete File&#039;, 0);
           result := False;
        end; 
     end;


   // Timer.Enabled := True;

   fileDelete.Free;</code></pre></div><br /><p>As noted above, if I remove the code segment <em><span style="color: blue">if fileDelete[indx] = filename</span></em>, then it does delete files.&nbsp; So from a purely academic viewpoint does anyone have any thoughts on a way to ignore the time stamp in the file name?</p><p>Thank you in advance for any thoughts.</p>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Fri, 12 Jun 2020 09:49:47 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=36779#p36779</guid>
		</item>
	</channel>
</rss>
