|
Tipp 0318
|
Datei-Eigenschaften ermitteln (FSO)
|
|
|
Autor/Einsender: Datum: |
|
Peter Wagenbauer 09.03.2003 |
|
Entwicklungsumgebung: |
|
VB 5 |
|
|
Ab Visual Basic 6 steht standardmäßig das FileSystemObject (FSO) zur
Verfügung, mit dem auch ein komfortabler Umgang mit Ordnern und Dateien möglich ist.
Mit folgendem Beispiel werden verschiedene Datei-Eigenschaften ermittelt, wie z. B.
Datei-Typ, Pfad, Dateiname, Größe, MS-DOS-Dateiname, usw.
|
|
|
Option Explicit
Private fso As FileSystemObject
Private Sub Form_Load()
Set fso = New FileSystemObject
With File1
.Archive = True
.Hidden = True
End With
Dir1.Path = App.Path
If File1.ListCount > 0 Then
File1.ListIndex = 0
End If
End Sub
Private Sub Drive1_Change()
On Error Resume Next
Err.Clear
Dir1.Path = Drive1.Drive
If Err.Number = 68 Then
MsgBox "Das Gerät ist nicht bereit!", vbOKOnly + vbCritical
Drive1.Drive = Dir1.Path
End If
On Error GoTo 0
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
If File1.ListCount > 0 Then
File1.ListIndex = 0
ResizeForm True
Else
Me.Caption = "Datei-Eigenschaften ermitteln (FSO)"
ResizeForm False
End If
End Sub
Private Sub File1_Click()
Dim sFileName As String
Dim File As File
sFileName = File1.Path & "\" & File1.FileName
On Error Resume Next
Set File = fso.GetFile(sFileName)
GetFileProperties File
Me.Caption = "Datei-Eigenschaften von " & File1.FileName
Set File = Nothing
End Sub
Private Sub GetFileProperties(ByVal File As File)
Dim varTemp As Variant
On Error GoTo err_Handler
varTemp = CStr(File.Type)
txtType.Text = varTemp
varTemp = CStr(File.ParentFolder)
txtParentFolder.Text = varTemp
varTemp = CStr(File.Name)
txtName.Text = varTemp
varTemp = CStr(File.ShortName)
txtShortName.Text = varTemp
varTemp = CStr(Format(File.Size, "#,##0")) & " Bytes"
txtSize.Text = varTemp
varTemp = CStr(File.DateCreated)
txtDateCreated.Text = varTemp
varTemp = CStr(File.DateLastModified)
txtDateLastModified.Text = varTemp
varTemp = CStr(File.DateLastAccessed)
txtDateLastAccessed.Text = varTemp
On Error GoTo 0
chkReadOnly.Value = Abs(CBool(File.Attributes And ReadOnly))
chkArchive.Value = Abs(CBool(File.Attributes And Archive))
chkCompressed.Value = Abs(CBool(File.Attributes And Compressed))
chkHidden.Value = Abs(CBool(File.Attributes And Hidden))
Exit Sub
err_Handler:
varTemp = "Unknown"
Resume Next
End Sub
Private Sub ResizeForm(ByVal vboolFilesExist As Boolean)
If vboolFilesExist Then
Me.Height = 7485
frameInfos.Visible = True
Else
Me.Height = 3315
frameInfos.Visible = False
End If
With cmdBeenden
.Move .Left, Me.ScaleHeight - .Height - 120
End With
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
Set fso = Nothing
End
End Sub
|
|
|
|
Um diesen Tipp ausführen zu können, muss die Bibliothek Microsoft Scripting Runtime
(SCRRUN.DLL) in das Projekt eingebunden werden.
|
|
|
|
Windows-Version |
95 |
|
|
98/SE |
|
|
ME |
|
|
NT |
|
|
2000 |
|
|
XP |
|
|
Vista |
|
|
Win
7 |
|
|
|
VB-Version |
VBA 5 |
|
|
VBA 6 |
|
|
VB 4/16 |
|
|
VB 4/32 |
|
|
VB 5 |
|
|
VB 6 |
|
|
|
|
Download (4,4 kB)
|
Downloads bisher: [ 2722 ]
|
|
|