FileSystemObject
Home Up Past Meetings Notes Tips Directory Links of Interest Site Map FAQs

Help on the FileSystemObject - part of a library to work with the Windows File System.

See Syntax for: Folder Tools, File Tools, FileProperties, Drive Tools, TextStream Tools

Example Functions: mshShowDriveList

 
MSKB: How To Use FileSystemObject with Visual Basic
MSDN: Reference to Properties, Methods, Objects, Collections and Basics.
MSDN: FileSystemObject Sample Code
Demonstrates the following
'  - FileSystemObject.DriveExists
'  - FileSystemObject.FolderExists
'  - FileSystemObject.CreateFolder
'  - FileSystemObject.CreateTextFile
'  - Folders.Add
'  - Folder.CreateTextFile
'  - TextStream.WriteLine
'  - TextStream.Close
MSDN: Working with Files, Folders, and Drives: More VBA Tips and Tricks (2000)   (VBA techniques without FileSystemObject)

Notes by Stephen Rasey:

In the Visual Basic Editor: Goto Tools, Refrences.....
Check on "Microsoft Scripting Runtime"     which is scrrun.dll.

Dim fso as New FileSystemObject    
Dim strFolderPath as string       
Dim strFullPath as string

strFolderPath = "D:\data\case2"         'a valid path
strFolderPath = "D:\data\case2\"        'a valid path can end in a "\"
strFullPath = strFolderPath & "\" & "WantedFile.txt"
If fso.folderexists(FolderPath) then
    If fso.FileExists(strFullPath) Then
    	'OK to access strFullPath 
    Else
    	'Folder Exists, but file not found
    End IF
Else
	'Folder cannot be found
End if

	
'Alternative Method of creating FSO
Dim fso as object
Set fso = CreateObject("Scripting.FileSystemObject")
 
Sub TestFSOproperty()
    Dim fso As New FileSystemObject      'Reference: Microsoft Scripting Runtime.
    Dim vout As Variant
    
'Folder and Path Tools
    'v = fso.CopyFolder(strSource, strDestPath, [bOverWrite])
    'asFolder = fso.CreateFolder(strPath)
    'v = fso.DeleteFolder(strFolderSpec, [bBForce])
    'bol = fso.FolderExists(strFolderSpec)
    'strAbsPath = fso.GetAbsolutePathName(strPath)
    'asStr = fso.BuildPath(Path, Name)   'Path does not need to exist.
    'asFolder = fso.GetFolder(strFolderPath)
    'strPathParent = fso.GetParentFolderName(strPath)   'Does not check for existance.
    'asfolder = fso.GetSpecialFolder(i)  'i=0 (Windows Opsys Folder, =1 System, 2=Temp)
    'v = fso.MoveFolder(StrSource, strDestPath)

'File & Filename tools
    'v = fso.DeleteFile(strFileSpec, [bForce])   'Force = True will delete ReadOnly.
    'bol = fso.FileExists(strFileSpec)
    'strBase = fso.GetBaseName(Path)     'Returns the filename part of the FullPath w/o extension.
    'vout = fso.GetBaseName("D:\data\case2\0409\xxx.xls")   'returns "xxx"   Existance is not tested.
    'strExt = fso.GetExtensionName(strPath)
    'asFile = fso.GetFile(FilePath)     'error if not exist.
    'strNameAndExt = fso.GetFileName(strPath)   'Does not check existance.
    'vout = fso.GetFileName("D:\data\case2\0409\xxx.xls")  'returns "xxx.xls"
    'v = fso.CopyFile(strSource, strDestPath, [bOverWrite])
    'v = fso.MoveFile(Source, strDestPath)      'Wildcards in source's last component only.
                                              'No wildcards in strDestPath.
           'If Source has wildcards or DestPath ends in "\",
           'then Dest is treated as a FOLDER.
           'Stops on first error, no rollback.
'File Properties
    'strVersion = fso.GetFileVersion(strPath)
'Drive tools
    'bol = fso.DriveExists(strDriveSpec)   'Or (strFileSpec)
    'DriveCollection = fso.Drives(Item(Key))   ' MS VBA help show no arguments.
    'drive1 = fso.GetDrive(strDriveSpec)
    'str1 = fso.GetDriveName(strPath)   'Does not check for existance.

    
'TextStream functions.
    'asTextStream = fso.GetStandardStream(standardStreamType, [bunicode])
    'AsTextStream = fso.CreateTextFile(strFilename, bOverWrite, bUnicode)
    'strFileName = fso.GetTempName    'generates random name for a temp File.   Use with CreateTextFile
    'asTextStream=fso.OpenTextFile(strFilePath, [ForAppendReadWrite], bCreate, FormatTristate])
    

    Set fso = Nothing
End Sub

Code Examples (Public Domain or Fair Use)

Sub mshShowDriveList                           'msh = MicroSoft Help example
	'from MS VBA Help - Drives Property
    Dim fs, d, dc, s, n
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set dc = fs.Drives
    For Each d in dc
        s = s & d.DriveLetter & " - " 
        If d.DriveType = 3 Then
            n = d.ShareName
        Else
            n = d.VolumeName
        End If
        s = s & n & vbCrLf
    Next
    MsgBox s
End Sub

For questions or comments concerning content on this website: Stephen Rasey
Design of this site by Cheryl D. Wise
Copyright © 2000-2004 by WiserWays. All rights reserved.
Revised: 2005-07-10 01:09 .