File: n_cst_filesrv.sru
Size: 8053
Date: Sat, 08 Dec 2007 15:38:26 +0100
$PBExportHeader$n_cst_filesrv.sru
$PBExportComments$Some Filesystem related functions
forward
global type n_cst_filesrv from nonvisualobject
end type
type os_filedatetime from structure within n_cst_filesrv
end type
type os_systemtime from structure within n_cst_filesrv
end type
type os_securityattributes from structure within n_cst_filesrv
end type
type os_finddata from structure within n_cst_filesrv
end type
type os_fileopeninfo from structure within n_cst_filesrv
end type
end forward

type os_filedatetime from structure
   unsignedlong      ul_lowdatetime
   unsignedlong      ul_highdatetime
end type

type os_systemtime from structure
   unsignedinteger      wyear
   unsignedinteger      wmonth
   unsignedinteger      wdayofweek
   unsignedinteger      wday
   unsignedinteger      whour
   unsignedinteger      wminute
   unsignedinteger      wsecond
   unsignedinteger      wmilliseconds
end type

type os_securityattributes from structure
   unsignedlong      ul_length
   string      ch_description
   boolean     b_inherit
end type

type os_finddata from structure
   unsignedlong      ul_fileattributes
   os_filedatetime      str_creationtime
   os_filedatetime      str_lastaccesstime
   os_filedatetime      str_lastwritetime
   unsignedlong      ul_filesizehigh
   unsignedlong      ul_filesizelow
   unsignedlong      ul_reserved0
   unsignedlong      ul_reserved1
   character      ch_filename[260]
   character      ch_alternatefilename[14]
end type

type os_fileopeninfo from structure
   character      c_length
   character      c_fixed_disk
   unsignedinteger      ui_dos_error
   unsignedinteger      ui_na1
   unsignedinteger      ui_na2
   character      c_pathname[128]
end type

global type n_cst_filesrv from nonvisualobject
end type
global n_cst_filesrv n_cst_filesrv

type prototypes

// Win 32 calls
Function ulong GetDriveType (string drive) library "kernel32" alias for GetDriveTypeA
Function boolean CreateDirectory (ref string directoryname, ref os_securityattributes secattr) library "kernel32" alias for CreateDirectoryA
Function boolean RemoveDirectory (ref string directoryname) library "kernel32" alias for RemoveDirectoryA
Function ulong GetCurrentDirectory (ulong textlen, ref string dirtext) library "kernel32" alias for GetCurrentDirectoryA
Function boolean SetCurrentDirectory (ref string directoryname ) library "kernel32" alias for SetCurrentDirectoryA
Function ulong GetFileAttributes (ref string filename) library "kernel32" alias for GetFileAttributesA
Function boolean SetFileAttributes (ref string filename, ulong attrib) library "kernel32" alias for SetFileAttributesA
Function boolean MoveFile (ref string oldfile, ref string newfile) library "kernel32" alias for MoveFileA
Function long FindFirstFile (ref string filename, ref os_finddata findfiledata) library "kernel32" alias for FindFirstFileA
Function boolean FindNextFile (long handle, ref os_finddata findfiledata) library "kernel32" alias for FindNextFileA
Function boolean FindClose (long handle) library "kernel32"
Function boolean GetDiskFreeSpace (string drive, ref long sectpercluster, ref long bytespersect, ref long freeclusters, ref long totalclusters) library "kernel32" alias for GetDiskFreeSpaceA
Function long GetLastError() library "kernel32"

// Win32 calls for file date and time
Function long OpenFile (ref string filename, ref os_fileopeninfo of_struct, ulong action) LIBRARY "kernel32"
Function boolean CloseHandle (long file_hand) LIBRARY "kernel32"
Function boolean GetFileTime(long hFile, ref os_filedatetime  lpCreationTime, ref os_filedatetime  lpLastAccessTime, ref os_filedatetime  lpLastWriteTime  )  library "kernel32"
Function boolean FileTimeToSystemTime(ref os_filedatetime lpFileTime, ref os_systemtime lpSystemTime) library "kernel32"
Function boolean FileTimeToLocalFileTime(ref os_filedatetime lpFileTime, ref os_filedatetime lpLocalFileTime) library "kernel32"
Function boolean SetFileTime(long hFile, os_filedatetime  lpCreationTime, os_filedatetime  lpLastAccessTime, os_filedatetime  lpLastWriteTime  )  library "kernel32"
Function boolean SystemTimeToFileTime(os_systemtime lpSystemTime, ref os_filedatetime lpFileTime) library "kernel32"
Function boolean LocalFileTimeToFileTime(ref os_filedatetime lpLocalFileTime, ref os_filedatetime lpFileTime) library "kernel32"

// 
Function Int GetTempFileName(Ref String ls_path,Ref String ls_filename,Int li_Seed,Ref String ls_result_name) LIBRARY "kernel32" ALIAS FOR  GetTempFileNameA 
Function  Long GetTempPath( long nBufferLength, Ref String lpBuffer ) LIBRARY "kernel32" ALIAS FOR GetTempPathA 
end prototypes
type variables

end variables

forward prototypes
public function long of_deleteolderfiles (string as_filename, datetime adt_beforedatetime)
public function long of_findolderfiles (string as_seachfilename, datetime adt_beforedatetime, ref string as_filename[])
end prototypes

public function long of_deleteolderfiles (string as_filename, datetime adt_beforedatetime);/**
  Find and delete files older than given datetime
*/

os_FindData lstr_FindData
os_FindData lstr_FindDataTemp
os_SystemTime lstr_SystemTime

long ll_handle
datetime ldt_filedatetime
string ls_directory
string ls_filename[]
long ll_idx, ll_cnt
ll_cnt = 0
ll_handle=FindFirstFile ( as_filename, lstr_FindData)
ls_directory = Left ( as_filename , lastPos ( as_filename , "\" ))

if len ( ls_directory ) > 0 then 
   log.of_addmessage( 1,"Deleting in :" + ls_directory )
else
   return 0
end if

If ll_handle = -1 Then 
  // something wrong!
  return 0
Else
   Do 
      FileTimeToLocalFileTime & 
      (lstr_FindData.str_lastwritetime, lstr_FindDataTemp.str_lastwritetime)
      If FileTimeToSystemTime & 
         (lstr_FindDataTemp.str_lastwritetime, lstr_SystemTime) Then
         ldt_FileDateTime = datetime( &
                  date(lstr_SystemTime.wYear, lstr_SystemTime.wMonth, lstr_SystemTime.wDay), & 
                  time(lstr_SystemTime.wHour,lstr_SystemTime.wMinute, lstr_SystemTime.wSecond))
         If ldt_FileDateTime < adt_BeforeDateTime Then
            // Delete File here
            ll_cnt ++
            ls_filename[ll_cnt] = lstr_FindData.ch_filename
         
         End if
      End if
   loop while FindNextFile ( ll_handle, lstr_FindData )
   FileClose(ll_handle)
End if

for ll_idx = 1 to ll_cnt 
   if Len ( ls_filename [ll_idx ] ) > 0 then 
      log.of_addmessage( 1,"Deleting:" + ls_directory +   ls_filename [ll_idx ])
      FileDelete ( ls_directory +   ls_filename [ll_idx ] )
   end if         
next  

return ll_cnt
end function

public function long of_findolderfiles (string as_seachfilename, datetime adt_beforedatetime, ref string as_filename[]);
/* NOT YET TESTED */

os_FindData lstr_FindData
os_FindData lstr_FindDataTemp
os_SystemTime lstr_SystemTime

long ll_handle
datetime ldt_filedatetime
string ls_directory
long ll_idx, ll_cnt
ll_cnt = 0
ll_handle=FindFirstFile ( as_seachfilename, lstr_FindData)
ls_directory = Left ( as_seachfilename , lastPos ( as_seachfilename , "\" ))

if len ( ls_directory ) > 0 then ; else ; return 0 ; end if

If ll_handle = -1 Then 
  // something wrong!
  return 0
Else
   Do 
      FileTimeToLocalFileTime & 
      (lstr_FindData.str_lastwritetime, lstr_FindDataTemp.str_lastwritetime)
      If FileTimeToSystemTime & 
         (lstr_FindDataTemp.str_lastwritetime, lstr_SystemTime) Then
         ldt_FileDateTime = datetime( &
                  date(lstr_SystemTime.wYear, lstr_SystemTime.wMonth, lstr_SystemTime.wDay), & 
                  time(lstr_SystemTime.wHour,lstr_SystemTime.wMinute, lstr_SystemTime.wSecond))
         If ldt_FileDateTime < adt_BeforeDateTime Then
            // Add file to result (refrenced array)
            ll_cnt ++
            as_filename[ll_cnt] = lstr_FindData.ch_filename
         End if
      End if
   loop while FindNextFile ( ll_handle, lstr_FindData )
   FileClose(ll_handle)
End if

return ll_cnt
end function

on n_cst_filesrv.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_cst_filesrv.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on