December 11, 2024, 08:32:51 PM

News:

IWBasic runs in Windows 11!


Recursion no longer works?

Started by billhsln, September 22, 2014, 01:48:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

billhsln

I wrote a simple program to scan thru a drive and write out files older than 90 days.  It goes thru a couple of directories, but then aborts.  I keep looking at the code, but don't see what I am doing wrong.

' PrintIFS.iwb

' Created 2014-09-22

AUTODEFINE "off"

DECLARE IMPORT,FileTimeToSystemTime(timef:filetime,times:systemtime), INT
DECLARE IMPORT,FileTimeToLocalFileTime(timef:filetime, timel:filetime)
DECLARE IMPORT,GetSystemTime(POINTER p)
DECLARE IMPORT,SystemTimeToFileTime(POINTER p,POINTER p),INT

CONST Max_Path = 260

TYPE FILETIME
DEF dwLowDateTime:INT
DEF dwHighDateTime:INT
ENDTYPE

TYPE SYSTEMTIME,2
DEF wMillisecondsec,wSecond,wMinute,wHour,wDay,wDayofWeek,wMonth,wYear:WORD
ENDTYPE

TYPE WIN32_FIND_DATA,4
DEF dwFileAttributes:INT
DEF ftCreationTime:FILETIME
DEF ftLastAccessTime:FILETIME
DEF ftLastWriteTime:FILETIME
DEF nFileSizeHigh:UINT
DEF nFileSizeLow:UINT
DEF dwReserved0:INT
DEF dwReserved1:INT
DEF cFileName[Max_Path]:ISTRING
DEF cAlternate[14]:ISTRING
ENDTYPE

DEF dir:INT
DEF size:UINT64
DEF drive, filename, fDate, temp:STRING
DEF temptime:FILETIME
DEF times:SYSTEMTIME
DEF cDate = date$("yyyy-MM-dd"):STRING
DEF tFound, cFound:INT
DEF oFile:FILE

DateSub(90)
PRINT cDate, " Date"
PRINT " "

tFound = 0

INPUT "Enter Drive: ", drive
drive = drive + ":\\"
IF (OPENFILE(oFile,drive + "zFiles.txt","W") = 0)
WRITE oFile, cDate
PrintFiles(drive)
WRITE oFile, "Final Total " + STR$(tFound)
CLOSEFILE oFile
ENDIF
PRINT "Final total ",tFound

DO:UNTIL INKEY$ <> ""

END

SUB PrintFiles(iPath:STRING)
PRINT iPath
dir = FINDOPEN(iPath + "*.*")
IF (dir)
cFound = 0
WHILE (dir)
filename = *<WIN32_FIND_DATA>dir.cFileName
IF (*<WIN32_FIND_DATA>dir.dwFileAttributes & @FILE_DIRECTORY)
IF (filename = ".") OR (filename = "..")
size = 0
ELSE
PrintFiles(iPath + filename + "\\")
ENDIF
ELSE
FileTimeToLocalFileTime(*<WIN32_FIND_DATA>dir.ftLastWriteTime,temptime)
filetimetosystemtime(temptime,times)
fDate = LTRIM$(STR$(times.wYear)) + "-"
temp = LTRIM$(STR$(times.wMonth))
IF (LEN(temp) < 2) THEN temp = "0" + temp
fDate += temp + "-"
temp = LTRIM$(STR$(times.wDay))
IF (LEN(temp) < 2) THEN temp = "0" + temp
fDate += temp
' Must be < days ago
IF fDate < cDate
Write oFile, iPath + "," + filename + "," + fDate
cFound++
ENDIF
ENDIF
filename = FINDNEXT(dir)
IF (!*<WIN32_FIND_DATA>dir.cFileName[0])
FINDCLOSE(dir)
dir = 0
ENDIF
ENDWHILE
ENDIF
tFound = tFound + cFound
IF cFound > 0
PRINT ipath, " ", cFound
' DO:UNTIL INKEY$ <> ""
PRINT " "
ENDIF
RETURN
ENDSUB

SUB DateSub(sdays:INT)

TYPE DateType
DEF LowDW:UINT
DEF HighDW:UINT
END TYPE

UNION uDT
DEF qVar:UINT64
DEF dtVar:DateType
END UNION

DEF st:SYSTEMTIME
DEF ft:FILETIME
DEF u:uDT
DEF rtn:INT

' First get the system time so that all the other structure members are filled.
GetSystemTime(st)
'Setup the SYSTEMTIME structure with the original date like so:
st.wYear = VAL(MID$(cDate,1,4))
st.wMonth = VAL(MID$(cDate,6,2))
st.wDay = VAL(MID$(cDate,9,2))

' Convert the system time to file time.
rtn = SystemTimeToFileTime(st, ft)

' Move the FILETIME info into a UINT64 variable
' and add days using the Quad variable. (This is how M$ recommends doing it)
u.dtVar.LowDW = ft.dwLowDateTime
u.dtVar.HighDW = ft.dwHighDateTime

'Subtract days using the Quad variable.
' 1 day in nano seconds 24 * 60 * 60 * 10000000
u.qVar -= ((24 * 60 * 60 * 10000000) * sdays) '<---- Sub days

ft.dwLowDateTime = u.dtVar.LowDW
ft.dwHighDateTime = u.dtVar.HighDW

' Convert the file time to system time.
rtn = FileTimeToSystemTime(ft, st)

' Now your new date will be in the SYSTEMTIME structure members.

'PRINT st.wYear," ",st.wMonth," ",st.wDay
cDate = USING("0####",st.wYear) + "-" + USING("0##",st.wMonth) + "-" + USING("0##",st.wDay)
'PRINT cDate

RETURN
ENDSUB


Hopefully some one can see what I am missing.

Thanks,
Bill
When all else fails, get a bigger hammer.

LarryMc

I got rid of some of the clutter
This works fine on my c drive

$include "windowssdk.inc"

DEF count:INT

OPENCONSOLE
DEF dir:INT
DEF size:UINT64
DEF drive, filename, fDate, temp:STRING
DEF temptime:FILETIME
DEF times:SYSTEMTIME
DEF cDate = date$("yyyy-MM-dd"):STRING
DEF tFound, cFound:INT
DEF oFile:FILE
DateSub(90)
PRINT cDate, " Date"
PRINT " "

tFound = 0

INPUT "Enter Drive: ", drive
drive = drive + ":\\"
IF (OPENFILE(oFile,getstartpath + "zFiles.txt","W") = 0)
WRITE oFile, cDate
PrintFiles(drive)
WRITE oFile, "Final Total " + STR$(tFound)
CLOSEFILE oFile
ENDIF
PRINT "Final total ",tFound

DO:UNTIL INKEY$ <> ""

END


SUB PrintFiles(path:STRING)
DEF temptime:FILETIME
DEF times:SYSTEMTIME

DEF dir,attrib:INT
DEF filename:STRING
DEF fullname:STRING
dir = FINDOPEN(path + "*.*")
IF(dir)
cFound=0
DO
filename = FINDNEXT(dir,attrib)
IF len(filename)

IF attrib & @FILE_DIRECTORY
IF(filename <> ".") & (filename <> "..")
'PRINT path + "[" + filename + "]"
fullname = path + filename + "\\"
PrintFiles(fullname)
ENDIF
ELSE
FileTimeToLocalFileTime(*<WIN32_FIND_DATA>dir.ftLastWriteTime,temptime)
filetimetosystemtime(temptime,times)
fDate = LTRIM$(STR$(times.wYear)) + "-"
temp = LTRIM$(STR$(times.wMonth))
IF (LEN(temp) < 2) THEN temp = "0" + temp
fDate += temp + "-"
temp = LTRIM$(STR$(times.wDay))
IF (LEN(temp) < 2) THEN temp = "0" + temp
fDate += temp
' Must be < days ago
IF fDate<cDate
Write oFile, Path + "," + filename + "," + fDate
cFound++
ENDIF
ENDIF
ENDIF
UNTIL filename = ""
FINDCLOSE dir

ENDIF
tFound = tFound + cFound
IF cFound > 0
PRINT path, " ", cFound
' DO:UNTIL INKEY$ <> ""
PRINT " "
ENDIF
return
ENDSUB

SUB DateSub(sdays:INT)

TYPE DateType
DEF LowDW:UINT
DEF HighDW:UINT
END TYPE

UNION uDT
DEF qVar:UINT64
DEF dtVar:DateType
END UNION


DEF st:SYSTEMTIME
DEF ft:FILETIME
DEF u:uDT
DEF rtn:INT

' First get the system time so that all the other structure members are filled.
GetSystemTime(st)
'Setup the SYSTEMTIME structure with the original date like so:
st.wYear = VAL(MID$(cDate,1,4))
st.wMonth = VAL(MID$(cDate,6,2))
st.wDay = VAL(MID$(cDate,9,2))

' Convert the system time to file time.
rtn = SystemTimeToFileTime(st, ft)

' Move the FILETIME info into a UINT64 variable
' and add days using the Quad variable. (This is how M$ recommends doing it)
u.dtVar.LowDW = ft.dwLowDateTime
u.dtVar.HighDW = ft.dwHighDateTime

'Subtract days using the Quad variable.
' 1 day in nano seconds 24 * 60 * 60 * 10000000
u.qVar -= ((24 * 60 * 60 * 10000000) * sdays) '<---- Sub days

ft.dwLowDateTime = u.dtVar.LowDW
ft.dwHighDateTime = u.dtVar.HighDW

' Convert the file time to system time.
rtn = FileTimeToSystemTime(ft, st)

' Now your new date will be in the SYSTEMTIME structure members.

'PRINT st.wYear," ",st.wMonth," ",st.wDay
cDate = USING("0####",st.wYear) + "-" + USING("0##",st.wMonth) + "-" + USING("0##",st.wDay)
'PRINT cDate

RETURN
ENDSUB
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

That seems to have fixed the problem.   All I can say is WEIRD...

Thanks for the help,
Bill
When all else fails, get a bigger hammer.