熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp之FSO大全

2022-06-13   來源: .NET編程 

  對於一個支持asp和fso的空間來說有了fso一切變得簡單多了

我也是個新手寫了一些代碼供大家學習研究用

首先看支持fso組件嗎

<%
FSO組件名稱
dim FSObject
FSObject="ScriptingFileSystemObject"

=========================================================
◆是否支持組件
=========================================================
Function IsObjInstalled(strClassString)
On Error Resume Next
IsObjInstalled = False
Err =
Dim xTestObj
Set xTestObj = ServerCreateObject(strClassString)
If = Err Then IsObjInstalled = True
Set xTestObj = Nothing
Err =
End Function

if IsObjInstalled(FSObject) then
responsewrite "√"
else
responsewrite "×"
end if%>

<%
=========================================================
◆是否支持組件
=========================================================
Function IsObjInstalled(strClassString)
On Error Resume Next
IsObjInstalled = False
Err =
Dim xTestObj
Set xTestObj = ServerCreateObject(strClassString)
If = Err Then IsObjInstalled = True
Set xTestObj = Nothing
Err =
End Function
=========================================================
fso 操作
=========================================================
◆檢查某一目錄是否存在
=========================================================
Function CheckDir(FolderPath)
folderpath=ServerMapPath("")&""&folderpath
Set fso= CreateObject(FSObject)
If fsoFolderExists(FolderPath) then
CheckDir = True
Else
CheckDir = False
End if
Set fso= nothing
End Function
=========================================================
◆ 根據指定名稱生成目錄
=========================================================
Function MakeNewsDir(foldername)
dim fs
Set fso= CreateObject(FSObject)
Set fs= fsoCreateFolder(foldername)
Set fso = nothing
End Function
=========================================================
◆ 如果文件夾不存在則建立新文件夾 ◆
=========================================================
Function checkFolder(folderpath)
If CheckDir(folderpath) = false Then如果文件夾不存在
MakeNewsDir(folderpath)就建一個文件夾
end if
end Function
=========================================================
◆ 刪除文件夾 ◆
=========================================================
Function DeleteFoldera(folderpath)
dim path
Set fso = CreateObject(FSObject)
path=requestServerVariables("APPL_PHYSICAL_PATH")&folderpath
fsoDeleteFolder(path)
Set fso = nothing
end Function
=========================================================
◆ 更改文件夾名稱 ◆
=========================================================
Function moveFolder(foldernamenewfoldername)
isfso
Set fso = CreateObject(FSObject)
fsomoveFolder ""&requestServerVariables("APPL_PHYSICAL_PATH")&""&foldername&"" ""&requestServerVariables("APPL_PHYSICAL_PATH")&""&newfoldername&""
Set fso =nothing
End Function
=========================================================
◆ 刪除指定文件 ◆
=========================================================
Function DeleteFile(file)
Set fso = CreateObject(FSObject)
fsoDeleteFile requestServerVariables("APPL_PHYSICAL_PATH")&file
Set fso = nothing
End Function
=========================================================
◆ 備份指定文件 ◆
=========================================================
Function CopyFile(oldfilenewfile)
Set fso = CreateObject(FSObject)
On Error Resume Next
Set fso=ServerCreateObject(FSObject)
oldfile=ServerMapPath(oldfile)
if ErrNumber> Then call alert("原路徑錯誤!""")
newfile=ServerMapPath(newfile)
if ErrNumber> Then call alert("新路徑錯誤!""")
fsoCopyFile oldfilenewfile覆蓋原來的文件
if ErrNumber> Then call alert(ErrDescription"")
Set fso=nothing
End Function
=========================================================
◆ 轉移指定文件 ◆
=========================================================
Function MoveFile(oldfilenewfile)
Set fso = CreateObject(FSObject)
On Error Resume Next
Set fso=ServerCreateObject(FSObject)
oldfile=ServerMapPath(oldfile)
if ErrNumber> Then call alert("原路徑錯誤!""")
newfile=ServerMapPath(newfile)
if ErrNumber> Then call alert("新路徑錯誤!""")
fsoMoveFile oldfilenewfile不能覆蓋原來的文件
fsoMoveFile "d:odatatesttxt""d:odatabackuptesttxt"
if ErrNumber> Then call alert(ErrDescription"")
Set fso=nothing
End Function
=========================================================
◆ 讀取文件代碼 ◆
=========================================================
Function loadfile(file)讀取文件
dim ftemp
Set fso = CreateObject(FSObject)
Set ftemp=fsoOpenTextFile(ServerMapPath(""&file&"") )
loadfile=ftempReadAll
ftempClose
fsoclose
set fso=nothing
End Function
=========================================================
◆ 根據代碼生成文件 ◆
=========================================================
========================================
■file生成文件名
■code文件的代碼
========================================
Function savefile(filecode)保存文件
dim MyFile
Set fso = CreateObject(FSObject)
Set MyFile = fsoCreateTextFile(ServermapPath(file) True)
MyFileWriteLine(code)
MyFileClose
set MyFile=nothing
End Function
=========================================================
◆ 壓縮數據庫 ◆
=========================================================
========================================
■dbPath數據文件路徑
■boolIs access壓縮
========================================
Function CompactDB(dbPathboolIs)
dim strDBPathfsoEngine
dbPath=servermappath(dbpath)
strDBPath = left(dbPathinstrrev(DBPath""))
Set fso = CreateObject(FSObject)
If fsoFileExists(dbPath) Then
Set Engine = CreateObject("JROJetEngine")
If boolIs = "True" Then
dim JET_X
EngineCompactDatabase "Provider=MicrosoftJetOLEDB;Data Source=" & dbpath _
"Provider=MicrosoftJetOLEDB;Data Source=" & strDBPath & "tempmdb;" _
&"Jet OLEDB:Engine Type=" & JET_X
Else
EngineCompactDatabase "Provider=MicrosoftJetOLEDB;Data Source=" & dbpath _
"Provider=MicrosoftJetOLEDB;Jet OLEDB:Database password="&dbpw&";Data Source="&strDBPath&"tempmdb"
End If
fsoCopyFile strDBPath & "tempmdb"dbpath
fsoDeleteFile(strDBPath&"tempmdb")
Set fso = nothing
Set Engine = nothing
CompactDB="當前數據庫已經壓縮成功!"
Else
CompactDB="數據庫名稱或路徑不正確 請重試!"
End If
End Function
%>


From:http://tw.wingwit.com/Article/program/net/201311/14161.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.