home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1998 June / Cd Pc Users 9.iso / prog / inst / baslibs / basfile.bas < prev    next >
Encoding:
BASIC Source File  |  1996-12-08  |  13.3 KB  |  499 lines

  1. Attribute VB_Name = "basFile"
  2. Option Explicit
  3.  
  4. Private Const MAX_FILENAME_LEN = 256
  5.  
  6. ' File and Disk functions.
  7. Public Const DRIVE_CDROM = 5
  8. Public Const DRIVE_FIXED = 3
  9. Public Const DRIVE_RAMDISK = 6
  10. Public Const DRIVE_REMOTE = 4
  11. Public Const DRIVE_REMOVABLE = 2
  12. Public Const DRIVE_UNKNOWN = 0    'Unknown, or unable to be determined.
  13.  
  14. Private Declare Function GetDriveTypeA Lib "kernel32" (ByVal nDrive As String) As Long
  15.  
  16. Private Declare Function GetVolumeInformation& Lib "kernel32" Alias "GetVolumeInformationA" _
  17.    (ByVal lpRootPathName As String, ByVal pVolumeNameBuffer As String, _
  18.     ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, _
  19.     lpMaximumComponentLength As Long, lpFileSystemFlags As Long, _
  20.     ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long)
  21.  
  22. Private Declare Function GetWindowsDirectoryA Lib "kernel32" _
  23.    (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  24.    
  25. Private Declare Function GetTempPathA Lib "kernel32" _
  26.    (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  27.  
  28. Private Const UNIQUE_NAME = &H0
  29.  
  30. Private Declare Function GetTempFileNameA Lib "kernel32" (ByVal _
  31.    lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique _
  32.    As Long, ByVal lpTempFileName As String) As Long
  33.    
  34. Private Declare Function GetSystemDirectoryA Lib "kernel32" _
  35.    (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  36.  
  37.  
  38. Private Declare Function ShellExecute Lib _
  39.    "shell32.dll" Alias "ShellExecuteA" _
  40.    (ByVal hwnd As Long, _
  41.    ByVal lpOperation As String, _
  42.    ByVal lpFile As String, _
  43.    ByVal lpParameters As String, _
  44.    ByVal lpdirectory As String, _
  45.    ByVal nShowCmd As Long) As Long
  46.    
  47. Private Const SW_HIDE = 0             ' = vbHide
  48. Private Const SW_SHOWNORMAL = 1       ' = vbNormal
  49. Private Const SW_SHOWMINIMIZED = 2    ' = vbMinimizeFocus
  50. Private Const SW_SHOWMAXIMIZED = 3    ' = vbMaximizedFocus
  51. Private Const SW_SHOWNOACTIVATE = 4   ' = vbNormalNoFocus
  52. Private Const SW_MINIMIZE = 6         ' = vbMinimizedNofocus
  53.  
  54. Private Declare Function GetShortPathNameA Lib "kernel32" _
  55.    (ByVal lpszLongPath As String, ByVal lpszShortPath _
  56.    As String, ByVal cchBuffer As Long) As Long
  57.    
  58. Private Type SHFILEOPSTRUCT
  59.         hwnd As Long
  60.         wFunc As Long
  61.         pFrom As String
  62.         pTo As String
  63.         fFlags As Integer
  64.         fAborted As Boolean
  65.         hNameMaps As Long
  66.         sProgress As String
  67. End Type
  68.  
  69. Private Const FO_DELETE = &H3
  70. Private Const FOF_ALLOWUNDO = &H40
  71. Private Const FOF_SILENT = &H4
  72. Private Const FOF_NOCONFIRMATION = &H10
  73.  
  74. Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
  75.    "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
  76.  
  77. Private Type STARTUPINFO
  78.     cb As Long
  79.     lpReserved As String
  80.     lpDesktop As String
  81.     lpTitle As String
  82.     dwX As Long
  83.     dwY As Long
  84.     dwXSize As Long
  85.     dwYSize As Long
  86.     dwXCountChars As Long
  87.     dwYCountChars As Long
  88.     dwFillAttribute As Long
  89.     dwFlags As Long
  90.     wShowWindow As Integer
  91.     cbReserved2 As Integer
  92.     lpReserved2 As Long
  93.     hStdInput As Long
  94.     hStdOutput As Long
  95.     hStdError As Long
  96. End Type
  97.  
  98. Private Type PROCESS_INFORMATION
  99.     hProcess As Long
  100.     hThread As Long
  101.     dwProcessId As Long
  102.     dwThreadID As Long
  103. End Type
  104.  
  105. Private Const NORMAL_PRIORITY_CLASS = &H20&
  106. Private Const INFINITE = -1&
  107. Private Const SYNCHRONIZE = &H100000
  108.  
  109. Private Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
  110.  
  111. Private Declare Function WaitForSingleObject Lib "kernel32" _
  112.     (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  113.     
  114. Private Declare Function CreateProcessA Lib "kernel32" _
  115.     (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, _
  116.     ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
  117.     ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
  118.     ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
  119.     lpStartupInfo As STARTUPINFO, _
  120.     lpProcessInformation As PROCESS_INFORMATION) As Long
  121.  
  122. Private Declare Function FindExecutableA Lib "shell32.dll" _
  123.    (ByVal lpFile As String, ByVal lpdirectory As _
  124.    String, ByVal lpResult As String) As Long
  125.  
  126. Private Declare Function SetVolumeLabelA Lib "kernel32" _
  127.    (ByVal lpRootPathName As String, _
  128.    ByVal lpVolumeName As String) As Long
  129.  
  130. '
  131. '  Finds the executable associated with a file
  132. '
  133. '  Returns "" if no file is found.
  134. '
  135. Public Function FindExecutable(s As String) As String
  136.    Dim i As Integer
  137.    Dim s2 As String
  138.    
  139.    s2 = String(MAX_FILENAME_LEN, 32) & Chr$(0)
  140.    
  141.    i = FindExecutableA(s & Chr$(0), vbNullString, s2)
  142.    
  143.    If i > 32 Then
  144.       FindExecutable = Left$(s2, InStr(s2, Chr$(0)) - 1)
  145.    Else
  146.       FindExecutable = ""
  147.    End If
  148.    
  149. End Function
  150.  
  151.  
  152. '
  153. '  Deletes a single file, or an array of files to the trashcan.
  154. '
  155. Public Function ShellDelete(ParamArray vntFileName() As Variant) As Boolean
  156.    Dim i As Integer
  157.    Dim sFileNames As String
  158.    Dim SHFileOp As SHFILEOPSTRUCT
  159.  
  160.    For i = LBound(vntFileName) To UBound(vntFileName)
  161.       sFileNames = sFileNames & vntFileName(i) & vbNullChar
  162.    Next
  163.         
  164.    sFileNames = sFileNames & vbNullChar
  165.  
  166.    With SHFileOp
  167.       .wFunc = FO_DELETE
  168.       .pFrom = sFileNames
  169.       .fFlags = FOF_ALLOWUNDO + FOF_SILENT + FOF_NOCONFIRMATION
  170.    End With
  171.  
  172.    i = SHFileOperation(SHFileOp)
  173.    
  174.    If i = 0 Then
  175.       ShellDelete = True
  176.    Else
  177.       ShellDelete = False
  178.    End If
  179. End Function
  180. '
  181. '  Runs a command as the Shell command does but waits for the command
  182. '  to finish before returning.  Note: The full path and filename extention
  183. '  is required.
  184. '  You might want to use Environ$("COMSPEC") & " /c " & command
  185. '  if you wish to run it under the command shell (and thus it)
  186. '  will search the path etc...
  187. '
  188. '  returns false if the shell failed
  189. '
  190. Public Function ShellWait(cCommandLine As String) As Boolean
  191.     Dim NameOfProc As PROCESS_INFORMATION
  192.     Dim NameStart As STARTUPINFO
  193.     Dim i As Long
  194.  
  195.     NameStart.cb = Len(NameStart)
  196.     i = CreateProcessA(0&, cCommandLine, 0&, 0&, 1&, _
  197.         NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
  198.    
  199.     If i <> 0 Then
  200.        Call WaitForSingleObject(NameOfProc.hProcess, INFINITE)
  201.        Call CloseHandle(NameOfProc.hProcess)
  202.        ShellWait = True
  203.     Else
  204.        ShellWait = False
  205.     End If
  206.     
  207. End Function
  208.  
  209. '
  210. '  As the Execute function but waits for the process to finish before
  211. '  returning
  212. '
  213. '  returns true on success.
  214.  
  215. Public Function ExecuteWait(s As String, Optional param As Variant) As Boolean
  216.    Dim s2 As String
  217.    
  218.    s2 = FindExecutable(s)
  219.    
  220.    If s2 <> "" Then
  221.       ExecuteWait = ShellWait(s2 & _
  222.          IIf(IsMissing(param), " ", " " & CStr(param) & " ") & s)
  223.    Else
  224.       ExecuteWait = False
  225.    End If
  226. End Function
  227. '
  228. '  Adds a backslash if the string doesn't have one already.
  229. '
  230. Public Function AddBackslash(s As String) As String
  231.    If Len(s) > 0 Then
  232.       If Right$(s, 1) <> "\" Then
  233.          AddBackslash = s + "\"
  234.       Else
  235.          AddBackslash = s
  236.       End If
  237.    Else
  238.       AddBackslash = "\"
  239.    End If
  240. End Function
  241.  
  242. '
  243. ' Executes a file with it's associated program.
  244. '    windowstyle uses the same constants as the Shell function:
  245. '       vbHide   0
  246. '       vbNormalFocus  1
  247. '       vbMinimizedFocus  2
  248. '       vbMaximizedFocus  3
  249. '       vbNormalNoFocus   4
  250. '       vbMinimizedNoFocus   6
  251. '
  252. '   returns true on success
  253. Public Function Execute(ByVal hwnd As Integer, s As String, Optional param As Variant, Optional windowstyle As Variant) As Boolean
  254.    Dim i As Long
  255.    
  256.    If IsMissing(windowstyle) Then
  257.       windowstyle = vbNormalFocus
  258.    End If
  259.    
  260.    i = ShellExecute(hwnd, vbNullString, s, IIf(IsMissing(param) Or (param = ""), vbNullString, CStr(param)), GetPath(s), CLng(windowstyle))
  261.    If i > 32 Then
  262.       Execute = True
  263.    Else
  264.       Execute = False
  265.    End If
  266. End Function
  267.  
  268. '
  269. '  Returns the file portion of a file + pathname
  270. '
  271. Public Function GetFile(s As String) As String
  272.    Dim i As Integer
  273.    Dim j As Integer
  274.    
  275.    i = 0
  276.    j = 0
  277.    
  278.    i = InStr(s, "\")
  279.    Do While i <> 0
  280.       j = i
  281.       i = InStr(j + 1, s, "\")
  282.    Loop
  283.    
  284.    If j = 0 Then
  285.       GetFile = ""
  286.    Else
  287.       GetFile = Right$(s, Len(s) - j)
  288.    End If
  289. End Function
  290.  
  291. '
  292. '  Returns the path portion of a file + pathname
  293. '
  294. Public Function GetPath(s As String) As String
  295.    Dim i As Integer
  296.    Dim j As Integer
  297.    
  298.    i = 0
  299.    j = 0
  300.    
  301.    i = InStr(s, "\")
  302.    Do While i <> 0
  303.       j = i
  304.       i = InStr(j + 1, s, "\")
  305.    Loop
  306.    
  307.    If j = 0 Then
  308.       GetPath = ""
  309.    Else
  310.       GetPath = Left$(s, j)
  311.    End If
  312. End Function
  313. '
  314. '  Returns a volume's serial number
  315. '
  316. Public Function GetSerialNumber(sDrive As String) As Long
  317.    Dim ser As Long
  318.    Dim s As String * MAX_FILENAME_LEN
  319.    Dim s2 As String * MAX_FILENAME_LEN
  320.    Dim i As Long
  321.    Dim j As Long
  322.    
  323.    Call GetVolumeInformation(sDrive + ":\" & Chr$(0), s, MAX_FILENAME_LEN, ser, i, j, s2, MAX_FILENAME_LEN)
  324.    GetSerialNumber = ser
  325. End Function
  326.  
  327.  
  328. Public Function GetShortPathName(longpath As String) As String
  329.    Dim s As String
  330.    Dim i As Long
  331.    
  332.    i = Len(longpath) + 1
  333.    s = String(i, 0)
  334.    GetShortPathNameA longpath, s, i
  335.    
  336.    GetShortPathName = Left$(s, InStr(s, Chr$(0)) - 1)
  337. End Function
  338.  
  339. Public Function GetVolumeName(sDrive As String) As String
  340.    Dim ser As Long
  341.    Dim s As String * MAX_FILENAME_LEN
  342.    Dim s2 As String * MAX_FILENAME_LEN
  343.    Dim i As Long
  344.    Dim j As Long
  345.    
  346.    Call GetVolumeInformation(sDrive + ":\" & Chr$(0), s, MAX_FILENAME_LEN, ser, i, j, s2, MAX_FILENAME_LEN)
  347.    GetVolumeName = Left$(s, InStr(s, Chr$(0)) - 1)
  348. End Function
  349. '
  350. '  Sets the volume name.  Returns true on success, false on failure.
  351. '
  352. Public Function SetVolumeName(sDrive As String, n As String) As Boolean
  353.    Dim i As Long
  354.    
  355.    i = SetVolumeLabelA(sDrive + ":\" & Chr$(0), n & Chr$(0))
  356.    
  357.    SetVolumeName = IIf(i = 0, False, True)
  358. End Function
  359. '
  360. '  Returns the system directory.
  361. '
  362. Public Function GetSystemDirectory() As String
  363.    Dim s As String
  364.    Dim i As Integer
  365.    i = GetSystemDirectoryA("", 0)
  366.    s = Space(i)
  367.    Call GetSystemDirectoryA(s, i)
  368.    GetSystemDirectory = AddBackslash(Left$(s, i - 1))
  369. End Function
  370.  
  371. '
  372. '  Returns a unique tempfile name.
  373. '
  374. Public Function GetTempFileName() As String
  375.    Dim s As String
  376.    Dim s2 As String
  377.    
  378.    s2 = GetTempPath
  379.    s = Space(Len(s2) + MAX_FILENAME_LEN)
  380.    Call GetTempFileNameA(s2, App.EXEName, UNIQUE_NAME, s)
  381.    GetTempFileName = Left$(s, InStr(s, Chr$(0)) - 1)
  382. End Function
  383.  
  384. '
  385. '  Returns the path to the temp directory.
  386. '
  387. Public Function GetTempPath() As String
  388.    Dim s As String
  389.    Dim i As Integer
  390.    i = GetTempPathA(0, "")
  391.    s = Space(i)
  392.    Call GetTempPathA(i, s)
  393.    GetTempPath = AddBackslash(Left$(s, i - 1))
  394. End Function
  395.  
  396. '
  397. '  Returns the windows directory.
  398. '
  399. Public Function GetWindowsDirectory() As String
  400.    Dim s As String
  401.    Dim i As Integer
  402.    i = GetWindowsDirectoryA("", 0)
  403.    s = Space(i)
  404.    Call GetWindowsDirectoryA(s, i)
  405.    GetWindowsDirectory = AddBackslash(Left$(s, i - 1))
  406. End Function
  407.  
  408. '
  409. '  Removes the backslash from the string if it has one.
  410. '
  411. Public Function RemoveBackslash(s As String) As String
  412.    Dim i As Integer
  413.    i = Len(s)
  414.    If i <> 0 Then
  415.       If Right$(s, 1) = "\" Then
  416.          RemoveBackslash = Left$(s, i - 1)
  417.       Else
  418.          RemoveBackslash = s
  419.       End If
  420.    Else
  421.       RemoveBackslash = ""
  422.    End If
  423. End Function
  424.  
  425. '
  426. ' Returns the drive type if possible.
  427. '
  428. Public Function sDriveType(sDrive As String) As String
  429. Dim lRet As Long
  430.  
  431.     lRet = GetDriveTypeA(sDrive & ":\")
  432.     Select Case lRet
  433.         Case 0
  434.             'sDriveType = "Cannot be determined!"
  435.             sDriveType = "Unknown"
  436.             
  437.         Case 1
  438.             'sDriveType = "The root directory does not exist!"
  439.             sDriveType = "Unknown"
  440.         Case DRIVE_CDROM:
  441.             sDriveType = "CD-ROM Drive"
  442.             
  443.         Case DRIVE_REMOVABLE:
  444.             sDriveType = "Removable Drive"
  445.             
  446.         Case DRIVE_FIXED:
  447.             sDriveType = "Fixed Drive"
  448.             
  449.         Case DRIVE_REMOTE:
  450.             sDriveType = "Remote Drive"
  451.         End Select
  452. End Function
  453.  
  454. Public Function GetDriveType(sDrive As String) As Long
  455.   Dim lRet As Long
  456.   lRet = GetDriveTypeA(sDrive & ":\")
  457.   
  458.   If lRet = 1 Then
  459.      lRet = 0
  460.   End If
  461.  
  462.   GetDriveType = lRet
  463. End Function
  464. '-----------------------------------------------------------
  465. ' FUNCTION: FileExists
  466. ' Determines whether the specified file exists
  467. '
  468. ' IN: [strPathName] - file to check for
  469. '
  470. ' Returns: True if file exists, False otherwise
  471. '-----------------------------------------------------------
  472. '
  473. Public Function FileExists(ByVal strPathName As String) As Integer
  474.     Dim intFileNum As Integer
  475.  
  476.     On Error Resume Next
  477.  
  478.     '
  479.     'Remove any trailing directory separator character
  480.     '
  481.     If Right$(strPathName, 1) = "\" Then
  482.         strPathName = Left$(strPathName, Len(strPathName) - 1)
  483.     End If
  484.  
  485.     '
  486.     'Attempt to open the file, return value of this function is False
  487.     'if an error occurs on open, True otherwise
  488.     '
  489.     intFileNum = FreeFile
  490.     Open strPathName For Input As intFileNum
  491.  
  492.     FileExists = IIf(Err, False, True)
  493.  
  494.     Close intFileNum
  495.  
  496.     Err = 0
  497. End Function
  498.  
  499.