Example of Using FileSystemObject (FSO)

You should also better better blood circulation that will ultimately bring new joys and happiness in bed: Generic Sildenafil Available at the Cheapest Prices Various forms available of this medication Easily approachable via Kamagra online Generic sildenafil in various soft versions let the patients take a sigh of relief after the launch of kamagra products raindogscine.com generic cialis in the market. The “unblocked” nostril deals with the fast dissolving chemicals while the swollen, seemingly redundant nostril buy cialis pharmacy handles the slow dissolving odours. With two Brit Awards for Best best price for cialis Male Solo Artist and Best British Breakthrough Act under his belt, it’s fairly safe to say that Ed Sheeran has arrived on the world stage. Young adults and kids have an amazing reserve of levitra 40 mg http://raindogscine.com/tag/documental-2/ resiliency and can bounce back if healthy directives are taken.

Example of Using FileSystemObject (FSO)

The FileSystemObject is used to gain an access to a computer system. It is a built-in command in the system that VBA can use. It can create new files, folders, directory paths, and access the existing ones. It is a powerful and short code which is handy to use. The FileSystemObject has many usable methods such as CopyFile, DeleteFile, CopyFolder etc. Before using the FileSystemObject , we need to create the Object by using code: Set objFSO = CreateObject(“Scripting.FileSystemObject”). More information about FileSystemObject here.

For example, there are some things below that you can do by using the FileSystemObject in Access VBA:

  • Check if a file or a folder exists
  • Create or rename files or folders
  • Copy files from one folder to another                                                                                                                              

Example of Using  FolderExists, CreateFolder, and CopyFile method of FSO

Public Function CreateBackup() As Boolean
Dim Path, Source, Target As String
Dim objFSO As Object

Source = CurrentDb.Name     ‘current database file with its path
Path = "C:\TestDB"         ‘new location to save
Target = Path & "\New BackupDB" & ".accdb"    ‘assign new location with new file name to Target
Set objFSO = CreateObject("Scripting.FileSystemObject") ‘create FileSystemObject for objFSO object
If objFSO.FolderExists(Path) Then     ‘use FolderExists object test if "C:\TestDB" exists
   a = objFSO.CopyFile(Source, Target, True)   ‘use CopyFile object, True = Override old file
Else
   objFSO.Createfolder (Path)     ‘use CreateFolder object, create folder if no folder yet before copy file
   a = objFSO.CopyFile(Source, Target, True)
End If
Set objFSO = Nothing
Set a = Nothing
End Function

 

Example of Using GetFile, GetFileName, GetExtensionName, and Delete of FSO

Private Sub Command0_Click()
Dim strGetFile, FileName, FileExtension As String
Dim objFSO As Object
Dim FilePath As String

FilePath = "C:\testdb\backupdb_thu.accdb"  ‘ file location
Set objFSO = CreateObject("scripting.FileSystemObject")
       strGetFile = objFSO.Getfile(FilePath)
       MsgBox (strGetFile)  'return "C:\testdb\backupdb_thu.accdb"   
       
FileName = objFSO.GetFileName(FilePath)

      MsgBox (FileName)  'return "backupdb_thu.accdb"

       FileExtension = objFSO.GetExtensionName(FilePath)

       MsgBox (FileExtension)  'return "accdb"

    Set strdeletefile = objFSO.getfile(FilePath)
       MsgBox (strdeletefile)  'return "C:\testdb\backupdb_thu.accdb"
       strdeletefile.Delete    'Delete only file "backupdb_thu.accdb" in the folder

Set objFSO = Nothing
End Sub

Example of Using GetParentFolder, GetFolder, and DeleteFolder of FSO

Private Sub Command0_Click()
Dim strParentFolder, strFolder As String
Dim objFSO As Object
Dim FilePath As String

FilePath = "C:\testdb\New Folder\backupdb_thu.accdb"
Set objFSO = CreateObject("scripting.FileSystemObject")
      strParentFolder = objFSO.GetParentFolderName(FilePath)
      MsgBox (strParentFolder)  'return "C:\testdb\New Folder"     

        strParentFolder = objFSO.GetFolder("C:\TestDB\New folder")
       MsgBox (strParentFolder)  'return "C:\testdb\New Folder"      

        strFolder = objFSO.GetFolder("C:\TestDB\New folder\BackupDB_Thu.accdb")
       MsgBox (strFolder)  'get run-time error: 76 - Path Not found
     
       a = objFSO.DeleteFolder("C:\TestDB\New folder") 'delete sub-folder "New Folder" and all files in that folder, not delete TestDB folder
            
Set objFSO = Nothing
End Sub

 Example of Using CreateTextFile and WriteLine of FSO

Private Sub Command1_Click()
Dim Path As String

Set fs = CreateObject("Scripting.FileSystemObject")

Path = "c:\TestDB\testfile.txt"
     Set a = fs.CreateTextFile(Path, True) 'Create a text file "testfile.txt”
     a.WriteLine ("This is a test.") 'write text "This is a test." in the text file
a.Close
Set fs = Nothing
End Sub

 

The table below is the list of Methods of  FileSystemObject from Microsoft website:

 

Method

Description

BuildPath

Appends a name to an existing path.

CopyFile

Copies one or more files from one location to another.

CopyFolder

Copies one or more folders from one location to another.

CreateFolder

Creates a new folder.

CreateTextFile

Creates a text file and returns a TextStream object that can be used to read from, or write to the file.

DeleteFile

Deletes one or more specified files.

DeleteFolder

Deletes one or more specified folders.

DriveExists

Checks if a specified drive exists.

FileExists

Checks if a specified file exists.

FolderExists

Checks if a specified folder exists.

GetAbsolutePathName

Returns the complete path from the root of the drive for the specified path.

GetBaseName

Returns the base name of a specified file or folder.

GetDrive

Returns a Drive object corresponding to the drive in a specified path.

GetDriveName

Returns the drive name of a specified path.

GetExtensionName

Returns the file extension name for the last component in a specified path.

GetFile

Returns a File object for a specified path.

GetFileName

Returns the file name or folder name for the last component in a specified path.

GetFolder

Returns a Folder object for a specified path.

GetParentFolderName

Returns the name of the parent folder of the last component in a specified path.

GetSpecialFolder

Returns the path to some of Windows’ special folders.

GetTempName

Returns a randomly generated temporary file or folder.

Move

Moves a specified file or folder from one location to another.

MoveFile

Moves one or more files from one location to another.

MoveFolder

Moves one or more folders from one location to another.

OpenAsTextStream

Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.

OpenTextFile

Opens a file and returns a TextStream object that can be used to access the file.

WriteLine

Writes a specified string and new-line character to a TextStream file.

 

Related posts