"File" Objects Representing Files
This section describes File object and its properties and methods. A tutorial example is provided on how to list all properties of a file.
File: An object representing a file in a file system. It offers the following
properties and methods:
- "Attributes": Property to return the attributes of the file.
- "DateCreated": Property to return the date and time when the file was created.
- "DateLastAccessed": Property to return the date and time when the file was last accessed.
- "DateLastModified": Property to return the date and time when the file was last modified.
- "Drive": Property to return the driver letter of the file.
- "Name": Property to return the name of the file.
- "ParentFolder": Property to return the "Folder" object of the parent folder.
- "Path": Property to return the path string of the file.
- "ShortName": Property to return the name of the file in 8.3 format.
- "ShortPath": Property to return the path string of the file in 8.3 format.
- "Size": Property to return the size of the file in bytes.
- "Copy(destination[,true|false])": Method to copy the file to the specified destination. The optional
argument [true|false] specifies that the copy operation will override the existing file.
- "Delete([true|false])": Method to delete the file. The optional [true|false] argument specifies
that the delete operation will delete the read-only file.
- "Move(destination)": Method to move the file to the specified destination.
- "OpenAsTextStream([io_mode[,tristate]])": Method to open the file and return
a TextStream object representing the I/O stream to that file.
io_mode(=1|2|8: ForReading|ForWriting|ForAppending) specifies how the file should be opened.
tristate(=-2|-1|0: TristateUserDefault|TristateTrue|TristateFalse) specifies if the file should
be open as Unicode or not.
Here is a VBScript example showing you how to get detailed information about a file.
- Copyright (c) 2002 HerongYang.com. All Rights Reserved.
' Creating a FileSystemObject object
set fs = CreateObject("Scripting.FileSystemObject")
' Display detailed information about this file
set f = fs.GetFile("c:\winnt\system32\scrrun.dll")
document.writeln("Path = " & f.Path)
document.writeln("Name = " & f.Name)
document.writeln("Short Name = " & f.ShortName)
document.writeln("Short Path = " & f.ShortPath)
document.writeln("Drive = " & f.Drive)
document.writeln("Size = " & f.Size)
document.writeln("Attributes = " & f.Attributes)
document.writeln("Date Created = " & f.DateCreated)
When you load this example into IE, you will get a security warning.
You can ignore it and get the output:
Tests on the File class:
Path = C:\WINNT\system32\scrrun.dll
Name = scrrun.dll
Short Name = scrrun.dll
Short Path = C:\winnt\system32\scrrun.dll
Drive = c:
Size = 147512
Attributes = 32
Date created = 6/26/2001 6:06:58 PM
Table of Contents
About This Book
Introduction of VBScript - Visual Basic Scripting Edition
Variant Data Type, Subtypes, and Literals
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
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)
Full Version in PDF/EPUB