INFO: Print Out File Properties Using the FileSystemObject
ID: Q190029
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
SUMMARY
This article shows you how to print out file properties, such as the date
created, the short file name, and the size of all files in a specified
folder using the FileSystemObject. The article shows how to create a
project that prints out file information to a specified text file.
MORE INFORMATION
This article assumes you are familiar with using objects, collections, and
object models in Visual Basic. The FileSystemObject allows you to perform a
number of tasks on the file system of a computer. This object is available
to your project by making a reference to the Microsoft Script Runtime file
scrrun.dll. This file ships with Windows 98 and the following products:
Windows Script Host
Windows NT Option Pack
Microsoft Internet Information Server 3.0
Scripting 3.1 upgrade
Visual Studio 98
Visual Basic 6.0
After making a reference to the Script Runtime file, you create an instance
of the FileSystemObject using the CreateObject method. The FileSystemObject
can create a text file with the CreateTextFile method. You write to the
text file using the Write, the WriteBlankLines, or the WriteLine functions.
To get the properties of all files in a specified folder, open the folder
using the GetFolder method. You can then loop through each member of the
Files collection and get the properties of each file.
The next section shows you how to create a sample project that demonstrates
using the FileSystemObject to get the properties of all files in a
specified directory. These properties are then printed to a text file.
Steps to Create Sample Project
- Start a new Standard EXE project in Visual Basic. Form1 is created by
default.
- Add a reference to the Microsoft Script Runtime by completing the
following steps:
- From the Project menu, click References to display the References
dialog box.
- Click Browse to open the Add Reference dialog box.
- Select the file scrrun.dll and click OK to close the Add Reference
dialog box. This file is installed in your system directory.
Microsoft Scripting Runtime appears with a check in the Available
Referenced list box of the References dialog box.
- Click OK to close the References dialog box.
- Add the following controls to Form1:
- CommandButton
- DriveListBox
- DirListBox
- Label
- Copy the following code to the Code window of Form1:
Option Explicit
Private Sub Form_Load()
Label1.AutoSize = True
Label1.WordWrap = True
Label1.Caption = Dir1.Path
Dir1.Path = Drive1.Drive
Command1.Caption = "Create File List"
End Sub
Private Sub Dir1_Change()
Label1.Caption = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim f As Folder
Dim fc As Files
Dim fl As File
Dim ts As TextStream
Dim sFileName As String
Dim sAttributes As String
Set fso = CreateObject("Scripting.FileSystemObject")
sFileName = InputBox("Enter File Name", _
"Enter Path and File Name")
If sFileName = "" Then
Exit Sub
Else
Set ts = fso.CreateTextFile(sFileName, False)
Set f = fso.GetFolder(Dir1.Path)
Set fc = f.Files
With ts
.WriteLine ("Files in " & Dir1.Path)
.WriteBlankLines (1)
For Each fl In fc
.WriteLine ("Filename: " & fl.Name)
.WriteLine (vbTab & "File Type: " & fl.Type)
.WriteLine (vbTab & "Created: " & fl.DateCreated)
.WriteLine (vbTab & "Last Modified: " & _
fl.DateLastModified)
.WriteLine (vbTab & "Last Accessed: " & _
fl.DateLastAccessed)
.WriteLine (vbTab & "Path: " & fl.Path)
.WriteLine (vbTab & "Short Name: " & fl.ShortName)
.WriteLine (vbTab & "Size: " & fl.Size & " bytes")
.WriteBlankLines (1)
Next
End With
MsgBox (sFileName & " created.")
End If
End Sub
- On the Run menu, click Start or press the F5 key to start the program.
- Select a drive and a folder. The path appears in the Label control.
- Click the Create a file list CommandButton. An input dialog box
appears.
- Type the path and file name of the text file that will contain the
file properties. Click OK to close the input box.
A message box appears when the text file is created. Open the text file
to view the results.
REFERENCES
For additional information, please see the following articles in the
Microsoft Knowledge Base:
Q186118
: HOWTO: Use FileSystemObject with Visual Basic
Q185601
: HOWTO: Recursively Search Directories Using FileSystemObject
Additional query words:
kbDSupport kbDSD kbVBp kbVBp500 kbVBp600 kbFileIO
Keywords : kbGrpVB
Version : WINDOWS:5.0
Platform : WINDOWS
Issue type : kbinfo