Accessing the File System
The FileSystemObject Object
The FileSystemObject object lets you create and work with files and directories on the server. For example, you can create a file, write data to it and then delete it when the file is no longer necessary. You can also determine whether or not certain files exist, as well as their locations, extensions and other information.
FileSystemObject Object: Methods |
BuildPath |
Adds a folder or file name to a path. |
CopyFile |
Copies a file from one location to another. |
CopyFolder |
Copies a folder and all its files from one location to another. |
CreateFolder |
Creates a new folder. |
CreateTextFile |
Creates a new text file using the TextStream object. |
DeleteFile |
Deletes a file. |
DeleteFolder |
Deletes a folder and all its files. |
DriveExists |
Determines whether or not a drive exists on the server. |
FileExists |
Determines whether or not a file exists on the server. |
FolderExists |
Determines whether or not a folder exists on the server. |
GetAbsolutePathName |
Returns the full path (from the root) for the specified folder or file. |
GetBaseName |
Returns the base name (without the extension) of the specified file or folder. |
GetDrive |
Creates an instance of the Drive object. |
GetDriveName |
Returns the drive containing the specified file. |
GetExtensionName |
Returns the extension of the specified file. |
GetFile |
Creates an instance of the File object. |
GetFileName |
Returns the file or folder name specified in a path. |
GetFolder |
Creates an instance of the Folder object. |
GetParentFolderName |
Returns the name of the parent folder for the specified file. |
GetSpecialFolder |
Returns the path of a special Windows folder. 0 returns the path for the folder containing the operating system; 1 returns the path for the folder containing libraries and drivers; 2 returns the path for the Temp folder. |
GetTempName |
Generates a temporary file or folder with a random name. |
MoveFile |
Moves a file from one location to another on the server. |
MoveFolder |
Moves a folder and all its files from one location to another on the server. |
OpenTextFile |
Opens a file using the TextStream object. |
FileSystemObject Object: Properties |
Drives |
Returns a collection of Drive objects. |
The following code determines whether or not a certain file (temp.txt) exists on the server and, if it does, deletes it:
<% Dim varFileSystem Set varFileSystem=Server.CreateObject(“Scripting.FileSystemObject”) If varFileSystem.FileExists(“c:\inetpub\wwwroot\temp.txt”)=true then varFileSystem.DeleteFile(“c:\inetpub\wwwroot\temp.txt”) End If %> |
Once you’ve created a FileSystemObject object, you can use its methods to create instances of the TextStream object, the File object, the Folder object and the Drive object. All of these have methods and properties of their own that let you create and manipulate specific files, folders and drives on the server.
The TextStream Object
The TextStream object lets you create and work with a text file.
TextStream Object: Methods |
Close |
Closes the text file. |
Read |
Returns a specified number of characters from a text file. |
ReadAll |
Returns an entire text file. |
ReadLine |
Returns a line from a text file. |
Skip |
Skips a specified number of characters when reading a text file. |
SkipLine |
Skips a line when reading a text file. |
Write |
Writes text to a text file. |
WriteLine |
Writes a line of text to a text file. |
WriteBlankLine |
Writes the specified number of blank lines to a text file. |
TextStream Object: Properties |
AtEndOfLine |
Is “true” if the location is just before the end of a line in a text file. |
AtEndOfStream |
Is “true” if the location is at the end of a text file. |
Column |
Returns the column number of the current character. |
Line |
Returns the line number of the current position. |
The following code creates a text file, writes a line that contains title text, creates two blank lines, writes heading text using tabs (the ASCII code chr(9)) between the headings, and then creates another blank line before closing the file:
<% Dim varFileSystem, varTextStream Set varFileSystem=Server.CreateObject(“Scripting.FileSystemObject”) Set varTextStream=varFileSystem.CreateTextFile(“c:\plist.txt”) varTextStream.WriteLine “Company Call List” varTextStream.WriteBlankLines(2) varTextStream.Write “Name” & chr(9) varTextStream.Write “Employee ID” & chr(9) varTextStream.Write “Phone Number” varTextStream.WriteBlankLines(1) varTextStream.Close %> |
You must close the file in order to allow users to access it. Once you’ve created a text file, you can use the TextStream object to open the file and display its contents in an ASP page:
<% Dim varFileSystem, varTextStream Set varFileSystem=Server.CreateObject(“Scripting.FileSystemObject”) Set varTextStream=varFileSystem.OpenTextFile(“c:\plist.txt”) Response.Write(varTextStream.ReadAll) %> |
The File Object
You use the File object to return information about files on the server, and to copy, move and delete files as necessary.
File Object: Methods |
Copy |
Copies a file from one location to another. |
Delete |
Deletes a file. |
Move |
Moves a file from one location to another. |
OpenAsTextStream |
Opens the file using an instance of the TextStream object. |
File Object: Properties |
Attributes |
Specifies or returns attributes of the specified file. |
DateCreated |
Returns the date and time a file was created. |
DateLastAccessed |
Returns the date and time a file was last accessed. |
DateLastModified |
Returns the date and time a file was last modified. |
Drive |
Returns the letter of the drive where a specified file resides. |
Name |
Specifies or returns the name of a file. |
ParentFolder |
Returns the parent object for a file using an instance of the Folder object. |
Path |
Returns the path for a file. |
ShortName |
Returns the short name of a file. |
ShortPath |
Returns the short path of a file. |
Size |
Returns the size of a file. |
Type |
Returns the type of a file. |
The following code copies a file to an archives folder and then deletes the original file:
<% Dim varFileSystem,varFile Set varFileSystem=Server.CreateObject(“Scripting.FileSystemObject”) Set varFile=varFileSystem.GetFile(“c:\plist.txt”) varFile.Copy(“c:\archives\plist.text”) varFile.Delete Set varFile=nothing Set varFileSystem=nothing %> |
The Folder Object
You use the Folder object to return information about folders on the server, and to copy, move and delete folders as necessary.
Folder Object: Methods |
Copy |
Copies a folder from one location to another. |
Delete |
Deletes a folder. |
Move |
Moves a folder from one location to another. |
CreateTextFile |
Creates a text file in the specified folder using an instance of the TextStream object. |
Folder Object: Properties |
Attributes |
Specifies or returns attributes of the specified folder. |
DateCreated |
Returns the date and time a folder was created. |
DateLastAccessed |
Returns the date and time a folder was last accessed. |
DateLastModified |
Returns the date and time a folder was last modified. |
Drive |
Returns the letter of the drive where a specified folder resides. |
IsRootFolder |
Is “true” if the folder is a root folder. |
Name |
Specifies or returns the name of a folder. |
ParentFolder |
Returns the parent folder. |
Path |
Returns the path for a folder. |
ShortName |
Returns the short name of a folder. |
ShortPath |
Returns the short path of a folder. |
Size |
Returns the size of a folder. |
Type |
Returns the type of a folder. |
The following code displays the date the archives folder was last accessed:
<% Dim varFileSystem,varFolder Set varFileSystem=Server.CreateObject(“Scripting.FileSystemObject”) Set varFolder=varFileSystem.GetFolder(“c:\archives”) Response.Write(varFolder.DateLastAccessed) Set varFolder=nothing Set varFileSystem=nothing %> |
The Drive Object
You use the Drive object to return information about a local or network drive.
Drive Object: Properties |
AvailableSpace |
Returns the amount of available space on the specified drive. |
DriveLetter |
Returns the drive letter of the specified drive. |
DriveType |
Returns the type of the specified drive. |
FileSystem |
Returns the file system in use on the drive. |
FreeSpace |
Returns the amount of free space on the specified drive. |
IsReady |
Is “true” if the specified drive is ready. |
Path |
Returns the path for the specified drive. |
RootFolder |
Returns the root folder of the specified drive using an instance of the Folder object. |
SerialNumber |
Returns the serial number of the specified drive. |
ShareName |
Returns the network share name of the specified drive. |
TotalSize |
Returns the total size of the specified drive. |
VolumeName |
Specifies or returns the volume name of the drive. |
The following code displays a drive’s available space:
<% Dim varFileSystem,varDrive Set varFileSystem=Server.CreateObject(“Scripting.FileSystemObject”) Set varDrive=varFileSystem.GetDrive(“c:”) Response.Write(“Available space on Drive ” & varDrive & “:”) Response.Write(varDrive.AvailableSpace) set varDrive=nothing set varFileSystem=nothing %> |
Pages:
1 2 3 4 5 6 7 8 9 10 11 12 13 14