ファイルアクセスコンポーネント  

ネイティブ ASP オブジェクト (Request、Response など) および各種のインストール可能なコンポーネント (Ad Rotator、Browser Capabilities など) に加え、第 3 のオブジェクトのグループにもアクセスできます。これらのオブジェクトは、Microsoft Scripting Runtime DLL (scrrun.dll ) から直接インスタンス化されます。この DLL には、ネイティブ ASP オブジェクトにも VBScript ランタイム (vbscript.dll ) 自身にもない機能が用意されています。スクリプティング DLL から、アプリケーションに広範囲のファイル操作機能を提供するオブジェクトをインスタンス化できます。この DLL からは、true のコレクションなしで類似のコレクション機能を実行できる Dictionary オブジェクトを作成することもできます。

すべてのファイル操作は、 FileSystemObject オブジェクトによって実行されます。アプリケーションにはこの中の 1 つのオブジェクトしかなく、そのオブジェクトがシステムのファイル構造に対するアプリケーションの "ウィンドウ" となります。このオブジェクトを使用すると、ファイルを開く、ファイルを閉じるなどの簡単な機能を実行できます。ただし、このオブジェクトの本当の強みは、Drive、Folder、および File といった他のファイル操作オブジェクトをインスタンス化できることです。アプリケーションでは、これらのオブジェクトにより、コマンド行インターフェイスを使用して得られるファイルシステムに対する能力をほとんど確保できます。

アクセサリファイルおよび必要な DLL ファイル  
 
 
Scrrun.DLL

すべてのスクリプティングオブジェクトを保持する動的リンクライブラリです。この DLL は、Web サーバーに IIS 4.0 をインストールすると、デフォルトでインストールされます。

インストール可能なコンポーネントのインスタンス化  
 
 

FileSystemObject コンポーネントのインスタンスを保持するオブジェクト変数を作成するには、Server オブジェクトの CreateObject メソッドを使用します。CreateObject メソッドの構文を次に示します。

Set objMyObject = Server.CreateObject(strProgId)

ここで、各値の意味は次のとおりです。

    objMyObject は FileSystemObject 変数の名前を表します。

    strProgId は、FileSystemObject コンポーネント、つまり、Scripting.FileSystemObject. のプログラム識別子 (ProgID) を表します。

<% 

' The following code uses the CreateObject method of 
' the Server object to instantiate a FileSystemObject.
Dim fsFileSystemObject
Set fsFileSystemObject = _
   Server.CreateObject("Scripting.FileSystemObject")

%>

CreateObject メソッドの使用方法の詳細については、第 9 章の該当する項を参照してください。

コメントおよびトラブルシューティング  
 
 

scrrun.dll のファイルアクセスコンポーネントを使用するのは簡単です。エラーが発生すると、さまざまなプロパティとメソッドすべてが、エラーメッセージを返します。このメッセージは、コマンド行を介して所定のファイル操作を実行した場合に想定されるメッセージに合致しています。たとえば、コンピュータのフロッピーディスクドライブ上のファイルを読み書きしようとして、ドライブにディスクがない場合、"disk not ready" エラーが表示されます。

AtEndOfLine (TextStream オブジェクト)  
tsObj.AtEndOfLine
 

ファイルポインタが現在の行の末尾にあるかどうかを示すブール値です。これは読み取り専用のプロパティです。

 
パラメータ

なし

 

次のコードは、FileSystemObject と TextStream オブジェクトをインスタンス化します。その後、Read メソッドを使用して、行の末尾に到達するまで一度に 1 文字ずつ読み取ります。AtEndOfStream プロパティと AtEndOfLine プロパティの使用方法が同じことが分ります。

<%

' Set up constants.
Const constForReading       = 1 
Const constTristateFalse    = 0

' Dimension local variables. 
Dim fsoObject     ' FileSystemObject
Dim tsObject      ' TextStream Object
Dim strReturned   ' String variable to hold file contents

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the OpenTextFile method of fsoObject,
' create a text file.
Set tsObject = _
    fsoObject.OpenTextFile("d:\docs\test.txt", _
    constForReading, constTristateFalse)

' Read one character at a time until the end of the 
' line has been reached.
Do While Not tsObject.AtEndOfLine
   StrReturned = strReturned & tsObject.Read(1)
Loop
. . . [additional code]

%>
 
メモ

読み取り以外の目的で開いたテキストファイルで AtEndOfLine プロパティを使用しようとすると、エラーが発生します。

AtEndOfLine プロパティは、ファイルの最後に到達したことを通知しません。

 
AtEndOfStream (TextStream オブジェクト)  
tsObj.AtEndOfStream
 

現在のテキストファイルの最後に到達したかどうかを示すブール値です。これは読み取り専用のプロパティです。

 
パラメータ

なし

 

次のコードは、FileSystemObject と TextStream オブジェクトをインスタンス化します。その後、Read メソッドを使用して、ファイルの最後に到達するまで一度に 1 文字ずつ読み取ります。AtEndOfStream プロパティと AtEndOfLine プロパティの使用方法が同じことが分ります。

<%

' Set up constants.
Const constForReading      = 1 
Const constTristateFalse    = 0

' Dimension local variables. 
Dim fsoObject     ' FileSystemObject
Dim tsObject      ' TextStream Object
Dim strReturned   ' String variable to hold file contents.

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the OpenTextFile method of fsoObject, create 
' a text file.
Set tsObject = _
    fsoObject.OpenTextFile("d:\docs\test.txt", _
    constForReading, constTristateFalse)

' Read one character at a time until the end of the 
' file has been reached
Do While Not tsObject.AtEndOfStream
   StrReturned = strReturned & tsObject.Read(1)
Loop
. . . [additional code]

%>
 
メモ

読み取り以外の目的で開いたテキストファイルで AtEndOfStream プロパティを使用しようとすると、エラーが発生します。

 
Attributes (File オブジェクト、Folder オブジェクト)  
Normal
 

さまざまなファイルシステムの属性を表す値の組み合わせを保持する整数です。このプロパティは、該当するファイル属性によって読み取り専用、または読み書き可能になります。

次の表に、Attributes プロパティが保持できる値を示します。File オブジェクトまたは Folder オブジェクトに特定の値が保持されているかどうかを判断するには、ビットごとの And 演算子を使用して、Attributes プロパティの値と関心のある特定の定数を比較します。結果が True の場合、該当する属性は True となります。次の例を参照してください。

Attributes 定数

説明

Normal

0

属性は設定されません。

ReadOnly

1

読み取り専用。この属性は読み書き可能です。

Hidden

2

非表示。この属性は読み書き可能です。

System

4

システムファイル。この属性は File オブジェクトに対してのみ有効で、読み書き可能です。

Volume

8

ドライブのボリュームラベル。この属性は読み取り専用です。

Directory

16

ディレクトリ。この属性は読み取り専用です。

Archive

32

アーカイブ済み。この属性は読み書き可能です。

Alias

64

別のファイルのリンクまたはショートカット。この属性は File オブジェクトに対してのみ有効で、読み取り専用です。

Compressed

128

圧縮済み。この属性は File オブジェクトに対してのみ有効で、読み取り専用です。

パラメータ
intNewAttributes

ファイルまたはフォルダの属性の合計を保持する整数です。たとえば、Archived 属性と Hidden 属性を True に設定する場合、intNewAttributes の値は、Hidden + Archive、つまり 34 (2 + 32) になります。Attributes プロパティに割り当てると、この整数は 2 つの属性を True に設定します。

 

次のコードは、最初に File オブジェクトで、次に Folder オブジェクトで Attributes プロパティを使用します。

<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim filObject   ' File Object
Dim fdrObject   ' Folder Object

' Declare constants.
Const Hidden = 2
Const Archive = 32

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize the 
' File object.
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Set the Hidden (value = 2) and Archive (value = 32) 
' attributes for the Test.TXT file.
filObject.Attributes = (Hidden + Archive)

' Using the GetFolder method of fsoObject, initialize 
' the Folder object.
Set fdrObject = fsoObject.GetFolder("d:\docs")

' Determine whether the folder is itself hidden.
If (fdrObject.Attributes And Archive) Then
   ' Folder is hidden.
Else
   ' Folder is NOT hidden.
End If
. . . [additional code]

%>
 
メモ

File オブジェクトのみを扱う読み取り専用属性を Folder オブジェクトで使用しようとすると、結果は常に False 値となります。ただし、File オブジェクトまたは Folder オブジェクト用のいずれかの読み取り専用属性を設定しようとすると、結果はエラーになります。

ファイルアクセスコンポーネントで使用する定数は、明示的に宣言する必要があります。

 
AvailableSpace (Drive オブジェクト)  
drvObj. AvailableSpace
 

現在のドライブに残っている領域のバイト数です。使用可能な領域が 2GB を超えるドライブの場合、この数字は不正確です。これは読み取り専用のプロパティです。

 
パラメータ

なし

 
<%
' Dimension local variables. 
Dim fsoObject       ' FileSystemObject
Dim drvObject       ' Drive Object
Dim lngAvailBytes   ' Number of bytes available

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetDrive method of fsoObject, initialize a 
' Drive object.
Set drvObject = fsoObject.GetDrive("\\PublicDocs")
' Retrieve the amount of space (in bytes) available 
' on the drive.
lngAvailBytes = drvObject.AvailableSpace
. . . [additional code]

%>
 
メモ

AvailableSpace プロパティの値と FreeSpace プロパティの値が異なるのは、ドライブがクオータをサポートしている場合だけです。実際上、この 2 つのプロパティを互換的に使用できます。

 
DateCreated (File オブジェクト、Folder オブジェクト)  
Obj.DateCreated
 

ファイルまたはフォルダが作成された日付を表す日付値です。これはオペレーティングシステムで制御される読み取り専用値です。

 
パラメータ

なし

 
<%

' Dimension local variables. 
Dim fsoObject    ' FileSystemObject.
Dim fdrObject    ' Folder object.
Dim datCreated   ' Date variable.

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFolder method of fsoObject, initialize 
' a Folder object
Set fdrObject = fsoObject.GetFolder("c:\Docs")
' Retrieve the date the folder was created.
datCreated = fdrObject.DateCreated
. . . [additional code]

%>
 
メモ

このプロパティの値は、ファイルが現在のドライブに書き込まれた日付でなく、ファイルが作成された日付を示します。

 
Drive (File オブジェクト、Folder オブジェクト)  
Obj.Drive
 

File オブジェクトまたは Folder オブジェクトが関連付けられている Drive を返します。このプロパティは読み取り専用です。

 
パラメータ

なし

 
<%

' Dimension local variables. 
Dim fsoObject     ' FileSystemObject
Dim filObject     ' File Object
Dim objDrive      ' Drive name

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize 
' a File object.
Set filObject = fsoObject.GetFile("PublicDocs.txt")
' Retrieve the drive name with which the File object 
' is associated.
Set objDrive = filObject.Drive
' Note that this drive is actually the current drive 
' in this case.
. . . [additional code]

%>
 
メモ

Drive プロパティは、物理ドライブ、ローカルドライブ、マップドライブ、またはネットワーク共有を表すことができます。

Drive オブジェクトのデフォルトのプロパティは Path であるため、次のようにドライブ名を文字列に割り当てることができます。

strDrive = filObject.Drive

これは、実際には次の簡略化バージョンです。

strDrive = filObject.Drive.Path

ただし、Drive オブジェクトを操作する場合、Set ステートメントを使用して、オブジェクト変数への参照を割り当てる必要があります。次に例を示します。

Set objDrive = filObject.Drive
 
FileSystem (Drive オブジェクト)  
drvObj.FileSystem
 

現在のドライブをフォーマットするファイルシステムタイプを表す文字列です。認識できるファイルシステムタイプは、CDFS、NTFS、FAT、および FAT32 です。これは読み取り専用のプロパティです。

 
パラメータ

なし

 
<%

' Dimension local variables. 
Dim fsoObject      ' FileSystemObject
Dim drvObject      ' Drive Object
Dim strFileSys     ' File system of drive

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetDrive method of fsoObject, initialize 
' a Drive object.
Set drvObject = fsoObject.GetDrive("\\PublicDocs")
' Retrieve the file system for the drive. This value 
' will contain one of the following strings: 
' NTFS, FAT, or CDFS. 
strFileSys = drvObject.FileSystem
. . . [additional code]

%>
 
メモ

Drive オブジェクトの FileSystem プロパティの値を使用すると、現在のドライブで利用できるクラスターサイズとセキュリティ機能を反映させることができます。

 
IsReady (Drive オブジェクト)  
drvObj.IsReady
 

現在のドライブが読み取り可能または書き込み可能かを表すブール値です。このプロパティを使用すると、たとえば、ドライブにフロッピーディスクまたは CD が挿入されているかどうかを判断できます。これは読み取り専用のプロパティです。

 
パラメータ

なし

 
<%

' Dimension local variables. 
Dim fsoObject      ' FileSystemObject
Dim drvObject      ' Drive Object

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject(
                "Scripting.FileSystemObject")
' Using the GetDrive method of fsoObject, initialize a 
' Drive object.
Set drvObject = fsoObject.GetDrive("\\PublicDocs")
' Check to see if the drive is ready.
If drvObject.IsReady Then
   ' Drive is ready for read/write.
Else
   ' Drive is not ready.
End If
. . . [additional code]

%>
 
メモ

何らかのドライブアクセスを試行する前に、IsReady プロパティを使用することをお勧めします。このプロパティは、リムーバブルメディアドライブ (フロッピードライブと CD-ROM ドライブ) および固定メディアドライブの準備できているかどうかを判別できます。

 
IsRootFolder (Folder オブジェクト)  
fdr.IsRootFolder
 

現在のフォルダがルートフォルダかどうかを判断できるブール値です。これは読み取り専用のプロパティです。

 
パラメータ

なし

 
<%

' Dimension local variables. 
Dim fsoObject      ' FileSystemObject
Dim fdrObject      ' Folder Object

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFolder method of fsoObject, initialize a 
' File object.
Set fdrObject = fsoObject.GetFolder("PublicDocs.txt")
' Determine whether the current folder is a root folder
' or if it is nested.
If fdrObject.IsRootFolder Then
   ' Folder is located directly off the drive letter 
   ' or share name.
Else
   ' The folder is nested within at least one other 
   ' folder.
End If
. . . [additional code]

%>
 
メモ

Microsoft のマニュアルには、このプロパティを使用して現在のフォルダがネストされているレベル数を判別する方法について記載されています。便宜的に、この方法を次のコードで示します。

<%

' Dimension local variables. 
Dim fsoObject      ' FileSystemObject
Dim fdrObject      ' Folder Object
Dim intNestedLevel ' Level to which the folder is nested

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFolder method of fsoObject, initialize a 
' File object.
Set fdrObject = fsoObject.GetFolder("PublicDocs.txt")
' Determine whether the current folder is a root folder
' or if it is nested.
If fdrObject.IsRootFolder Then
   ' Folder is located directly off the drive letter or 
   ' share name.
Else
   ' For more on the ParentFolder property of the 
   ' Folder object, see the following.
   Do Until fdrObject.IsRootFolder
      Set fdrObject = fdrObject.ParentFolder
      intNestedLevel = intNestedLevel + 1
   Loop
End If
. . . [additional code]

%>
 
ParentFolder (File オブジェクト、Folder オブジェクト)  
Obj.ParentFolder
 

ファイルまたはフォルダが置かれているフォルダを表す Folder オブジェクトを返します。これは読み取り専用のプロパティです。

 
パラメータ

なし

 

次のコードは、最初に File オブジェクトで、次に Folder オブジェクトで使用する際の ParentFolder プロパティの使用方法を示します。Name は Folder オブジェクトのデフォルトのプロパティであるため、ASP ページ内のコードは、ParentFolder プロパティが返す値を文字列として処理することになります。

<%

' Dimension local variables. 
Dim fsoObject        ' FileSystemObject
Dim filObject        ' File Object
Dim fdrObject        ' Folder Object
Dim strFileParent    ' Parent folder of file object
Dim strFolderParent  ' Parent folder of folder object

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize the 
' File object.
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Retrieve the name of the folder containing the file Test.TXT.
' In this example, the value of strFileParent is "docs".
strFileParent = filObject.ParentFolder
' Using the GetFolder method of fsoObject, initialize 
' the Folder object.
Set fdrObject = fsoObject.GetFolder("d:\mystuff\docs")

' Retrieve the name of the folder that contains the 
' folder "docs". In this example, the value of 
' strFileParent is "mystuff".
strFolderParent = fdrObject.ParentFolder
. . . [additional code]

%>
 
Close (TextStream オブジェクト)  
tsObj.Close
 

TextStream オブジェクトとして開かれたテキストファイルを閉じます。

 
パラメータ

なし

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim tsObject    ' TextStream Object

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the OpenTextFile method of fsoObject, initialize 
' the File object.
Set tsObject = fsoObject.OpenTextFile( _
               "d:\docs\test.txt", ForReading, False)

' Read into the string the contents of the text file.
strContents = tsObject.ReadAll
' Close the open text file.
tsObject.Close
. . . [additional code]

%>
 
メモ

Visual Basic での開いているファイルの使用方法と同様に、アプリケーションで許されるオープンファイル数は限られているため、処理が終了したら開いているテキストファイルをすべて閉じることが重要です。

 
Copy (File オブジェクト、Folder オブジェクト)  
obj.Copy strDestination [, blnOverWrite]
 

ファイルをある場所から別の場所へコピーします。

 
パラメータ
strDestination

現在のファイルのコピー先の完全パスを表す文字列です。

blnOverWrite

コピー対象のファイルと同じ名前のファイルを上書きするかどうかを示すブール値です。デフォルト値は True です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim filObject   ' File Object

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize 
' the File object.
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Copy the file to a temporary directory.
filObject.Copy "e:\storage\temp\test_copy.txt", True
. . . [additional code]

%>
 
メモ

Copy メソッドは、FileSystemObject オブジェクトの CopyFile メソッドや CopyFolder メソッドとまったく同じ機能を実行します。ただし、CopyFile メソッドと CopyFolder メソッドでは、複数のファイルやフォルダを一度にコピーできることに注意する必要があります。

 
CopyFolder (FileSystemObject オブジェクト)  
fsoObj.CopyFolder strSource, strDestination [, blnOverWrite]
 

フォルダとそのすべてのコンテンツをある場所から別の場所へコピーできます。

 
パラメータ
strSource

フォルダのコピー元の完全パスを表す文字列です。strSource には、ワイルドカード文字を指定できます。

strDestination

strSource で指定されたフォルダのコピー先の完全パスを表す文字列です。

blnOverWrite

コピー対象のファイルと同じ名前のファイルを上書きするかどうかを示すブール値です。デフォルト値は True です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Use the FileSystemObject object's CopyFolder method
' to copy the Temp directory and all its contents from
' the C drive to the D drive, overwriting if necessary.
fsoObject.CopyFolder "c:\temp", "d:\temp", True
. . . [additional code]

%>
 
メモ

CopyFolder の呼び出しでエラーが発生すると、このメソッドはすぐに停止し、既に実行済みのアクションを元に戻すことはしません。

CopyFolder メソッドの速さは、コマンド行によるフォルダのコピーと同程度の速さです。

 
CreateFolder (FileSystemObject オブジェクト)  
fsoObj.CreateFolder( strFolderName)
 

フォルダを指定された場所に作成します。

 
パラメータ
strFolderName

作成するフォルダの完全物理パスを表す文字列です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Create a new directory.
fsoObject.CreateFolder("e:\storage\newdir")
. . . [additional code]

%>
 
メモ

既に存在しているフォルダを作成しようとすると、エラーが発生します。

 
Delete (File オブジェクト、Folder オブジェクト)  
Obj.Delete blnForce
 

ファイルまたはフォルダを削除します。

 
パラメータ
blnForce

読み取り専用のマークが付いている場合でもファイルまたはフォルダを削除するかどうかを示すブール値です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim filObject   ' File Object

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize the 
' File object.
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Delete the TEST.TXT file--even if the file is marked 
' as read-only.
filObject.Delete True
. . . [additional code]

%>
 
メモ

File オブジェクトと Folder オブジェクトの Delete メソッドは、FileSystemObject オブジェクトの DeleteFile メソッドおよび DeleteFolder メソッドと機能的には同じです。Folder オブジェクトの Delete メソッドを使用すると、該当するフォルダとそのすべてのコンテンツが削除されます。このメソッドは、ファイルを含むディレクトリを削除しようとしても警告を出しません。

 
GetBaseName (FileSystemObject オブジェクト)  
fsoObj.GetBaseName( strPath)
 

完全ファイルパスから "ファイル拡張子を除いた" ファイル名を抽出します。

 
パラメータ
strPath

その基本名を取得しようとする所定のファイルの完全ファイルパスを表す文字列です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetBaseName method, retrieve the base 
' names of several path strings. 
' This example returns "searchstart" as the base name.
Response.Write fsoObject.GetBaseName( _
               "/apps/search/searchstart.asp")
' This example returns "search" as the base name.
Response.Write fsoObject.GetBaseName("/apps/search/")
' This example returns "search" as the base name.
Response.Write fsoObject.GetBaseName("/apps/search")
' This example returns "nofile" as the base name--even
' though the nofile.txt file does not exist.
fsoObject.GetBaseName("/apps/search/nofile.txt")
. . . [additional code]

%>
 
メモ

GetBaseName は、パス文字列からファイルの基本名の取得を試みます。パス文字列の最後の要素がフォルダである場合、閉じるスラッシュ (/) またはバックスラッシュ (\) 文字を指定しても、フォルダ名が返されます。パス文字列は、サーバー上の実際のパスとしてその有効性と存在がチェックされることはありません。このメソッドは、パスを文字列とみなすだけです。このため、このメソッドと FileSystemObject オブジェクトとを関連付けることは、ファイル操作が実際に行われないため誤解を生みます。

 
GetParentFolderName (FileSystemObject オブジェクト)  
fsoObj.GetFolderName ( strPath)
 

指定されたパス文字列内の最後の親フォルダの名前を判別します。

 
パラメータ
strPath

その親フォルダ名を取得しようとする、所定のファイルまたはフォルダの完全ファイルパスを表す文字列です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject(
                "Scripting.FileSystemObject")
' Using the GetParentFolderName method, retrieve the 
' parent folder names of several path strings. 
' This example returns "search" as the parent folder
' name.
Response.Write fsoObject.GetParentFolderName( _
               "/apps/search/searchstart.asp")
' This example return "apps" as the parent folder name
Response.Write fsoObject.GetParentFolderName ("/apps/search/")
' This example also returns "apps" as the parent folder 
' name.
Response.Write fsoObject.GetParentFolderName ("/apps/search")
' This example returns "nofile" as the parent folder 
' name--even though nofile.txt does not exist.
Response.Write fsoObject.GetParentFolderName( _
               "/apps/search/nofile.txt")
. . . [additional code]

%>
 
メモ

FileSystemObject オブジェクトの GetBaseName メソッドと同様に、GetParentFolderName メソッドはパス文字列自身でのみ動作します。パス文字列パラメータの有効性と存在がチェックされることはありません。

 
GetSpecialFolder (FileSystemObject オブジェクト)  
fsoObj.GetSpecialFolder (intSpecialFolderType)
 

Web サーバー上の特別なフォルダの完全物理パスを取得します。

 
パラメータ
intSpecialFolderType

その完全物理パスを取得する特別なフォルダのタイプを表す整数です。このパラメータに指定できる値は次のとおりです。

定数

説明

WindowsFolder

0

オペレーティングシステムをインストールした Windows フォルダまたは WinNT フォルダ

SystemFolder

1

ライブラリとデバイスドライバがインストールされているシステムフォルダ

TemporaryFolder

2

環境変数内で宣言されている一時フォルダ

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
' Declare file constants.
Const WindowsFolder   = 0
Const SystemFolder    = 1
Const TemporaryFolder = 2

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Use GetSpecialFolder to retrieve the physical path
' for the Windows, System, and Temp directories. 
' This example returns something similar to "C:\WINNT". 
fsoObject.GetSpecialFolder(WindowsFolder)
' This example returns something similar to 
' "C:\WINNT\SYSTEM32". 
fsoObject.GetSpecialFolder(SystemFolder)

' This example returns something similar to 
' "C:\WINNT\TEMP" 
fsoObject.GetSpecialFolder(TemporaryFolder)
. . . [additional code]

%>
 
メモ

ファイルアクセスコンポーネントで使用する定数は、明示的に宣言する必要があります。

 
MoveFolder (FileSystemObject オブジェクト)  
fsoObj.MoveFolder strSourcePath, strDestinationPath
 

フォルダとそのすべてのコンテンツをある場所から別の場所へ移動します。

 
パラメータ
strSourcePath

移動対象のフォルダへのパスを表す文字列です。ワイルドカード文字を指定できるのは、パスの最後のセグメントの strSourcePath パラメータのみです。

strDestinationPath

strSourcePath パラメータで参照されるフォルダの移動先のパスを表す文字列です。strDestinationPath パラメータには、ワイルドカード文字を指定できません。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the MoveFolder method, move all the folders 
' under C:\APPS to the D: drive.
fsoObject.MoveFolder "C:\APPS\*.*", "D:\"
. . . [additional code]

%>
 
メモ

フォルダを既にファイル名となっている宛先に移動しようとすると、エラーが発生します。指定する宛先が既存のフォルダの名前を表している場合、ソースパラメータがワイルドカードまたはバックスラッシュ (\) で終了していない限り、エラーが発生します。この場合、ソースフォルダとそのすべてのコンテンツは、宛先フォルダに移動されます。たとえば、次のコードではエラーが発生します。

<%
' Assume FileSystemObject object is instantiated
'already. Also assume that D:\ apps already exists.
fsoObject.MoveFolder "C:\apps", "d:\apps"
%>

一方、次のコードではエラーは発生しません。

<%
' Assume FileSystemObject object is instantiated
' already. Also assume that D:\ apps already exists.
fsoObject.MoveFolder "C:\apps\*.*", "d:\apps"
' This last line create an apps folder in the D:\apps 
' folder (making D:\apps\apps)
%>

MoveFolder の呼び出し時に Web サーバーでエラーが発生した場合、すべてのアクションが停止します。前のアクションのロールバックは行われません。たとえば、3 つの一連のフォルダをそのすべてのコンテンツと共に移動しようとして、移動対象の 3 番目のフォルダでエラーが発生した場合、3 番目のフォルダが移動されていない状態でも、最初の 2 つのフォルダは移動されたままの状態になります。実際に移動されたファイルやフォルダ、および移動されていないファイルやフォルダをチェックするには、独自のコードを指定する必要があります。

ボリューム間でフォルダを移動する場合、基になるオペレーティングシステムがこの操作をサポートし、Web サーバー上のユーザーセキュリティでこの操作を許容する必要があります。

 
OpenAsTextStream (File オブジェクト)  
tsObject =filObj.OpenAsTextStream [ intAccessMode][, intFormat]
 

ファイルを開き、テキストファイルの読み取りまたは変更を行える TextStream オブジェクトを作成します。このメソッドは TextStream オブジェクトを返します。

 
パラメータ
intAccessMode

テキストファイルを開く入出力モードを示す整数です。このパラメータに指定できる値は次のとおりです。

定数

説明

ForReading

1

ファイルは読み取り専用として開かれ、現在の TextStream オブジェクトでは変更できません。

ForWriting

2

ファイルは書き込み用として開かれます。OpenAsTextStream メソッドの呼び出し時にファイルが既に存在している場合、元のファイルが上書きされます。

ForAppending

8

ファイルは追加専用として開かれます。このファイルの最後に文字のみ追加できます。

intFormat

TextStream オブジェクトとして開かれるファイルのフォーマットを示す整数です。このパラメータに指定できる値は、単一のトライステート値とみなされます。ファイルは、システムデフォルトとなっている Unicode、ASCII のいずれかです。このパラメータに指定できる値は次のとおりです。

定数

説明

TristateUseDefault

-2

ファイルフォーマットは、Web サーバーのデフォルト (Unicode または ASCII) と同じです。

TristateTrue

-1

ファイルフォーマットは Unicode です。

TristateFalse

0

ファイルフォーマットは ASCII です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim filObject   ' File Object
Dim tsObject    ' TextStream object

' Declare File Access constants.
Const ForAppending = 8
Const TristateTrue = -1

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize the 
' File object. 
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Use the OpenAsTextStream method to open the file for 
' appending and in Unicode format.
Set tsObject = filObject.OpenAsTextStream(ForAppending, TristateTrue)
%>
 
メモ

OpenAsTextStream メソッドは、実際上は FileSystemObject オブジェクトの OpenTextFile メソッドに相当します。唯一の違いは、OpenAsTextStream メソッドではまた、ファイルが存在していない場合に、新しいテキストファイルを作成できることです。

ファイルアクセスコンポーネントで使用する定数は、明示的に宣言する必要があります。

 
ReadLine (TextStream オブジェクト)  
tsObj.ReadLine
 

ReadLine メソッドは、テキストファイルから文字列変数への読み込み、またはこのような読み込みの結果と他のエンティティとの比較が可能という点で、TextStream オブジェクトの Read メソッドに似ています。ただし、パラメータを使用して読み取り文字数を判断する Read メソッドとは異なり、ReadLine メソッドは、現在のポインタ位置から次の改行文字までのすべての文字を読み取ります。

 
パラメータ

なし

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim filObject   ' File Object
Dim tsObject    ' TextStream object
Dim strBuffer   ' Holding buffer

' Declare file access constants.
Const ForReading = 1
Const TristateFalse = 0

' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize the 
' File object.
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Use the OpenAsTextStream method to open the file for 
' reading and in ASCII format.
Set tsObject = filObject.OpenAsTextStream(ForReading, TristateFalse)
' Use the ReadLine method to read the next line of text
' from the text file into the strBuffer variable.
strBuffer = tsObject.ReadLine
%>
 
メモ

ReadLine メソッドの呼び出し後、ファイル内のポインタの現在の位置は、最後の改行文字の直後、またはファイルマーカーの最後に置かれます。

ファイルアクセスコンポーネントで使用する定数は、明示的に宣言する必要があります。

 
Write (TextStream オブジェクト)  
tsObj.Write(strWriteString)
 

開いているテキストファイルのファイルポインタの現在の位置に、指定された文字列を書き込みます。

 
パラメータ
strWriteString

オープンファイルに書き込むテキストを表す文字列です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim filObject   ' File Object
Dim tsObject    ' TextStream object
Dim strEnding
' Declare file access constants.
Const ForAppending = 8
Const TristateFalse = 0

' Initialize string variable. This string will be 
' written to the end of the file opened next.
strEnding = "This is the end, my only friend, the end..."
' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize the
' File object.
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Use the OpenAsTextStream method to open the file for 
' appending and in Unicode format.
Set tsObject = filObject.OpenAsTextStream(ForAppending, TristateFalse)
' Write a short string to the end of the opened file.
tsObject.Write strEnding
. . . [additional code]

%>
 
メモ

Write メソッドは、書き込まれる各文字列の先頭または末尾に何の文字も配置しません。このため、Write メソッドを使用してファイルに追加する場合、目的の文字 (空白、改行文字など) をファイルに書き込む文字列の先頭または末尾に指定してください。

 
WriteLine (TextStream オブジェクト)  
tsObj.WriteLine([strWriteString])
 

文字列をオープンファイルのファイル内のポインタの位置に書き込みます。このメソッドはまた、改行文字を追加される文字列の末尾に書き込みます。それ以外は、Write メソッドとまったく同じです。

strWriteString

オープンテキストファイルに書き込むテキストを表す文字列です。

 
<%

' Dimension local variables. 
Dim fsoObject   ' FileSystemObject
Dim filObject   ' File Object
Dim tsObject    ' TextStream object
Dim strEnding
' Declare file access constants.
Const ForAppending = 8
Const TristateFalse = 0

' Initialize a string variable that will be written to 
' the end of the file opened next.
strEnding = "This is the end, my only friend, the end..."
' Instantiate the FileSystemObject variable.
Set fsoObject = Server.CreateObject( _
                "Scripting.FileSystemObject")
' Using the GetFile method of fsoObject, initialize the 
' File object.
Set filObject = fsoObject.GetFile("d:\docs\test.txt")

' Use the OpenAsTextStream method to open the file for 
' appending and in Unicode format.
Set tsObject = filObject.OpenAsTextStream(ForAppending, TristateFalse)
' Write a short string plus a newline character to the
' end of the opened file.
tsObject.WriteLine strEnding
. . . [additional code]

%>
 
メモ

WriteLine メソッドの呼び出し後、ファイルポインタは、ファイルに追加された改行文字の直後に置かれている文字をポイントします。