Alaska Software Inc. - How to delete files on folder based on date
Username: Password:
AuthorTopic: How to delete files on folder based on date
Juan GonzalezHow to delete files on folder based on date
on Fri, 25 Nov 2022 17:52:35 +0100
Hello,

I am in the need to periodically delete files accumulated on a folder, based on 
their date.

For example:
The folder D:\FOLDER\ has several hundreds of files

I want to delete all of them whose date is OLDER than 10 days.

Is that possible with a single Xpp instruction? or I will have to traverse the 
folder(s), obtaining every file´s date/time, and then decide to delete it each 
by each ?

I was also wondering if an external utility might exist with this 
functionality...

Thank you !
Thomas BraunRe: How to delete files on folder based on date
on Tue, 29 Nov 2022 16:46:14 +0100
Hi Juan,


> Is that possible with a single Xpp instruction? or I will have to traverse the 
> folder(s), obtaining every file´s date/time, and then decide to delete it each 
> by each ?

You will have to traverse them. Easiest way would be to use the diretory()
function which returns an array with all information needed.

> I was also wondering if an external utility might exist with this 
> functionality...

Yes, either powershell or an old school batch file will do:

https://www.howtogeek.com/131881/how-to-delete-files-older-than-x-days-on-windows/

HTH
Thomas
Boris BorzicRe: How to delete files on folder based on date
on Thu, 01 Dec 2022 19:24:36 +0100
Thomas Braun wrote in news:i9k3snzln9aw.zrbg9exp5w15.dlg@40tude.net:

>> Is that possible with a single Xpp instruction? or I will have to
>> traverse the folder(s), obtaining every files date/time, and then
>> decide to delete it each by each ?
> 
> You will have to traverse them. Easiest way would be to use the
> diretory() function which returns an array with all information
> needed. 
> 
>> I was also wondering if an external utility might exist with this 
>> functionality...
> 
> Yes, either powershell or an old school batch file will do:
> 
> https://www.howtogeek.com/131881/how-to-delete-files-older-than-x-days-
> on-windows/ 

Here is an example (delete .BAK files older than 30 days):

forfiles -p "E:\download" -s -m *.bak /D -30 /C "cmd /c del @path"


Best regards,
Boris Borzic

http://xb2.net
http://sqlexpress.net
industrial strength Xbase++ development tools
Jonathan LeemingRe: How to delete files on folder based on date
on Tue, 29 Nov 2022 17:00:20 -0700
On 11/25/2022 9:52 AM, Juan Gonzalez wrote:
> Hello,
> 
> I am in the need to periodically delete files accumulated on a folder, based on
> their date.
> 
> For example:
> The folder D:\FOLDER\ has several hundreds of files
> 
> I want to delete all of them whose date is OLDER than 10 days.
> 
> Is that possible with a single Xpp instruction? or I will have to traverse the
> folder(s), obtaining every file´s date/time, and then decide to delete it each
> by each ?
> 
> I was also wondering if an external utility might exist with this
> functionality...
> 
> Thank you !

Hi Juan,

This is a simple little function that I wrote to use in my application...

#Include "Directory.ch"

FUNCTION DelOldFiles(cPath,cWild,nDays,aExcept)

* cPath : "C:\directory\folder\"
* cWild :"*.PDF", defaults to "*.*"
* nDays : Number of days old to retain. eg: 0 deletes all, 1 keeps 
todays, default is 0
* aExcept is an optional array of filenames to exclude from deletion process

LOCAL nPnt,;
       dDelDate,;
       aDirectory

    dDelDate := DATE() - IF(nDays==NIL,0,nDays)

    aDirectory := DIRECTORY(cPath+IF(cWild==NIL,"*.*",cWild))

    FOR nPnt := 1 TO LEN(aDirectory)
       IF aDirectory[nPnt,F_CREATION_DATE] <= dDelDate .AND.;
          (EMPTY(aExcept) .OR. ASCAN(aExcept,{|x|UPPER(x) == 
UPPER(cPath+ALLTRIM(aDirectory[nPnt,F_NAME]))}) == 0)
          FERASE(cPath+ALLTRIM(aDirectory[nPnt,F_NAME]))
       ENDIF
    NEXT nPnt

RETURN NIL

Regards... Jonathan