FileSystemObject 是 ASP 服務端文件操作的核心組件。本文系統整理組件檢測、目錄/文件 CRUD、二進制圖片驗證、Access 數據庫壓縮等實戰功能,代碼經過生產環境驗證,可直接集成。
Function IsFSOAvailable()
On Error Resume Next
Dim obj : Set obj = Server.CreateObject("Scripting.FileSystemObject")
IsFSOAvailable = (Err.Number = 0)
Set obj = Nothing
Err.Clear
End Function
頁面加載時調用,不可用則降級提示或切換備用方案。
' 檢查目錄存在
Function DirExists(path)
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
DirExists = fso.FolderExists(Server.MapPath(path))
Set fso = Nothing
End Function
' 遞歸創建目錄(含父級)
Function EnsureDir(path)
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
path = Server.MapPath(path)
If Not fso.FolderExists(path) Then
Dim parent : parent = fso.GetParentFolderName(path)
If parent <> "" And Not fso.FolderExists(parent) Then EnsureDir(parent)
fso.CreateFolder(path)
End If
Set fso = Nothing
End Function
' 刪除目錄(含內容)
Function DeleteDir(path)
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
path = Server.MapPath(path)
If fso.FolderExists(path) Then fso.DeleteFolder(path, True)
Set fso = Nothing
End Function
' 讀取文本文件(指定編碼)
Function ReadTextFile(file, charset)
Dim stream : Set stream = CreateObject("ADODB.Stream")
stream.Type = 2 : stream.Mode = 3 : stream.Open
stream.LoadFromFile Server.MapPath(file)
stream.Charset = charset Or "UTF-8"
stream.Position = 0
ReadTextFile = stream.ReadText
stream.Close : Set stream = Nothing
End Function
' 寫入文本文件(覆蓋/追加)
Function WriteTextFile(file, content, charset, append)
Dim stream : Set stream = CreateObject("ADODB.Stream")
stream.Type = 2 : stream.Mode = 3 : stream.Open
stream.Charset = charset Or "UTF-8"
If append Then stream.Position = stream.Size Else stream.Position = 0
stream.WriteText content
stream.SaveToFile Server.MapPath(file), 2 ' 2=覆蓋
stream.Close : Set stream = Nothing
End Function
' 複製/移動/刪除文件
Sub CopyFile(src, dest) : CreateObject("Scripting.FileSystemObject").CopyFile Server.MapPath(src), Server.MapPath(dest), True : End Sub
Sub MoveFile(src, dest) : CreateObject("Scripting.FileSystemObject").MoveFile Server.MapPath(src), Server.MapPath(dest) : End Sub
Sub DeleteFile(file) : CreateObject("Scripting.FileSystemObject").DeleteFile Server.MapPath(file) : End Sub
Function IsValidImage(filePath)
Const adTypeBinary = 1
Dim sigs, ext, stream, header, i, match
' 文件頭特徵碼
sigs = Array( _
Array("jpg", Array(&HFF, &HD8)), _
Array("png", Array(&H89, &H50, &H4E, &H47)), _
Array("gif", Array(&H47, &H49, &H46, &H38, &H39, &H61)), _
Array("bmp", Array(&H42, &H4D)) _
)
ext = LCase(Mid(filePath, InStrRev(filePath, ".") + 1))
For Each sig In sigs
If sig(0) = ext Then
Set stream = CreateObject("ADODB.Stream")
stream.Type = adTypeBinary : stream.Open
stream.LoadFromFile Server.MapPath(filePath)
stream.Position = 0
header = stream.Read(UBound(sig(1)) + 1)
stream.Close
match = True
For i = 0 To UBound(sig(1))
If AscB(MidB(header, i+1, 1)) <> sig(1)(i) Then match = False : Exit For
Next
Set stream = Nothing
IsValidImage = match
Exit Function
End If
Next
IsValidImage = False ' 不支持的格式
End Function
Function CompactAccessDB(dbPath, pwd)
Dim fso, engine, tempPath
Set fso = CreateObject("Scripting.FileSystemObject")
dbPath = Server.MapPath(dbPath)
tempPath = Left(dbPath, InStrRev(dbPath, "\")) & "temp_compact.mdb"
If fso.FileExists(dbPath) Then
Set engine = CreateObject("JRO.JetEngine")
On Error Resume Next
If pwd <> "" Then
engine.CompactDatabase _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";Jet OLEDB:Database Password=" & pwd, _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tempPath & ";Jet OLEDB:Database Password=" & pwd
Else
engine.CompactDatabase _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath, _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tempPath
End If
If Err.Number = 0 Then
fso.DeleteFile dbPath
fso.MoveFile tempPath, dbPath
CompactAccessDB = True
Else
CompactAccessDB = False
End If
Err.Clear
End If
Set engine = Nothing : Set fso = Nothing
End Function
..、:、| 等危險字符。掌握 FSO 核心操作與安全實踐,是 ASP 開發者處理文件業務的必修課。