home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / access / diverses / fattr / fattr.txt < prev   
Text File  |  1994-03-19  |  12KB  |  364 lines

  1. FATTR - Sample file manipulation DLL for Microsoft Access
  2. =========================================================
  3.  
  4. 3/20/94 -- KNG
  5.     Added Get/SetFileAttr() functions.
  6.  
  7.  
  8. This is a DLL callable from Microsoft Access that contains the following
  9. functions:
  10.  
  11. GetFileSize() - gets the size of a specified file
  12. GetFileDateTime() - gets the timestamp of a specified file
  13. GetDiskFree() - returns the amount of free disk space on a specifie drive
  14. DosFindFirst() - returns the first file matching a specification
  15. DosFindNext() - returns the next file from the original DosFindFirst()
  16. ConvertDosTime() - converts the date/time information from DOS time to
  17.                    Access time format
  18. GetFileAttr() - Retrieve a specific file's DOS attribute (one or more of the 
  19.                 ATTR_*, constants listed below, added together
  20. SetFileAttr() - Set a specific file's DOS attribute, using the sum of one
  21.                 or more of the ATTR_* constants.
  22.  
  23.  
  24. This is commonly requested information from Microsoft Access, however they
  25. are not currently supplied by Access or the Windows API.
  26.  
  27.  
  28. To use
  29. ------
  30.  
  31. Copy FATTR.DLL to your Access directory or Windows directory.
  32.  
  33. The file FATTR.MDB contains the following declarations:
  34.  
  35. Global Const ATTR_NORMAL = 0
  36. Global Const ATTR_READONLY = 1
  37. Global Const ATTR_HIDDEN = 2
  38. Global Const ATTR_SYSTEM = 4
  39. Global Const ATTR_LABEL = 8
  40. Global Const ATTR_SUBDIR = 16
  41. Global Const ATTR_ARCHIVE = 32
  42.  
  43. Type FINDFILE
  44.   reserved As String * 21
  45.   attrib As String * 1
  46.   wr_time As Integer
  47.   wr_date As Integer
  48.   size As Long
  49.   name As String * 13
  50. End Type
  51.  
  52. ' Both these functions return the file's attribute after the function call.
  53. ' On error, they return -2 if the file wasn't found, or -5 if permission was denied.
  54. Declare Function GetFileAttr Lib "FATTR.DLL" Alias "GetFAttr" (ByVal strFileName As String) As Integer
  55. Declare Function SetFileAttr Lib "FATTR.DLL" Alias "SetFAttr" (ByVal strFileName As String, ByVal intAttr As Integer) As Integer
  56.  
  57. Declare Function GetDiskFree Lib "FATTR.DLL" (ByVal disk As Integer) As Long
  58. Declare Function ConvertDosTime Lib "FATTR.DLL" (ff As FINDFILE) As Double
  59. Declare Function DosFindFirst Lib "FATTR.DLL" (ByVal FileName$, ByVal attrib%, ff As FINDFILE) As Integer
  60. Declare Function DosFindNext Lib "FATTR.DLL" (ff As FINDFILE) As Integer
  61. Declare Function GetFileDateTime Lib "FATTR.DLL" (ByVal FileName$) As Double
  62. Declare Function GetFileSize Lib "FATTR.DLL" (ByVal FileName$) As Long
  63.  
  64. Reference to Functions
  65. ======================
  66.  
  67. GetFileAttr()
  68. -------------
  69.  
  70. Syntax    GetFileAttr(strFileName)
  71.  
  72.           strFileName is the name of a file to examine.
  73.  
  74. Returns   an integer representing the file's attribute.
  75.           -2 if the file wasn't found.
  76.           -5 if permission was denied.
  77.  
  78. Notes
  79.  
  80.           Use the ATTR_* attribute constants when checking 
  81.           and setting file attributes.
  82.           To see if a file is hidden:
  83.  
  84.           intAttr = GetFileAttr("someFileName")
  85.           isHidden = intAttr AND ATTR_HIDDEN
  86.  
  87. SetFileAttr()
  88. -------------
  89.  
  90. Syntax    SetFileAttr(strFileName, intAttrib)
  91.  
  92.           strFileName is the name of a file for which to set the attribute.
  93.           intAttrib is the attribute to set for the file
  94.  
  95. Returns   The original file attribute.
  96.           -2 if the file wasn't found.
  97.           -5 if permission was denied.
  98.  
  99. Notes
  100.           To set a file to be hidden:
  101.  
  102.           intAttr = GetFileAttr("someFileName")
  103.           intAttr = intAttr OR ATTR_HIDDEN
  104.           intAttr = SetFileAttr("someFileName", intAttr)
  105.  
  106.           or
  107.  
  108.           intAttr = SetFileAttr("someFileName", GetFileAttr("someFileName") OR ATTR_HIDDEN)
  109.  
  110.  
  111. GetFileSize()
  112. -------------
  113.  
  114. Syntax    GetFileSize(sFileName)
  115.  
  116.           sFileName is the name of a file to examine.
  117.  
  118. Returns   a long integer representing the size of the file.
  119.  
  120. Notes     A full path may be necessary.  The function only looks for files
  121.           with normal attributes.  Hidden or system files will not be
  122.           searched for with this function.
  123.  
  124.  
  125. GetDiskFree()
  126. -------------
  127.  
  128. Syntax    GetDiskFree(nDrive)
  129.  
  130.           nDrive is a number, where 1=A, 2=B, 3=C, etc.
  131.  
  132. Returns   a long integer representing the amount of disk space available on
  133.           the designated drive.
  134.  
  135. Notes     nDrive can also be 0, which means default drive.  Huge drives may
  136.           not be representable with a long integer, since longs are signed
  137.           under Access.
  138.  
  139.  
  140. GetFileDateTime()
  141. -----------------
  142.  
  143. Syntax    GetFileDateTime(sFileName)
  144.  
  145.           sFileName is the name of a file to examine.
  146.  
  147. Returns   a double value, which is the internal representation of a date/time
  148.           under Access.
  149.  
  150. Notes     A full path may be necessary.  The date/time returned is a double,
  151.           but can be converted to a readable format with the Format function:
  152.  
  153.              Format(GetFileDateTime("MSACCESS.EXE"), "Long Date")
  154.  
  155.  
  156. DosFindFirst()
  157. --------------
  158.  
  159. Syntax    DosFindFirst(sFileSpec, nAttrib, ffStruct)
  160.  
  161.           sFileSpec is a valid DOS filespec, possibly including wildcards.
  162.           nAttrib can be one or more of the following OR'd together:
  163.             ATTR_NORMAL, ATTR_RDONLY, ATTR_HIDDEN, ATTR_SYSTEM, ATTR_ARCH, ATTR_VOLID,
  164.             ATTR_SUBDIR.
  165.           ffStruct is a variable of type FINDFILE.
  166.  
  167. Returns   Zero is successful.
  168.  
  169. Notes     To OR attributes together, either add them together or use the
  170.           OR operator.  Example:
  171.  
  172.              (ATTR_NORMAL OR ATTR_RDONLY OR ATTR_SUBDIR)
  173.  
  174.           ffStruct is filled in with the first file that meets the search
  175.           specification.  To retrieve the next file, use DosFindNext().
  176.  
  177.           ffStruct contains the following elements:
  178.  
  179.             name - an ASCII 0 terminated filename
  180.             attrib - a character attribute
  181.             wr_date/wr_time - the file timestamp
  182.             size - the file size
  183.  
  184.           To convert the wr_date/wr_time format to something readable, you
  185.           should first call ConvertDosTime().
  186.  
  187.           The attrib element will contain any file attributes OR'd together.
  188.           To extract a particular attribute, AND it with the attribute you
  189.           are querying.  For example, to determine if a file found is a
  190.           subdirectory, you can say something like
  191.  
  192.             If (Asc(ffStruct.attrib) AND ATTR_SUBDIR) Then
  193.               MsgBox "It is a subdirectory!"
  194.             End If
  195.  
  196.           Note that Asc() is necessary, because the element is a character.
  197.  
  198.           The name element is zero terminated, which means you will need to
  199.           remove the ASCII 0 from the string.  The following code shows how
  200.           to do that:
  201.  
  202.             If InStr(ffStruct.name, Chr(0)) > 0) Then
  203.                MyVar = Left(ffStruct.name, InStr(ffStruct.name, Chr(0)) - 1)
  204.             End If
  205.  
  206.  
  207. DosFindNext()
  208. -------------
  209.  
  210. Syntax    DosFindNext(ffStruct)
  211.  
  212.           ffStruct is a variable of type FINDFILE.  It should have been
  213.           initialized by calling DosFindFirst() first.
  214.  
  215. Returns   Zero is successful.
  216.  
  217. Notes     DosFindNext() goes hand-in-hand with DosFindFirst().  A common way
  218.           to traverse a directory is as follows:
  219.  
  220.             Dim status, ffStruct As FINDFILE
  221.             status = DosFindFirst("*.*", ATTR_NORMAL, ffStruct)
  222.             While status = 0 Then
  223.               status = DosFindNext(ffStruct)
  224.             Wend
  225.  
  226.  
  227. ConvertDosTime()
  228. ----------------
  229.  
  230. Syntax    ConvertDosTime(ffStruct)
  231.  
  232.           ffStruct is a variable of type FINDFILE.  It should be initialized
  233.           by DosFindFirst() or DosFindNext().
  234.  
  235. Returns   a double value, which is the internal representation of a date/time
  236.           under Access.
  237.  
  238. Notes     The date/time returned is a double, but can be converted to a
  239.           readable format with the Format function.  The following example,
  240.           goes through the Access directory and dumps the name and date of
  241.           every file to the Immediate Window:
  242.  
  243.             Dim status, ffStruct As FINDFILE
  244.             status = DosFindFirst("*.*", ATTR_NORMAL, ffStruct)
  245.             While status = 0 Then
  246.               Debug.Print ffStruct.name &
  247.                 Format(ConvertDosTime(ffStruct), "Long Date")
  248.               status = DosFindNext(ffStruct)
  249.             Wend
  250.  
  251.  
  252. Examples
  253. =======
  254.  
  255. The following examples come in the FATTR.MDB.  It is a "whereis" program
  256. written in Access Basic that uses the FATTR.DLL.  To use it, go to the Access
  257. Immediate Window and type the following:
  258.  
  259.   ? whereis("C:\WINDOWS\", "*.DLL")
  260.  
  261. The program will then dump all files matching the file specification to the
  262. Immediate Window.  whereis() takes two arguments:  the first argument is the
  263. directory to start from.  It must be a valid directory and MUST have a
  264. backslash as the last character.  The second argument is the filespec to
  265. search for.  It can contain the DOS wildcard characters "*" and "?".
  266.  
  267. The above example finds all DLLs in the WINDOWS directory all directories
  268. underneath.
  269.  
  270. Function WhereIs (strDirSpec As String, strSpec As String)
  271.     
  272.     Dim ff As FINDFILE
  273.     Dim intStat As Integer
  274.     Dim varRetval As Variant
  275.     Dim varTemp As Variant
  276.  
  277.     'look for subdirectories first
  278.     Debug.Print "Searching " & strDirSpec & strSpec
  279.     intStat = DosFindFirst(strDirSpec & "*.*", ATTR_SUBDIR, ff)
  280.     While (intStat = 0)
  281.         If (Asc(ff.attrib) And ATTR_SUBDIR) And (Left(ff.name, 1) <> ".") Then
  282.             'clean ff.name
  283.             If (InStr(ff.name, Chr(0)) > 0) Then
  284.                 varTemp = Left(ff.name, InStr(ff.name, Chr(0)) - 1)
  285.             Else
  286.                 varTemp = ff.name
  287.             End If
  288.             varRetval = WhereIs(strDirSpec & varTemp & "\", strSpec)
  289.         End If
  290.         intStat = DosFindNext(ff)
  291.     Wend
  292.  
  293.     'look specifically for a file type
  294.     intStat = DosFindFirst(strDirSpec & strSpec, 0, ff)
  295.     While (intStat = 0)
  296.         If (InStr(ff.name, Chr(0)) > 0) Then
  297.             varTemp = Left(ff.name, InStr(ff.name, Chr(0)) - 1)
  298.         Else
  299.             varTemp = ff.name
  300.         End If
  301.         Debug.Print varTemp, Format(ConvertDosTime(ff), "long date")
  302.         intStat = DosFindNext(ff)
  303.     Wend
  304.     'allow other things to happen
  305.     DoEvents
  306. End Function
  307.  
  308. This function uses the Dir() function to list all the files in the
  309. C:\ directory, along with their file attributes.
  310.  
  311. Function TestAttrib ()
  312.     ' Print out a list of files in your root
  313.     ' directory, along with the file attribute of each.
  314.  
  315.     Dim strName As String
  316.  
  317.     ' Note:  Dir() won't find hidden or system files.
  318.     strName = Dir("C:\")
  319.     Do While strName <> ""
  320.         Debug.Print strName, GetFileAttr("C:\" & strName)
  321.         strName = Dir
  322.     Loop
  323. End Function
  324.  
  325. Programming notes
  326. =================
  327.  
  328. Look at FATTR.C and notice how simple it is to create a DLL that you can use
  329. with Microsoft Access!
  330.  
  331. The DLL was built with Microsoft Visual C++ using the Large Memory Model and
  332. all default settings and optimizations.  Visual C++ will create a default
  333. WEP and LibMain(), so these aren't included.  Also note that the Large Model
  334. is required because much of the runtime library is not model independent!
  335.  
  336. To get the internal Access date format, I used the following formula:
  337.  
  338.   ((time_t - 25200) / 86400) + 25569
  339.  
  340. where
  341.   time_t is the C time format (number of seconds starting 1/1/70 midnight)
  342.   25200 is the number of seconds in 7 hours
  343.   86400 is the number of seconds in 1 day
  344.   25569 is the Access equivalent of 1/1/70
  345.  
  346.  
  347.  
  348. Legal stuff
  349. ===========
  350.  
  351. Please note that this sample is totally unsupported, and the software is
  352. PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND and in no event shall MS be
  353. liable for any damages whatsoever in connection with your use of the
  354. sample.
  355.  
  356. However, I'm willing to answer questions about sample and programming DLLs
  357. for Access in general.
  358.  
  359. Please feel free to copy this and use it, sell it, whatever.
  360.  
  361. Roger Harui [MSFT], 71742,1177
  362. Ken Getz 76137,3650
  363.  
  364.