Delete files by date

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
franco
Posts: 921
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Delete files by date

Post by franco »

I am trying to delete all .mem files in a folder where filedate() is less then date() - 20
I do not know the name of the files. I can delete them if I know the name of the file.
mem is a folder in the current path.

Code: Select all

local ti := 20
? delete file for filedate('mem\*.mem') < ti
? erase for filedate('mem\*.mem') < ti
Thanks, Franco
All The Best,
Franco
Canada
User avatar
AUGE_OHR
Posts: 2117
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Delete files by date

Post by AUGE_OHR »

hi,

try this

Code: Select all

#include "Directry.ch" 

PROCEDURE TESTDEL(cDate)
LOCAL dDate
LOCAL aDir := DIRECTORY("*.MEM")

   ASORT(aDir,,, {|aX,aY| aX[F_DATE] > aY[F_DATE] }
   nMax := LEN(aDir)
   FOR n := nMax TO 1 STEP -1
      dDate := aDir[i][F_DATE]
      IF dDate <= CTOD(cDate)
         FERASE( Dir[i][F_NAME]
      ELSE
         EXIT   
      ENDIF
   NEXT
have fun
Jimmy
franco
Posts: 921
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Delete files by date

Post by franco »

Thanks, Jimmy
the first characters of file name are a date. How could i use that instead if file date like.
filename is 0707202211002.mem this is a date, a time, and a column number with no separators.

Code: Select all

Ddate := aDir[i] [ ctod(substr([F_DATE],1,8)
This way I could erase from the original date. In my scheduler program the system date changes when I edit a mem file.
All The Best,
Franco
Canada
User avatar
AUGE_OHR
Posts: 2117
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Delete files by date

Post by AUGE_OHR »

hi Franco,

your Filename is "bad" to ASORT()

try this

Code: Select all

   cFile := aDir[i][F_NAME]
   IF File2Date(cFile) <= CTOD(cDate)
      FERASE( Dir[i][F_NAME]

FUNCTION File2Date(cFile)
LOCAL cDay, cMonth, cYear

   cDay := SUBSTR(cFile,1,2)
   cMonth := SUBSTR(cFile,3,2)
   cYear := SUBSTR(cFile,5,4)

RETURN StoD(cYear + cMonth + cDay) 
have fun
Jimmy
franco
Posts: 921
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Delete files by date

Post by franco »

I will try and let you know. You give me so may new ideas from your programming knowledge.
Tied up for rest of day.
You got me thinking. I have a field in table that is create date of record.
It would be easy to go though file and if field date is less then ? create file name and delete it.
All The Best,
Franco
Canada
franco
Posts: 921
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Delete files by date

Post by franco »

Hi Jimmy
The following seems to work fine.
I just keep looping though the file.
This works better because I am using a field with original date. If using system date it changes with each modification of record.

Code: Select all

                       SET DATE FORMAT TO 'ddmmyyyy'
			fi2 := dtoc(date)                                                    //+fi1+alltrim(str(tim))
			SET DATE FORMAT TO 'dd/mm/yyyy'

				fi := 'Appmem\'+fi2+fi1+alltrim(str(tim))+'.mem'

				if file(fi)
					erase (fi)
				endif
All The Best,
Franco
Canada
Post Reply