VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"FileSystemObject" Objects to Manage File Systems

This section describes FileSystemObject properties and methods. A tutorial example is provided on how to create a folder, copy files, and get file information.

FileSystemObject: An object representing the local file system Major methods and properties include:

  • "Drives": Property to return a read-only collection of "Drive" objects representing all drives in the current file system.
  • "GetDrive(drivePath)": Method to return a Drive object of the specified drive path (driver letter with ':').
  • "GetFolder(folderPath)": Method to return a Folder object of the specified folder path name.
  • "GetFile(filePath)": Method to return a File object of the specified file path name.
  • "CreateTextFile(pathname,[override[,unicode]])": Method to create a new text file and return a TextStream object connected to the new file. "override=true|false" specifies if the new file can override an existing file. "unicode=false|true" specifies if the new should written with unicode encoding.
  • "OpenTextFile(pathname[,io_mode[,create[,tristate]]])": Method to open a file and returns a TextStream object connected to the file. "create=false|true" specifies that if a new can be created when the specified file does not exists. io_mode(=1|2|8: ForReading|ForWriting|ForAppending) specifies how the file should be openned. tristate(=-2|-1|0: TristateUserDefault|TristateTrue|TristateFalse) speficies if the file should be open as Unicode or not.
  • "CopyFile(from_pathname, to_pathname)": Method to copy a file specified by the from_pathname to the to_pathname.
  • "DeleteFile()": Method to delete a file.
  • "MoveFile()": Method to move a file.
  • "FileExists()": Method to check if a file exists.
  • "CreateFolder": Method to create a folder.
  • "CopyFolder()": Method to copy a folder.
  • "DeleteFolder()": Method to delete a folder.
  • "MoveFolder()": Method to move a folder.
  • "FolderExists()": Method to check if a folder exists.
  • "DriveExists": Method to check if a drive exists.

Below is a VBScript example showing you how to create a folder and copy a file by using the FileSystemObject object:

<html>
<body>
<!-- file_system_test.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   ' Getting the FileSystemObject object
   set fs = CreateObject("Scripting.FileSystemObject")

   ' Creating a new file folder
   if fs.FolderExists("c:\temp\backup") then 
      fs.DeleteFolder("c:\temp\backup")
   end if
   set d = fs.CreateFolder("c:\temp\backup")

   ' Copying a file
   fs.CopyFile "c:\windows\system32\scrrun.dll", "c:\temp\backup\"

   ' Getting information about the copy
   set f = fs.GetFile("c:\temp\backup\scrrun.dll")
   document.writeln("f.Path: " & f.Path)
   document.writeln("f.ParentFolder: " & f.ParentFolder)
   document.writeln("f.Name: " & f.Name)
   document.writeln("f.Type: " & f.Type)
   document.writeln("f.Size: " & f.Size)
   document.writeln("f.DateCreated: " & f.DateCreated)
   document.writeln("f.DateLastModified: " & f.DateLastModified)
   document.writeln("f.DateLastAccessed: " & f.DateLastAccessed)
</script>
</pre>
</body>
</html>

When you load this example into IE, you will get a security warning. You can ignore it and get the output:

f.Path: C:\temp\backup\scrrun.dll
f.ParentFolder: C:\temp\backup
f.Name: scrrun.dll
f.Type: Application Extension
f.Size: 151552
f.DateCreated: 7/10/2006 10:30:23 PM
f.DateLastModified: 8/4/2004 4:00:00 AM
f.DateLastAccessed: 7/10/2006 10:30:23 PM

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

scrrun.dll - Scripting Runtime DLL Library

 Scripting Runtime DLL Library Overview

 "Dictionary" Objects to Store Keys and Values

"FileSystemObject" Objects to Manage File Systems

 "Drive" Objects Representing Disk Drives

 "Folder" Objects Representing File Folders

 "File" Objects Representing Files

 "TextStream" Objects Representing File Input and Output

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"FileSystemObject" Objects to Manage File Systems - Updated in 2015, by Dr. Herong Yang