Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Web servers drives

Status
Not open for further replies.

shiggyshag

Programmer
Dec 14, 2001
227
GB
Hi

I wish to have the Drives of my Web server display on an ASP page.

please help

Cheers
 
Look up the file system object.

DIM fso

SET fso = Server.CreateObject("scripting.filesystemobject")

Heres some examples cut and pasted from a help file

Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close

Function ShowDateCreated(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowDateCreated = f.DateCreated
End Function

Function ShowFreeSpace(drvPath)
Dim fso, d, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
s = "Drive " & UCase(drvPath) & " - "
s = s & d.VolumeName & &quot;<BR>&quot;
s = s & &quot;Free Space: &quot; & FormatNumber(d.FreeSpace/1024, 0)
s = s & &quot; Kbytes&quot;
ShowFreeSpace = s
End Function

Function ShowDateCreated(folderspec)
Dim fso, f
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f = fso.GetFolder(folderspec)
ShowDateCreated = f.DateCreated
End Function

Dim fso, MyFile
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set MyFile= fso.CreateTextFile(&quot;c:\testfile.txt&quot;, True)
MyFile.WriteLine(&quot;This is a test.&quot;)
MyFile.Close

The FileSystemObject (FSO) object model allows you to use the familiar object.method syntax with a rich set of properties, methods, and events to process folders and files.

Use this object-based tool with:

HTML to create Web pages
Windows Scripting Host to create batch files for Microsoft Windows
Script Control to provide a scripting capability to applications developed in other languages
Because use of the FSO on the client side raises serious security issues about providing potentially unwelcome access to a client's local file system, this documentation assumes use of the FSO object model to create scripts executed by Internet Web pages on the server side. Since the server side is used, the Internet Explorer default security settings do not allow client-side use of the FileSystemObject object. Overriding those defaults could subject a local computer to unwelcome access to the file system, which could result in total destruction of the file system's integrity, causing loss of data, or worse.

The FSO object model gives your server-side applications the ability to create, alter, move, and delete folders, or to detect if particular folders exist, and if so, where. You can also find out information about folders, such as their names, the date they were created or last modified, and so forth.

The FSO object model also makes it easy to process files. When processing files, the primary goal is to store data in a space- and resource-efficient, easy-to-access format. You need to be able to create files, insert and change the data, and output (read) the data. Since storing data in a database, such as Access or SQL Server, adds a significant amount of overhead to your application, storing your data in a binary or text file may be the most efficient solution. You may prefer not to have this overhead, or your data access requirements may not require all the extra features associated with a full-featured database.

The FSO object model, which is contained in the Scripting type library (Scrrun.dll), supports text file creation and manipulation through the TextStream object. Although it does not yet support the creation or manipulation of binary files, future support of binary files is planned.


--------------------------------------------------------------------------------

© 2001 Microsoft Corporation. All rights reserved.

Build: Topic Version 5.6.9309.1546


The FileSystemObject (FSO) object model contains the following objects and collections.

Object/Collection Description
FileSystemObject Main object. Contains methods and properties that allow you to create, delete, gain information about, and generally manipulate drives, folders, and files. Many of the methods associated with this object duplicate those in other FSO objects; they are provided for convenience.
Drive Object. Contains methods and properties that allow you to gather information about a drive attached to the system, such as its share name and how much room is available. Note that a &quot;drive&quot; isn't necessarily a hard disk, but can be a CD-ROM drive, a RAM disk, and so forth. A drive doesn't need to be physically attached to the system; it can be also be logically connected through a network.
Drives Collection. Provides a list of the drives attached to the system, either physically or logically. The Drives collection includes all drives, regardless of type. Removable-media drives need not have media inserted for them to appear in this collection.
File Object. Contains methods and properties that allow you to create, delete, or move a file. Also allows you to query the system for a file name, path, and various other properties.
Files Collection. Provides a list of all files contained within a folder.
Folder Object. Contains methods and properties that allow you to create, delete, or move folders. Also allows you to query the system for folder names, paths, and various other properties.
Folders Collection. Provides a list of all the folders within a Folder.
TextStream Object. Allows you to read and write text files.



--------------------------------------------------------------------------------

© 2001 Microsoft Corporation. All rights reserved.

Build: Topic Version 5.6.9309.1546


To program with the FileSystemObject (FSO) object model:

Use the CreateObject method to create a FileSystemObject object.
Use the appropriate method on the newly created object.
Access the object's properties.
The FSO object model is contained in the Scripting type library, which is located in the Scrrun.dll file. Therefore, you must have Scrrun.dll in the appropriate system directory on your Web server to use the FSO object model.

Creating a FileSystemObject Object
First, create a FileSystemObject object by using the CreateObject method.

The following code displays how to create an instance of the FileSystemObject:

[VBScript]
Dim fso
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
[JScript]
var fso;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
In both of these examples, Scripting is the name of the type library and FileSystemObject is the name of the object that you want to create. You can create only one instance of the FileSystemObject object, regardless of how many times you try to create another.

Using the Appropriate Method
Second, use the appropriate method of the FileSystemObject object. For example, to create a new object, use either CreateTextFile or CreateFolder (the FSO object model doesn't support the creation or deletion of drives).

To delete objects, use the DeleteFile and DeleteFolder methods of the FileSystemObject object, or the Delete method of the File and Folder objects. You can also copy and move files and folders, by using the appropriate methods.

Note Some functionality in the FileSystemObject object model is redundant. For example, you can copy a file using either the CopyFile method of the FileSystemObject object, or you can use the Copy method of the File object. The methods work the same; both exist to offer programming flexibility.
Accessing Existing Drives, Files, and Folders
To gain access to an existing drive, file, or folder, use the appropriate &quot;get&quot; method of the FileSystemObject object:

GetDrive
GetFolder
GetFile
To gain access to an existing file:

[VBScript]
Dim fso, f1
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f1 = fso.GetFile(&quot;c:\test.txt&quot;)
[JScript]
var fso, f1;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
f1 = fso.GetFile(&quot;c:\\test.txt&quot;);
Do not use the &quot;get&quot; methods for newly created objects, since the &quot;create&quot; functions already return a handle to that object. For example, if you create a new folder using the CreateFolder method, don't use the GetFolder method to access its properties, such as Name, Path, Size, and so forth. Just set a variable to the CreateFolder function to gain a handle to the newly created folder, then access its properties, methods, and events.

To set a variable to the CreateFolder function, use this syntax:

[VBScript]
Sub CreateFolder
Dim fso, fldr
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set fldr = fso.CreateFolder(&quot;C:\MyTest&quot;)
Response.Write &quot;Created folder: &quot; & fldr.Name
End Sub
[JScript]
function CreateFolder()
{
var fso, fldr;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
fldr = fso.CreateFolder(&quot;C:\\MyTest&quot;);
Response.Write(&quot;Created folder: &quot; + fldr.Name);
}
Accessing the Object's Properties
Once you have a handle to an object, you can access its properties. For example, to get the name of a particular folder, first create an instance of the object, then get a handle to it with the appropriate method (in this case, the GetFolder method, since the folder already exists).

Use this code to get a handle to the GetFolder method:

[VBScript]
Set fldr = fso.GetFolder(&quot;c:\&quot;)
[JScript]
var fldr = fso.GetFolder(&quot;c:\\&quot;);
Now that you have a handle to a Folder object, you can check its Name property.

[VBScript]
Response.Write &quot;Folder name is: &quot; & fldr.Name
[JScript]
Response.Write(&quot;Folder name is: &quot; + fldr.Name);
To find out the last time a file was modified, use the following syntax:

[VBScript]
Dim fso, f1
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
' Get a File object to query.
Set f1 = fso.GetFile(&quot;c:\detlog.txt&quot;)
' Print information.
Response.Write &quot;File last modified: &quot; & f1.DateLastModified
[JScript]
var fso, f1;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
// Get a File object to query.
f1 = fso.GetFile(&quot;c:\\detlog.txt&quot;);
// Print information.
Response.Write(&quot;File last modified: &quot; + f1.DateLastModified);


--------------------------------------------------------------------------------

© 2001 Microsoft Corporation. All rights reserved.

Build: Topic Version 5.6.9309.1546


With the FileSystemObject (FSO) object model, you can work with drives and folders programmatically just as you can in the Windows Explorer interactively. You can copy and move folders, get information about drives and folders, and so forth.

Getting Information About Drives
The Drive object allows you to gain information about the various drives attached to a system, either physically or over a network. Its properties allow you to obtain information about:

The total size of the drive in bytes (TotalSize property)
How much space is available on the drive in bytes (AvailableSpace or FreeSpace properties)
What letter is assigned to the drive (DriveLetter property)
What type of drive it is, such as removable, fixed, network, CD-ROM, or RAM disk (DriveType property)
The drive's serial number (SerialNumber property)
The type of file system the drive uses, such as FAT, FAT32, NTFS, and so forth (FileSystem property)
Whether a drive is available for use (IsReady property)
The name of the share and/or volume (ShareName and VolumeName properties)
The path or root folder of the drive (Path and RootFolder properties)
View the sample code to see how these properties are used in FileSystemObject.

Example Usage of the Drive Object
Use the Drive object to gather information about a drive. You won't see a reference to an actual Drive object in the following code; instead, use the GetDrive method to get a reference to an existing Drive object (in this case, drv).

The following example demonstrates how to use the Drive object:

[VBScript]
Sub ShowDriveInfo(drvPath)
Dim fso, drv, s
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set drv = fso.GetDrive(fso.GetDriveName(drvPath))
s = &quot;Drive &quot; & UCase(drvPath) & &quot; - &quot;
s = s & drv.VolumeName & &quot;<br>&quot;
s = s & &quot;Total Space: &quot; & FormatNumber(drv.TotalSize / 1024, 0)
s = s & &quot; Kb&quot; & &quot;<br>&quot;
s = s & &quot;Free Space: &quot; & FormatNumber(drv.FreeSpace / 1024, 0)
s = s & &quot; Kb&quot; & &quot;<br>&quot;
Response.Write s
End Sub
[JScript]
function ShowDriveInfo1(drvPath)
{
var fso, drv, s =&quot;&quot;;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
drv = fso.GetDrive(fso.GetDriveName(drvPath));
s += &quot;Drive &quot; + drvPath.toUpperCase()+ &quot; - &quot;;
s += drv.VolumeName + &quot;<br>&quot;;
s += &quot;Total Space: &quot; + drv.TotalSize / 1024;
s += &quot; Kb&quot; + &quot;<br>&quot;;
s += &quot;Free Space: &quot; + drv.FreeSpace / 1024;
s += &quot; Kb&quot; + &quot;<br>&quot;;
Response.Write(s);
}
Working with Folders
Common folder tasks and the methods for performing them are described in the following table.

Task Method
Create a folder. FileSystemObject.CreateFolder
Delete a folder. Folder.Delete or FileSystemObject.DeleteFolder
Move a folder. Folder.Move or FileSystemObject.MoveFolder
Copy a folder. Folder.Copy or FileSystemObject.CopyFolder
Retrieve the name of a folder. Folder.Name
Find out if a folder exists on a drive. FileSystemObject.FolderExists
Get an instance of an existing Folder object. FileSystemObject.GetFolder
Find out the name of a folder's parent folder. FileSystemObject.GetParentFolderName
Find out the path of system folders. FileSystemObject.GetSpecialFolder

View the sample code to see how many of these methods and properties are used in FileSystemObject.

The following example demonstrates how to use the Folder and FileSystemObject objects to manipulate folders and gain information about them.

[VBScript]
Sub ShowFolderInfo()
Dim fso, fldr, s
' Get instance of FileSystemObject.
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
' Get Drive object.
Set fldr = fso.GetFolder(&quot;c:&quot;)
' Print parent folder name.
Response.Write &quot;Parent folder name is: &quot; & fldr & &quot;<br>&quot;
' Print drive name.
Response.Write &quot;Contained on drive &quot; & fldr.Drive & &quot;<br>&quot;
' Print root file name.
If fldr.IsRootFolder = True Then
Response.Write &quot;This is the root folder.&quot; & &quot;&quot;<br>&quot;<br>&quot;
Else
Response.Write &quot;This folder isn't a root folder.&quot; & &quot;<br><br>&quot;
End If
' Create a new folder with the FileSystemObject object.
fso.CreateFolder (&quot;C:\Bogus&quot;)
Response.Write &quot;Created folder C:\Bogus&quot; & &quot;<br>&quot;
' Print the base name of the folder.
Response.Write &quot;Basename = &quot; & fso.GetBaseName(&quot;c:\bogus&quot;) & &quot;<br>&quot;
' Delete the newly created folder.
fso.DeleteFolder (&quot;C:\Bogus&quot;)
Response.Write &quot;Deleted folder C:\Bogus&quot; & &quot;<br>&quot;
End Sub
[JScript]
function ShowFolderInfo()
{
var fso, fldr, s = &quot;&quot;;
// Get instance of FileSystemObject.
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
// Get Drive object.
fldr = fso.GetFolder(&quot;c:&quot;);
// Print parent folder name.
Response.Write(&quot;Parent folder name is: &quot; + fldr + &quot;<br>&quot;);
// Print drive name.
Response.Write(&quot;Contained on drive &quot; + fldr.Drive + &quot;<br>&quot;);
// Print root file name.
if (fldr.IsRootFolder)
Response.Write(&quot;This is the root folder.&quot;);
else
Response.Write(&quot;This folder isn't a root folder.&quot;);
Response.Write(&quot;<br><br>&quot;);
// Create a new folder with the FileSystemObject object.
fso.CreateFolder (&quot;C:\\Bogus&quot;);
Response.Write(&quot;Created folder C:\\Bogus&quot; + &quot;<br>&quot;);
// Print the base name of the folder.
Response.Write(&quot;Basename = &quot; + fso.GetBaseName(&quot;c:\\bogus&quot;) + &quot;<br>&quot;);
// Delete the newly created folder.
fso.DeleteFolder (&quot;C:\\Bogus&quot;);
Response.Write(&quot;Deleted folder C:\\Bogus&quot; + &quot;<br>&quot;);
}


--------------------------------------------------------------------------------

© 2001 Microsoft Corporation. All rights reserved.

Build: Topic Version 5.6.9309.1546


There are two major categories of file manipulation:

Creating, adding, or removing data, and reading files
Moving, copying, and deleting files
Creating Files
There are three ways to create an empty text file (sometimes referred to as a &quot;text stream&quot;).

The first way is to use the CreateTextFile method. The following example demonstrates how to create a text file using the CreateTextFileMethod method.

[VBScript]
Dim fso, f1
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f1 = fso.CreateTextFile(&quot;c:\testfile.txt&quot;, True)
[JScript]
var fso, f1;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
f1 = fso.CreateTextFile(&quot;c:\\testfile.txt&quot;, true);
The second way to create a text file is to use the OpenTextFile method of the FileSystemObject object with the ForWriting flag set.

[VBScript]
Dim fso, ts
Const ForWriting = 2
Set fso = CreateObject(&quot;Scripting. FileSystemObject&quot;)
Set ts = fso_OpenTextFile(&quot;c:\test.txt&quot;, ForWriting, True)
[JScript]
var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
ts = fso_OpenTextFile(&quot;c:\\test.txt&quot;, ForWriting, true);
A third way to create a text file is to use the OpenAsTextStream method with the ForWriting flag set.

[VBScript]
Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
fso.CreateTextFile (&quot;c:\test1.txt&quot;)
Set f1 = fso.GetFile(&quot;c:\test1.txt&quot;)
Set ts = f1.OpenAsTextStream(ForWriting, True)
[JScript]
var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
fso.CreateTextFile (&quot;c:\\test1.txt&quot;);
f1 = fso.GetFile(&quot;c:\\test1.txt&quot;);
ts = f1.OpenAsTextStream(ForWriting, true);
Adding Data to the File
Once the text file is created, add data to the file using the following three steps:

Open the text file.

Write the data.

Close the file.

To open an existing file, use either the OpenTextFile method of the FileSystemObject object or the OpenAsTextStream method of the File object.

To write data to the open text file, use the Write, WriteLine, or WriteBlankLines methods of the TextStream object, according to the tasks outlined in the following table.

Task Method
Write data to an open text file without a trailing newline character. Write
Write data to an open text file with a trailing newline character. WriteLine
Write one or more blank lines to an open text file. WriteBlankLines

To close an open file, use the Close method of the TextStream object.

Note The newline character contains a character or characters (depending on the operating system) to advance the cursor to the beginning of the next line (carriage return/line feed). Be aware that the end of some strings may already have such nonprinting characters.
The following example demonstrates how to open a file, use all three write methods to add data to the file, and then close the file:

[VBScript]
Sub CreateFile()
Dim fso, tf
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set tf = fso.CreateTextFile(&quot;c:\testfile.txt&quot;, True)
' Write a line with a newline character.
tf.WriteLine(&quot;Testing 1, 2, 3.&quot;)
' Write three newline characters to the file.
tf.WriteBlankLines(3)
' Write a line.
tf.Write (&quot;This is a test.&quot;)
tf.Close
End Sub
[JScript]
function CreateFile()
{
var fso, tf;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
tf = fso.CreateTextFile(&quot;c:\\testfile.txt&quot;, true);
// Write a line with a newline character.
tf.WriteLine(&quot;Testing 1, 2, 3.&quot;) ;
// Write three newline characters to the file.
tf.WriteBlankLines(3) ;
// Write a line.
tf.Write (&quot;This is a test.&quot;);
tf.Close();
}
Reading Files
To read data from a text file, use the Read, ReadLine, or ReadAll method of the TextStream object. The following table describes which method to use for various tasks.

Task Method
Read a specified number of characters from a file. Read
Read an entire line (up to, but not including, the newline character). ReadLine
Read the entire contents of a text file. ReadAll

If you use the Read or ReadLine method and want to skip to a particular portion of data, use the Skip or SkipLine method. The resulting text of the read methods is stored in a string which can be displayed in a control, parsed by string functions (such as Left, Right, and Mid), concatenated, and so forth.

The following example demonstrates how to open a file, write to it, and then read from it:

[VBScript]
Sub ReadFiles
Dim fso, f1, ts, s
Const ForReading = 1
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f1 = fso.CreateTextFile(&quot;c:\testfile.txt&quot;, True)
' Write a line.
Response.Write &quot;Writing file <br>&quot;
f1.WriteLine &quot;Hello World&quot;
f1.WriteBlankLines(1)
f1.Close
' Read the contents of the file.
Response.Write &quot;Reading file <br>&quot;
Set ts = fso_OpenTextFile(&quot;c:\testfile.txt&quot;, ForReading)
s = ts.ReadLine
Response.Write &quot;File contents = '&quot; & s & &quot;'&quot;
ts.Close
End Sub
[JScript]
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
f1 = fso.CreateTextFile(&quot;c:\\testfile.txt&quot;, true);
// Write a line.
Response.Write(&quot;Writing file <br>&quot;);
f1.WriteLine(&quot;Hello World&quot;);
f1.WriteBlankLines(1);
f1.Close();
// Read the contents of the file.
Response.Write(&quot;Reading file <br>&quot;);
ts = fso_OpenTextFile(&quot;c:\\testfile.txt&quot;, ForReading);
s = ts.ReadLine();
Response.Write(&quot;File contents = '&quot; + s + &quot;'&quot;);
ts.Close();
}
Moving, Copying, and Deleting Files
The FSO object model has two methods each for moving, copying, and deleting files, as described in the following table.

Task Method
Move a file File.Move or FileSystemObject.MoveFile
Copy a file File.Copy or FileSystemObject.CopyFile
Delete a file File.Delete or FileSystemObject.DeleteFile

The following example creates a text file in the root directory of drive C, writes some information to it, moves it to a directory called \tmp, makes a copy of it in a directory called \temp, then deletes the copies from both directories.

To run the following example, create directories named \tmp and \temp in the root directory of drive C:

[VBScript]
Sub ManipFiles
Dim fso, f1, f2, s
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f1 = fso.CreateTextFile(&quot;c:\testfile.txt&quot;, True)
Response.Write &quot;Writing file <br>&quot;
' Write a line.
f1.Write (&quot;This is a test.&quot;)
' Close the file to writing.
f1.Close
Response.Write &quot;Moving file to c:\tmp <br>&quot;
' Get a handle to the file in root of C:\.
Set f2 = fso.GetFile(&quot;c:\testfile.txt&quot;)
' Move the file to \tmp directory.
f2.Move (&quot;c:\tmp\testfile.txt&quot;)
Response.Write &quot;Copying file to c:\temp <br>&quot;
' Copy the file to \temp.
f2.Copy (&quot;c:\temp\testfile.txt&quot;)
Response.Write &quot;Deleting files <br>&quot;
' Get handles to files' current location.
Set f2 = fso.GetFile(&quot;c:\tmp\testfile.txt&quot;)
Set f3 = fso.GetFile(&quot;c:\temp\testfile.txt&quot;)
' Delete the files.
f2.Delete
f3.Delete
Response.Write &quot;All done!&quot;
End Sub
[JScript]
function ManipFiles()
{
var fso, f1, f2, s;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
f1 = fso.CreateTextFile(&quot;c:\\testfile.txt&quot;, true);
Response.Write(&quot;Writing file <br>&quot;);
// Write a line.
f1.Write(&quot;This is a test.&quot;);
// Close the file to writing.
f1.Close();
Response.Write(&quot;Moving file to c:\\tmp <br>&quot;);
// Get a handle to the file in root of C:\.
f2 = fso.GetFile(&quot;c:\\testfile.txt&quot;);
// Move the file to \tmp directory.
f2.Move (&quot;c:\\tmp\\testfile.txt&quot;);
Response.Write(&quot;Copying file to c:\\temp <br>&quot;);
// Copy the file to \temp.
f2.Copy (&quot;c:\\temp\\testfile.txt&quot;);
Response.Write(&quot;Deleting files <br>&quot;);
// Get handles to files' current location.
f2 = fso.GetFile(&quot;c:\\tmp\\testfile.txt&quot;);
f3 = fso.GetFile(&quot;c:\\temp\\testfile.txt&quot;);
// Delete the files.
f2.Delete();
f3.Delete();
Response.Write(&quot;All done!&quot;);
}


--------------------------------------------------------------------------------

© 2001 Microsoft Corporation. All rights reserved.

Build: Topic Version 5.6.9309.1546


The sample code described in this section provides a real-world example that demonstrates many of the features available in the FileSystemObject object model. This code shows how all the features of the object model work together, and how to use those features effectively in your own code.

Notice that since this code is fairly generic, some additional code and a little tweaking are needed to make this code actually run on your machine. These changes are necessary because of the different ways input and output to the user is handled between Active Server Pages and the Windows Scripting Host.

To run this code on an Active Server Page, use the following steps:

Create a standard web page with an .asp extension.

Copy the following sample code into that file between the <BODY>...</BODY> tags.

Enclose all the code within <%...%> tags.

Move the Option Explicit statement from its current position in the code to the very top of your HTML page, positioning it even before the opening <HTML> tag.

Place <%...%> tags around the Option Explicit statement to ensure that it's run on the server side.

Add the following code to the end of the sample code:

Sub Print(x)
Response.Write &quot;<PRE>&ltFONT FACE=&quot;&quot;Courier New&quot;&quot; SIZE=&quot;&quot;1&quot;&quot;>&quot;
Response.Write x
Response.Write &quot;</FONT></PRE>&quot;
End Sub
Main
The previous code adds a print procedure that will run on the server side, but display results on the client side. To run this code on the Windows Scripting Host, add the following code to the end of the sample code:

Sub Print(x)
WScript.Echo x
End Sub
Main
The code is contained in the following section:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FileSystemObject Sample Code
' Copyright 1998 Microsoft Corporation. All Rights Reserved.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Regarding code quality:
' 1) The following code does a lot of string manipulation by
' concatenating short strings together with the &quot;&&quot; operator.
' Since string concatenation is expensive, this is a very
' inefficient way to write code. However, it is a very
' maintainable way to write code, and is used here because this
' program performs extensive disk operations, and because the
' disk is much slower than the memory operations required to
' concatenate the strings. Keep in mind that this is demonstration
' code, not production code.
'
' 2) &quot;Option Explicit&quot; is used, because declared variable access is
' slightly faster than undeclared variable access. It also prevents
' bugs from creeping into your code, such as when you misspell
' DriveTypeCDROM as DriveTypeCDORM.
'
' 3) Error handling is absent from this code, to make the code more
' readable. Although precautions have been taken to ensure that the
' code will not error in common cases, file systems can be
' unpredictable. In production code, use On Error Resume Next and
' the Err object to trap possible errors.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Some handy global variables
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim TabStop
Dim NewLine

Const TestDrive = &quot;C&quot;
Const TestFilePath = &quot;C:\Test&quot;

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Constants returned by Drive.DriveType
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const DriveTypeRemovable = 1
Const DriveTypeFixed = 2
Const DriveTypeNetwork = 3
Const DriveTypeCDROM = 4
Const DriveTypeRAMDisk = 5

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Constants returned by File.Attributes
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FileAttrNormal = 0
Const FileAttrReadOnly = 1
Const FileAttrHidden = 2
Const FileAttrSystem = 4
Const FileAttrVolume = 8
Const FileAttrDirectory = 16
Const FileAttrArchive = 32
Const FileAttrAlias = 64
Const FileAttrCompressed = 128

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Constants for opening files
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const OpenFileForReading = 1
Const OpenFileForWriting = 2
Const OpenFileForAppending = 8

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShowDriveType
' Purpose:
' Generates a string describing the drive type of a given Drive object.
' Demonstrates the following
' - Drive.DriveType
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ShowDriveType(Drive)

Dim S

Select Case Drive.DriveType
Case DriveTypeRemovable
S = &quot;Removable&quot;
Case DriveTypeFixed
S = &quot;Fixed&quot;
Case DriveTypeNetwork
S = &quot;Network&quot;
Case DriveTypeCDROM
S = &quot;CD-ROM&quot;
Case DriveTypeRAMDisk
S = &quot;RAM Disk&quot;
Case Else
S = &quot;Unknown&quot;
End Select

ShowDriveType = S

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShowFileAttr
' Purpose:
' Generates a string describing the attributes of a file or folder.
' Demonstrates the following
' - File.Attributes
' - Folder.Attributes
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function ShowFileAttr(File) ' File can be a file or folder

Dim S
Dim Attr

Attr = File.Attributes

If Attr = 0 Then
ShowFileAttr = &quot;Normal&quot;
Exit Function
End If

If Attr And FileAttrDirectory Then S = S & &quot;Directory &quot;
If Attr And FileAttrReadOnly Then S = S & &quot;Read-Only &quot;
If Attr And FileAttrHidden Then S = S & &quot;Hidden &quot;
If Attr And FileAttrSystem Then S = S & &quot;System &quot;
If Attr And FileAttrVolume Then S = S & &quot;Volume &quot;
If Attr And FileAttrArchive Then S = S & &quot;Archive &quot;
If Attr And FileAttrAlias Then S = S & &quot;Alias &quot;
If Attr And FileAttrCompressed Then S = S & &quot;Compressed &quot;

ShowFileAttr = S

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GenerateDriveInformation
' Purpose:
' Generates a string describing the current state of the
' available drives.
' Demonstrates the following
' - FileSystemObject.Drives
' - Iterating the Drives collection
' - Drives.Count
' - Drive.AvailableSpace
' - Drive.DriveLetter
' - Drive.DriveType
' - Drive.FileSystem
' - Drive.FreeSpace
' - Drive.IsReady
' - Drive.Path
' - Drive.SerialNumber
' - Drive.ShareName
' - Drive.TotalSize
' - Drive.VolumeName
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GenerateDriveInformation(FSO)

Dim Drives
Dim Drive
Dim S

Set Drives = FSO.Drives
S = &quot;Number of drives:&quot; & TabStop & Drives.Count & NewLine & NewLine

' Construct 1st line of report.
S = S & String(2, TabStop) & &quot;Drive&quot;
S = S & String(3, TabStop) & &quot;File&quot;
S = S & TabStop & &quot;Total&quot;
S = S & TabStop & &quot;Free&quot;
S = S & TabStop & &quot;Available&quot;
S = S & TabStop & &quot;Serial&quot; & NewLine

' Construct 2nd line of report.
S = S & &quot;Letter&quot;
S = S & TabStop & &quot;Path&quot;
S = S & TabStop & &quot;Type&quot;
S = S & TabStop & &quot;Ready?&quot;
S = S & TabStop & &quot;Name&quot;
S = S & TabStop & &quot;System&quot;
S = S & TabStop & &quot;Space&quot;
S = S & TabStop & &quot;Space&quot;
S = S & TabStop & &quot;Space&quot;
S = S & TabStop & &quot;Number&quot; & NewLine

' Separator line.
S = S & String(105, &quot;-&quot;) & NewLine

For Each Drive In Drives
S = S & Drive.DriveLetter
S = S & TabStop & Drive.Path
S = S & TabStop & ShowDriveType(Drive)
S = S & TabStop & Drive.IsReady

If Drive.IsReady Then
If DriveTypeNetwork = Drive.DriveType Then
S = S & TabStop & Drive.ShareName
Else
S = S & TabStop & Drive.VolumeName
End If
S = S & TabStop & Drive.FileSystem
S = S & TabStop & Drive.TotalSize
S = S & TabStop & Drive.FreeSpace
S = S & TabStop & Drive.AvailableSpace
S = S & TabStop & Hex(Drive.SerialNumber)
End If

S = S & NewLine

Next

GenerateDriveInformation = S

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GenerateFileInformation
' Purpose:
' Generates a string describing the current state of a file.
' Demonstrates the following
' - File.Path
' - File.Name
' - File.Type
' - File.DateCreated
' - File.DateLastAccessed
' - File.DateLastModified
' - File.Size
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GenerateFileInformation(File)

Dim S

S = NewLine & &quot;Path:&quot; & TabStop & File.Path
S = S & NewLine & &quot;Name:&quot; & TabStop & File.Name
S = S & NewLine & &quot;Type:&quot; & TabStop & File.Type
S = S & NewLine & &quot;Attribs:&quot; & TabStop & ShowFileAttr(File)
S = S & NewLine & &quot;Created:&quot; & TabStop & File.DateCreated
S = S & NewLine & &quot;Accessed:&quot; & TabStop & File.DateLastAccessed
S = S & NewLine & &quot;Modified:&quot; & TabStop & File.DateLastModified
S = S & NewLine & &quot;Size&quot; & TabStop & File.Size & NewLine

GenerateFileInformation = S

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GenerateFolderInformation
' Purpose:
' Generates a string describing the current state of a folder.
' Demonstrates the following
' - Folder.Path
' - Folder.Name
' - Folder.DateCreated
' - Folder.DateLastAccessed
' - Folder.DateLastModified
' - Folder.Size
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GenerateFolderInformation(Folder)

Dim S

S = &quot;Path:&quot; & TabStop & Folder.Path
S = S & NewLine & &quot;Name:&quot; & TabStop & Folder.Name
S = S & NewLine & &quot;Attribs:&quot; & TabStop & ShowFileAttr(Folder)
S = S & NewLine & &quot;Created:&quot; & TabStop & Folder.DateCreated
S = S & NewLine & &quot;Accessed:&quot; & TabStop & Folder.DateLastAccessed
S = S & NewLine & &quot;Modified:&quot; & TabStop & Folder.DateLastModified
S = S & NewLine & &quot;Size:&quot; & TabStop & Folder.Size & NewLine

GenerateFolderInformation = S

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GenerateAllFolderInformation
' Purpose:
' Generates a string describing the current state of a
' folder and all files and subfolders.
' Demonstrates the following
' - Folder.Path
' - Folder.SubFolders
' - Folders.Count
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GenerateAllFolderInformation(Folder)

Dim S
Dim SubFolders
Dim SubFolder
Dim Files
Dim File

S = &quot;Folder:&quot; & TabStop & Folder.Path & NewLine & NewLine
Set Files = Folder.Files

If 1 = Files.Count Then
S = S & &quot;There is 1 file&quot; & NewLine
Else
S = S & &quot;There are &quot; & Files.Count & &quot; files&quot; & NewLine
End If

If Files.Count <> 0 Then
For Each File In Files
S = S & GenerateFileInformation(File)
Next
End If

Set SubFolders = Folder.SubFolders

If 1 = SubFolders.Count Then
S = S & NewLine & &quot;There is 1 sub folder&quot; & NewLine & NewLine
Else
S = S & NewLine & &quot;There are &quot; & SubFolders.Count & &quot; sub folders&quot; _
NewLine & NewLine
End If

If SubFolders.Count <> 0 Then
For Each SubFolder In SubFolders
S = S & GenerateFolderInformation(SubFolder)
Next
S = S & NewLine
For Each SubFolder In SubFolders
S = S & GenerateAllFolderInformation(SubFolder)
Next
End If

GenerateAllFolderInformation = S

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GenerateTestInformation
' Purpose:
' Generates a string describing the current state of the C:\Test
' folder and all files and subfolders.
' Demonstrates the following
' - FileSystemObject.DriveExists
' - FileSystemObject.FolderExists
' - FileSystemObject.GetFolder
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GenerateTestInformation(FSO)

Dim TestFolder
Dim S

If Not FSO.DriveExists(TestDrive) Then Exit Function
If Not FSO.FolderExists(TestFilePath) Then Exit Function

Set TestFolder = FSO.GetFolder(TestFilePath)

GenerateTestInformation = GenerateAllFolderInformation(TestFolder)

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DeleteTestDirectory
' Purpose:
' Cleans up the test directory.
' Demonstrates the following
' - FileSystemObject.GetFolder
' - FileSystemObject.DeleteFile
' - FileSystemObject.DeleteFolder
' - Folder.Delete
' - File.Delete
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub DeleteTestDirectory(FSO)

Dim TestFolder
Dim SubFolder
Dim File

' Two ways to delete a file:

FSO.DeleteFile(TestFilePath & &quot;\Beatles\OctopusGarden.txt&quot;)

Set File = FSO.GetFile(TestFilePath & &quot;\Beatles\BathroomWindow.txt&quot;)
File.Delete

' Two ways to delete a folder:
FSO.DeleteFolder(TestFilePath & &quot;\Beatles&quot;)
FSO.DeleteFile(TestFilePath & &quot;\ReadMe.txt&quot;)
Set TestFolder = FSO.GetFolder(TestFilePath)
TestFolder.Delete

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CreateLyrics
' Purpose:
' Builds a couple of text files in a folder.
' Demonstrates the following
' - FileSystemObject.CreateTextFile
' - TextStream.WriteLine
' - TextStream.Write
' - TextStream.WriteBlankLines
' - TextStream.Close
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub CreateLyrics(Folder)

Dim TextStream

Set TextStream = Folder.CreateTextFile(&quot;OctopusGarden.txt&quot;)

' Note that this does not add a line feed to the file.
TextStream.Write(&quot;Octopus' Garden &quot;)
TextStream.WriteLine(&quot;(by Ringo Starr)&quot;)
TextStream.WriteBlankLines(1)
TextStream.WriteLine(&quot;I'd like to be under the sea in an octopus' garden in the shade,&quot;)
TextStream.WriteLine(&quot;He'd let us in, knows where we've been -- in his octopus' garden in the shade.&quot;)
TextStream.WriteBlankLines(2)

TextStream.Close

Set TextStream = Folder.CreateTextFile(&quot;BathroomWindow.txt&quot;)
TextStream.WriteLine(&quot;She Came In Through The Bathroom Window (by Lennon/McCartney)&quot;)
TextStream.WriteLine(&quot;&quot;)
TextStream.WriteLine(&quot;She came in through the bathroom window protected by a silver spoon&quot;)
TextStream.WriteLine(&quot;But now she sucks her thumb and wanders by the banks of her own lagoon&quot;)
TextStream.WriteBlankLines(2)
TextStream.Close

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetLyrics
' Purpose:
' Displays the contents of the lyrics files.
' Demonstrates the following
' - FileSystemObject.OpenTextFile
' - FileSystemObject.GetFile
' - TextStream.ReadAll
' - TextStream.Close
' - File.OpenAsTextStream
' - TextStream.AtEndOfStream
' - TextStream.ReadLine
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GetLyrics(FSO)

Dim TextStream
Dim S
Dim File

' There are several ways to open a text file, and several
' ways to read the data out of a file. Here's two ways
' to do each:

Set TextStream = FSO.OpenTextFile(TestFilePath & &quot;\Beatles\OctopusGarden.txt&quot;, OpenFileForReading)

S = TextStream.ReadAll & NewLine & NewLine
TextStream.Close

Set File = FSO.GetFile(TestFilePath & &quot;\Beatles\BathroomWindow.txt&quot;)
Set TextStream = File.OpenAsTextStream(OpenFileForReading)
Do While Not TextStream.AtEndOfStream
S = S & TextStream.ReadLine & NewLine
Loop
TextStream.Close

GetLyrics = S

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' BuildTestDirectory
' Purpose:
' Builds a directory hierarchy to demonstrate the FileSystemObject.
' We'll build a hierarchy in this order:
' C:\Test
' C:\Test\ReadMe.txt
' C:\Test\Beatles
' C:\Test\Beatles\OctopusGarden.txt
' C:\Test\Beatles\BathroomWindow.txt
' Demonstrates the following
' - FileSystemObject.DriveExists
' - FileSystemObject.FolderExists
' - FileSystemObject.CreateFolder
' - FileSystemObject.CreateTextFile
' - Folders.Add
' - Folder.CreateTextFile
' - TextStream.WriteLine
' - TextStream.Close
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function BuildTestDirectory(FSO)

Dim TestFolder
Dim SubFolders
Dim SubFolder
Dim TextStream

' Bail out if (a) the drive does not exist, or if (b) the directory is being built
' already exists.

If Not FSO.DriveExists(TestDrive) Then
BuildTestDirectory = False
Exit Function
End If

If FSO.FolderExists(TestFilePath) Then
BuildTestDirectory = False
Exit Function
End If

Set TestFolder = FSO.CreateFolder(TestFilePath)

Set TextStream = FSO.CreateTextFile(TestFilePath & &quot;\ReadMe.txt&quot;)
TextStream.WriteLine(&quot;My song lyrics collection&quot;)
TextStream.Close

Set SubFolders = TestFolder.SubFolders
Set SubFolder = SubFolders.Add(&quot;Beatles&quot;)
CreateLyrics SubFolder
BuildTestDirectory = True

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The main routine
' First, it creates a test directory, along with some subfolders
' and files. Then, it dumps some information about the available
' disk drives and about the test directory, and then cleans
' everything up again.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub Main

Dim FSO

' Set up global data.
TabStop = Chr(9)
NewLine = Chr(10)

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

If Not BuildTestDirectory(FSO) Then
Print &quot;Test directory already exists or cannot be created. Cannot continue.&quot;
Exit Sub
End If

Print GenerateDriveInformation(FSO) & NewLine & NewLine
Print GenerateTestInformation(FSO) & NewLine & NewLine
Print GetLyrics(FSO) & NewLine & NewLine
DeleteTestDirectory(FSO)

End Sub


--------------------------------------------------------------------------------

© 2001 Microsoft Corporation. All rights reserved.

Build: Topic Version 5.6.9309.1546
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top