ASP FSO文件操作

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

實例:文件刪除
SUB DelFile(filename)
dim fo,filenamestr
set fo=Server.Createobject("Scripting.FileSystemObject")
filenamestr=Server.MapPath(filename)
if fo.FileExists(filenamestr) then
fo.DeleteFile filenamestr
end if
set fo=nothing
END SUB


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章