FSO is a control for file operations in Microsoft ASP. So, the editor of the wrong new channel will share with you a detailed analysis of the FSO code.
Functions that modify file-specific content using 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
Functions that use FSO to read file content
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
Functions that use FSO to read a line of a file
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
template=split(tempcnt,chr(13)&chr(10))
iflineNum>ubund(temparray)+1then
exitfunction
else
FSOlinedit=temparray(lineNum-1)
endif
endif
endfunction
Functions that use FSO to write a certain line of a file
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
template=split(tempcnt,chr(13)&chr(10))
iflineNum>ubund(temparray)+1then
exitfunction
else
template(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
Functions to add new lines of a file using 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
Functions that read the last line of the file
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
template=split(tempcnt,chr(13)&chr(10))
FSOlastline=temparray(ubund(temparray))
endif
endfunction
FSO replaces the character of the specified file
Program code:
'FSO replaces the character of the specified file
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","world","Tomorrow is a good day to go")
Delete files
Program code:
'Delete the file
FunctionDelFile(Filename)
IfFilename""Then
SetFSO=Server.CreateObject("Scripting.FileSystemObject")
IfFSO.FileExists(Filename)Then
FSO.DeleteFileFilename
EndIf
SetFSO=Nothing
EndIf
EndFunction
Determine whether the file exists
Program code:
'Just determine whether the file exists
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
Functions that modify file-specific content using FSO
Program code:
'Use FSO to modify file-specific content functions
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
Functions that use FSO to write a certain line of a file
Program code:
'Use FSO to write a function that has a certain line of a file
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
template(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
The program that creates a directory. If there are multiple levels of directories, create one level at a time.
Program code:
'The program that creates a directory. If there are multiple levels of directories, creates it one level at a time.
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
Here are some of these incommon but very cool features:
FSO features that are rarely understood
GetSpecialFolderMethod returns the path of a specific Windows folder: Windows installation directory; Windows system directory; Windows temporary directory FSO.GetSpecialFolder([0,1,or2])
GetTempNameMethod returns a randomly generated file or directory name, which is used when temporary data is required.
GetAbsolutePathNameMethod returns the absolute path to the folder (similar to Server.MapPath).
For example, FSO.GetAbsolutePathName("region") will return a result similar to the following: "c:mydocsmyfolderegion"
GetExtensionNameMethod returns the extension of the last part of the path
(For example: FSO.GetExtensionName("c:docsest.txt") will return txt)
GetBaseNameandGetParentFolderMethods returns the parent folder of the last part of the path
(For example: FSO.GetParentFolder("c:docsmydocs") will return 'docs')
DrivesProperty returns a collection of all locally available drives for building resource browser-like user interfaces.
When using the above functions, it is best to create a code for error handling. Because if the required parameters do not exist, troublesome information will be generated.
The above is a detailed analysis of the FSO code. I believe you have learned it all. For more related content, please continue to pay attention to the Wuxin Technology Channel.