FSO - FileSystemObject 或 Scripting.FileSystemObject 的縮寫,為 IIS 內置組件,用于操作磁盤、文件夾或文本文件。FSO 的對象、方法和屬性非常的多,這里用示例的方式列出常用的,如果您要查看更詳盡的信息,請點擊這里下載 FileSystemObject 參考,注意:《VBScript 語言參考》或《JScript 語言參考》中的:《FileSystemObject 用戶指南》和《Scripting 運行時庫參考》便是微軟給出的 FileSystemObject 完整參考。
FSO 不能操作二進制文件,要操作二進制文件,請使用:ADODB.Stream。
創建文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) '第二個參數表示目標文件存在時是否覆蓋
f.Write("寫入內容")
f.WriteLine("寫入內容并換行")
f.WriteBlankLines(3) '寫入三個空白行(相當于在文本編輯器中按三次回車)
f.Close()
set f = nothing
set fso = nothing
打開并讀文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二個參數 1 表示只讀打開,第三個參數表示目標文件不存在時是否創建
f.Skip(3) '將當前位置向后移三個字符
f.SkipLine() '將當前位置移動到下一行的第一個字符,注意:無參數
response.Write f.Read(3) '從當前位置向后讀取三個字符,并將當前位置向后移三個字符
response.Write f.ReadLine() '從當前位置向后讀取直到遇到換行符(不讀取換行符),并將當前位置移動到下一行的第一個字符,注意:無參數
response.Write f.ReadAll() '從當前位置向后讀取,直到文件結束,并將當前位置移動到文件的最后
if f.atEndOfLine then
response.Write("一行的結尾!")
end if
if f.atEndOfStream then
response.Write("文件的結尾!")
end if
f.Close()
set f = nothing
set fso = nothing
打開并寫文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二個參數 2 表示重寫,如果是 8 表示追加
f.Write("寫入內容")
f.WriteLine("寫入內容并換行")
f.WriteBlankLines(3) '寫入三個空白行(相當于在文本編輯器中按三次回車)
f.Close()
set f = nothing
set fso = nothing
判斷文件是否存在
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FileExists("C:\test.txt") then
response.Write("目標文件存在")
else
response.Write("目標文件不存在")
end if
set fso = nothing
移動文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") '兩個參數的文件名部分可以不同
set fso = nothing
復制文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") '兩個參數的文件名部分可以不同
set fso = nothing
刪除文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:\test.txt")
set fso = nothing
創建文件夾
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder("C:\test") '目標文件夾的父文件夾必須存在
set fso = nothing
判斷文件夾是否存在
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
response.Write("目標文件夾存在")
else
response.Write("目標文件夾不存在")
end if
set fso = nothing
刪除文件夾
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\test") '文件夾不必為空
set fso = nothing
CreateTextFile
(filename,overwrite,unicode)
用指定的文件名在文件夾內創建一個新的文本文件,并且返回一個相應的TextStream對象。如果可選的overwrite參數設置為True,將覆蓋任何已有的同名文件。缺省的overwrite參數是False。如果可選的unicode參數設置為True,文件的內容將存儲為unicode文本。缺省的unicode是False
在文件夾之間可以使用當前文件夾的ParentFolder屬性,返回到父目錄。當到達一個文件夾時,如果IsRootFolder屬性是True,就停下來。離開驅動器的根目錄,沿目錄樹向下,可遍歷或訪問在Folders集合(由當前文件夾的SubFolders屬性返回)內的指定文件夾。
下列程序遍歷了驅動器C根目錄內的所有文件夾,并顯示各個文件夾的有關信息。
VBScript程序如下:
'In VBScript:
' Create a FileSystemObject instance
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' Get a reference to drive C
Set objDriveC = objFSO.GetDrive("C:")
' Get a reference to the root folder
Set objRoot = objDriveC.RootFolder
' Get a reference to the SubFolders collection
Set objFolders = objRoot.SubFolders
' Get a reference to the first folder in the SubFolders collection
For Each objFolder In objFolders
Set objFolder1 = objFolders.Item((objFolder.Name))
Exit For
Next
' Iterate through all the files in this folder
For Each objFile in objFolder1.Files
Response.Write "Name: " & objFile.Name & " "
Response.Write "ShortName: " & objFile.ShortName & " "
Response.Write "Size: " & objFile.Size & " bytes "
Response.Write "Type: " & objFile.Type & "<BR>"
Response.Write "Path: " & objFile.Path & " "
Response.Write "ShortPath: " & objFile.ShortPath & "<BR>"
Response.Write "Created: " & objFile.DateCreated & " "
Response.Write "LastModified: " & objFile.DateLastModified & "<P>"
Next
JScript程序如下:
//In JScript:
// Create a FileSystemObject instance
var objFSO = Server.CreateObject('Scripting.FileSystemObject');
// Get a reference to drive C
var objDriveC = objFSO.GetDrive('C:');
// Get a reference to the root folder
var objRoot = objDriveC.RootFolder;
// Get a reference to the first folder in the SubFolders collection
var colAllFolders = new Enumerator(objRoot.SubFolders);
var objFolder1 = colAllFolders.item();
// Get a reference to the Files collection for this folder
var colFiles = new Enumerator(objFolder1.Files);
// Iterate through all the files in this collection
for (; !colFiles.atEnd(); colFiles.moveNext()) {
objFile = colFiles.item()
Response.Write('Name: ' + objFile.Name + ' ');
Response.Write('ShortName: ' + objFile.ShortName + ' ');
Response.Write('Size: ' + objFile.Size + ' bytes ');
Response.Write('Type: ' + objFile.Type + '<BR>');
Response.Write('Path: ' + objFile.Path + ' ');
Response.Write('ShortPath: ' + objFile.ShortPath + '<BR>');
Response.Write('Created: ' + objFile.DateCreated + ' ');
Response.Write('Accessed: ' + objFile.DateLastAccessed + ' ');
Response.Write('Modified: ' + objFile.DateLastModified + '<P>');
}