FSO是微軟ASP的一個對文件操作的控件,那么接下來就由錯新頻道的小編給小伙伴們分享一篇關于FSO代碼詳細解析吧。
使用FSO修改文件特定內容的函數
functionFSOchange(filename,Target,String)
DimobjFSO,objCountFile,FiletempData
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)
FiletempData=objCountFile.ReadAll
objCountFile.Close
FiletempData=Replace(FiletempData,Target,String)
SetobjCountFile=objFSO.CreateTextFile(Server.MapPath(filename),True)
objCountFile.WriteFiletempData
objCountFile.Close
SetobjCountFile=Nothing
SetobjFSO=Nothing
EndFunction
使用FSO讀取文件內容的函數
functionFSOFileRead(filename)
DimobjFSO,objCountFile,FiletempData
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)
FSOFileRead=objCountFile.ReadAll
objCountFile.Close
SetobjCountFile=Nothing
SetobjFSO=Nothing
EndFunction
使用FSO讀取文件某一行的函數
functionFSOlinedit(filename,lineNum)
iflinenum
dimfso,f,temparray,tempcnt
setfso=server.CreateObject("scripting.filesystemobject")
ifnotfso.fileExists(server.mappath(filename))thenexitfunction
setf=fso.opentextfile(server.mappath(filename),1)
ifnotf.AtEndofStreamthen
tempcnt=f.readall
f.close
setf=nothing
temparray=split(tempcnt,chr(13)&chr(10))
iflineNum>ubound(temparray)+1then
exitfunction
else
FSOlinedit=temparray(lineNum-1)
endif
endif
endfunction
使用FSO寫文件某一行的函數
functionFSOlinewrite(filename,lineNum,Linecontent)
iflinenum
dimfso,f,temparray,tempCnt
setfso=server.CreateObject("scripting.filesystemobject")
ifnotfso.fileExists(server.mappath(filename))thenexitfunction
setf=fso.opentextfile(server.mappath(filename),1)
ifnotf.AtEndofStreamthen
tempcnt=f.readall
f.close
temparray=split(tempcnt,chr(13)&chr(10))
iflineNum>ubound(temparray)+1then
exitfunction
else
temparray(lineNum-1)=lineContent
endif
tempcnt=join(temparray,chr(13)&chr(10))
setf=fso.createtextfile(server.mappath(filename),true)
f.writetempcnt
endif
f.close
setf=nothing
endfunction
使用FSO添加文件新行的函數
functionFSOappline(filename,Linecontent)
dimfso,f
setfso=server.CreateObject("scripting.filesystemobject")
ifnotfso.fileExists(server.mappath(filename))thenexitfunction
setf=fso.opentextfile(server.mappath(filename),8,1)
f.writechr(13)&chr(10)&Linecontent
f.close
setf=nothing
endfunction
讀文件最后一行的函數
functionFSOlastline(filename)
dimfso,f,temparray,tempcnt
setfso=server.CreateObject("scripting.filesystemobject")
ifnotfso.fileExists(server.mappath(filename))thenexitfunction
setf=fso.opentextfile(server.mappath(filename),1)
ifnotf.AtEndofStreamthen
tempcnt=f.readall
f.close
setf=nothing
temparray=split(tempcnt,chr(13)&chr(10))
FSOlastline=temparray(ubound(temparray))
endif
endfunction
FSO替換指定文件的字符
程序代碼:
'FSO替換指定文件的字符
FunctionFSOLineEdit(filename,Target,String)
DimobjFSO,objCountFile,FiletempData
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)
FiletempData=objCountFile.ReadAll
objCountFile.Close
FiletempData=Replace(FiletempData,Target,String)
SetobjCountFile=objFSO.CreateTextFile(Server.MapPath(filename),True)
objCountFile.WriteFiletempData
objCountFile.Close
SetobjCountFile=Nothing
SetobjFSO=Nothing
EndFunction
'Response.WriteFSOLineEdit("test.txt","世界","明天是一個好天去")
刪除文件
程序代碼:
'刪除文件
FunctionDelFile(Filename)
IfFilename""Then
SetFSO=Server.CreateObject("Scripting.FileSystemObject")
IfFSO.FileExists(Filename)Then
FSO.DeleteFileFilename
EndIf
SetFSO=Nothing
EndIf
EndFunction
判斷文件是否存在
程序代碼:
'判斷文件是否存在
FunctionReportFileStatus(filespec)
DimFSO,msg
SetFSO=CreateObject("Scripting.FileSystemObject")
If(FSO.FileExists(filespec))Then
msg=filespec&"exists."
Else
msg=filespec&"doesn'texist."
EndIf
ReportFileStatus=msg
EndFunction
使用FSO修改文件特定內容的函數
程序代碼:
'使用FSO修改文件特定內容的函數
FunctionFSOchange(filename,Target,String)
DimobjFSO,objCountFile,FiletempData
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)
FiletempData=objCountFile.ReadAll
objCountFile.Close
FiletempData=Replace(FiletempData,Target,String)
SetobjCountFile=objFSO.CreateTextFile(Server.MapPath(filename),True)
objCountFile.WriteFiletempData
objCountFile.Close
SetobjCountFile=Nothing
SetobjFSO=Nothing
EndFunction
使用FSO寫文件某一行的函數
程序代碼:
'使用FSO寫文件某一行的函數
FunctionFSOlinewrite(filename,lineNum,Linecontent)
Iflinenum
DimFSO,f,temparray,tempCnt
SetFSO=Server.CreateObject("Scripting.FileSystemObject")
IfNotFSO.FileExists(Server.MapPath(filename))ThenExitFunction
Setf=FSO.OpenTextFile(Server.MapPath(filename),1)
IfNotf.AtEndofStreamThen
tempcnt=f.ReadAll
f.Close
temparray=Split(tempcnt,Chr(13)&Chr(10))
IflineNum>UBound(temparray)+1Then
ExitFunction
Else
temparray(lineNum-1)=lineContent
EndIf
tempcnt=join(temparray,Chr(13)&Chr(10))
Setf=FSO.createtextfile(Server.MapPath(filename),true)
f.Writetempcnt
EndIf
f.Close
Setf=Nothing
EndFunction
建立目錄的程序,如果有多級目錄,則一級一級的創建
程序代碼:
'建立目錄的程序,如果有多級目錄,則一級一級的創建
FunctionCreateDIR(ByValLocalPath)
OnErrorResumeNext
LocalPath=Replace(LocalPath,"/","/")
SetFileObject=Server.CreateObject("Scripting.FileSystemObject")
patharr=Split(LocalPath,"/")
path_level=UBound(patharr)
Fori=0topath_level
Ifi=0Thenpathtmp=patharr(0)&"/"Elsepathtmp=pathtmp&patharr(i)&"/"
cpath=Left(pathtmp,Len(pathtmp)-1)
IfNotFileObject.FolderExists(cpath)ThenFileObject.CreateFoldercpath
Next
SetFileObject=Nothing
IfErr.Number0Then
CreateDIR=False
Err.Clear
Else
CreateDIR=True
EndIf
EndFunction
下面列舉一下這些不常用但是卻非常酷的功能:
很少被了解的FSO功能
GetSpecialFolderMethod返回特定的Windows文件夾的路徑:Windows安裝目錄;Windows系統目錄;Windows臨時目錄FSO.GetSpecialFolder([0,1,or2])
GetTempNameMethod返回一個隨機產生的文件或者目錄名字,用于需要存儲臨時數據時
GetAbsolutePathNameMethod返回文件夾的絕對路徑(類似于Server.MapPath)。
比如,FSO.GetAbsolutePathName("region")將返回類似于下面的結果:"c:mydocsmyfolderegion"
GetExtensionNameMethod返回路徑中最后部分的擴展名
(比如:FSO.GetExtensionName("c:docsest.txt")將返回txt)
GetBaseNameandGetParentFolderMethods返回路徑中最后部分的父文件夾
(比如:FSO.GetParentFolder("c:docsmydocs")將返回'docs')
DrivesProperty返回所有本地可用驅動器的集合,用于建立資源瀏覽器樣的用戶接口。
使用上面的功能時,最好建立好出錯處理的代碼。因為如果需要的參數不存在,將會產生麻煩的信息。
以上就是關于FSO代碼詳細解析了,想必都了解了吧,更多相關內容請繼續關注錯新技術頻道。
新聞熱點
疑難解答