Microsoft KB Archive/190029

= INFO: Print Out File Properties Using the FileSystemObject =

Article ID: 190029

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition

-



This article was previously published under Q190029



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.</ol> </li> Add the following controls to Form1: <ul> CommandButton</li> DriveListBox</li> DirListBox</li> Label</li></ul> </li>  Copy the following code to the Code window of Form1: Option Explicit Private Sub Form_Load Label1.AutoSize = 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 sVersion 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, True, 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) ' Get Version Information sVersion = fso.GetFileVersion(fl.Path) If sVersion <> "" Then .WriteLine (vbTab & "Version: " & fso.GetFileVersion(fl.Path)) Else .WriteLine (vbTab & "No Version Information") End If           .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  'Destroy All Objects Set fc = Nothing Set f = Nothing Set ts = Nothing Set fso = Nothing End Sub </li> On the Run menu, click Start or press the F5 key to start the program.

<ol style="list-style-type: lower-alpha;"> Select a drive and a folder. The path appears in the Label control.</li> Click the Create a file list CommandButton. An input dialog box appears.</li> Type the path and file name of the text file that will contain the file properties. Click OK to close the input box.</li></ol>

A message box appears when the text file is created. Open the text file to view the results.</li></ol>

<div class="references_section">