Methods and Properties of the File SystemObject

The FileSystemObject is an ActiveX control that provides access to a computer's file system. This object offers a number of methods, some of which are shown in Table 8-3. Table 8-3: Selected methods of the FileSystemObject

Method Description

FileExists Returns True if the specified file exists

Sub FileExists() Dim fs As Object Dim strFile As String

Set fs = CreateObject("Scripting.FileSystemObject") strFile = InputBox("Enter the full name of the file:") If fs.FileExists(strFile) Then MsgBox strFile & " was found."

Else

MsgBox "File does not exist." End If End Sub

GetFile Returns a File object

GetFileName Returns the filename with a path

GetFileVersion Returns the file version

CopyFile Copies a file

Sub CopyFile()

Dim fs As Object Dim strFile As String Dim strNewFile As String strFile = "C:\Hello.doc"

strNewFile = "C:\Program Files\Hello.doc"

Set fs = CreateObject("Scripting.FileSystemObject") fs.CopyFile strFile, strNewFile MsgBox "A copy of the specified file was created." Set fs = Nothing End Sub

MoveFile Moves a file

DeleteFile Deletes a file

Sub DeleteFile()

Dim fs As FileSystemObject Set fs = New FileSystemObject fs.DeleteFile "C:\Program Files\Hello.doc" MsgBox "The requested file was deleted." End Sub

Method Description

DriveExists Returns True if the specified drive exists

Function DriveExists(disk) Dim fs As Object Dim strMsg As String

Set fs = CreateObject("Scripting.FileSystemObject") If fs.DriveExists(disk) Then strMsg = "Drive " & UCase(disk) & " exists." Else strMsg = UCase(disk) & " was not found." End If

DriveExists = strMsg ' run this function from the worksheet

' by entering in any cell the following: =DriveExists("E:\") End Function GetDrive Returns a Drive object

Sub DriveInfo()

Dim fs, disk, infoStr, strDiskName strDiskName = InputBox("Enter the drive letter:", _ "Drive Name", "C:\")

Set fs = CreateObject("Scripting.FileSystemObject") Set disk = fs.GetDrive(fs.GetDriveName(strDiskName)) infoStr = "Drive: " & UCase(strDiskName) & vbCrLf infoStr = infoStr & "Drive letter: " & _

UCase(disk.DriveLetter) & vbCrLf infoStr = infoStr & "Drive Type: " & disk.DriveType & vbCrLf infoStr = infoStr & "Drive File System: " & _

disk.FileSystem & vbCrLf infoStr = infoStr & "Drive SerialNumber: " & _

disk.SerialNumber & vbCrLf infoStr = infoStr & "Total Size in Bytes: " & _

FormatNumber(disk.TotalSize / 1024, 0) & " Kb" & vbCrLf infoStr = infoStr & "Free Space on Drive: " & _

FormatNumber(disk.FreeSpace / 1024, 0) & " Kb" & vbCrLf MsgBox infoStr, vbInformation, "Drive Information" End Sub

GetDriveName Returns a string containing the name of a drive or network share

Function DriveName(disk) Dim fs As Object Dim strDiskName As String

Set fs = CreateObject("Scripting.FileSystemObject") strDiskName = fs.GetDriveName(disk) DriveName = strDiskName ' run this function from the Immediate window ' by entering ?DriveName("D:\") End Function

Method Description

Returns True if the specified folder exists

Sub DoesFolderExist() Dim fs As Object

Set fs = CreateObject("Scripting.FileSystemObject") MsgBox fs.FolderExists("C:\Program Files") End Sub Returns a Folder object Sub FilesInFolder() Dim fs As Object Dim objFolder As Object Dim objFile As Object

Set fs = CreateObject("Scripting.FileSystemObject") Set objFolder = fs.GetFolder("C:\")

Workbooks.Add

For Each objFile In objFolder.Files ActiveCell.Select Selection.Formula = objFile.Name ActiveCell.Offset(0, 1).Range("A1").Select Selection.Formula = objFile.Type ActiveCell.Offset(1, -1).Range("A1").Select

Next

Columns("A:B").Select Selection.Columns.AutoFit End Sub

GetSpecial- Returns the path to the operating system folders: Folder 0 - Windows folder

1 - System folder

2 - Temp folder

Sub SpecialFolders() Dim fs As Object Dim strWindowsFolder As String Dim strSystemFolder As String Dim strTempFolder As String

Set fs = CreateObject("Scripting.FileSystemObject") strWindowsFolder = fs.GetSpecialFolder(0) strSystemFolder = fs.GetSpecialFolder(1) strTempFolder = fs.GetSpecialFolder(2)

MsgBox strWindowsFolder & vbCrLf _ & strSystemFolder & vbCrLf _ & strTempFolder, vbInformation + vbOKOnly, _ "Special Folders"

End Sub

FolderExists

GetFolder

Method Description

CreateFolder Creates a folder

Sub MakeNewFolder() Dim fs, objFolder

Set fs = CreateObject("Scnpting.FileSystemObject") Set objFolder = fs.CreateFolder("C:\TestFolder") MsgBox "A new folder named " & _ objFolder.Name & " was created."

End Sub

CopyFolder Creates a copy of a folder

Sub MakeFolderCopy()

Dim fs As FileSystemObject Set fs = New FileSystemObject If fs.FolderExists("C:\TestFolder") Then fs.CopyFolder "C:\TestFolder", "C:\FinalFolder" MsgBox "The folder was copied." End If

End Sub

MoveFolder Moves a folder

DeleteFolder Deletes a folder

Sub RemoveFolder()

Dim fs As FileSystemObject Set fs = New FileSystemObject

If fs.FolderExists("C:\TestFolder") Then fs.DeleteFolder "C:\TestFolder" MsgBox "The folder was deleted." End If

End Sub

CreateTextFile Creates a text file OpenTextFile Opens a text file

Sub ReadTextFile() Dim fs As Object Dim objFile As Object Dim strContent As String Dim strFileName As String strFileName = "C:\WINNT\System.ini"

Set fs = CreateObject("Scnpting.FileSystemObject")

Set objFile = fs.OpenTextFile(strFileName)

Do While Not objFile.AtEndOfStream strContent = strContent & objFile.ReadLine & vbCrLf

Loop objFile.Close Set objFile = Nothing ActiveWorkbook.Sheets(3).Select Range("A1").Select Selection.Formula = strContent End Sub

The FileSystemObject has only one property called Drives, which returns a reference to the collection of drives. Using this property. you can create a list of drives on a computer, as shown below:

Sub DrivesList() Dim fs As Object Dim colDrives As Object Dim strDrive As String

Set fs = CreateObject("Scripting.FileSystemObject") Set colDrives = fs.Drives

For Each Drive In colDrives strDrive = "Drive " & Drive.DriveLetter & ": " Debug.Print strDrive

Next

End Sub

0 0

Post a comment