The FSO components in ASP are very powerful. If you don’t have this function, you don’t know what ASP will become. In fact, friends who want to learn ASP programming will definitely be exposed to FSO-related operations. The following new technology channel will bring you a complete collection of ASP FSO file processing functions.
The code copy is as follows:<%
'Create a folder function
Function CreateFolder(strFolder)' parameter is a relative path
'The first choice is to determine whether the folder to be created already exists
Dim strTestFolder,objFSO
strTestFolder = Server.Mappath(strFolder)
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Check if the folder exists
If not objFSO.FolderExists(strTestFolder) Then
'If it does not exist, create a folder
objFSO.CreateFolder(strTestFolder)
End If
Set objFSO = Nothing
End function
'Delete the folder
Function DelFolder(strFolder)' parameter is relative path
strTestFolder = Server.Mappath(strFolder)
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Check if the folder exists
If objFSO.FolderExists(strTestFolder) Then
objFSO.DeleteFolder(strTestFolder)
end if
Set objFSO = Nothing
End function
'Create a text file
Function Createtextfile(fileurl,filecontent)' parameter is the relative path and the content to be written to the file
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set fout = objFSO.CreateTextFile(Server.MapPath(fileurl))
fout.WriteLine filecontent
fout.close
Set objFSO = Nothing
End Function
'Delete files (suitable for all files)
Function Deltextfile(fileurl)' parameter is relative path
Set objFSO = CreateObject("Scripting.FileSystemObject")
fileurl = Server.MapPath(fileurl)
if objFSO.FileExists(fileurl) then 'Check whether the file exists
objFSO.DeleteFile(Server.mappath(fileurl))
end if
Set objFSO = nothing
End Function
'Create image files and save image data stream
Function Createimage(fileurl,imagecontent)' parameter is relative path and file content
Set objStream = Server.CreateObject("ADODB.Stream") 'To create ADODB.Stream object, ADO version 2.5 or above must be created
objStream.Type =1 'Open in binary mode
objStream.Open
objstream.write imagecontent 'Write string content to buffer
objstream.SaveToFile server.mappath(fileurl),2 '-Write buffered content to file
objstream.Close()'Close object
set objstream=nothing
End Function
'Remotely obtain file data
Function getHTTPPage(url)
'On Error Resume Next
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
If Err.number<>0 then
getHTTPPage = "The server got file content error"
Err.Clear
End If
End function
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
'Get picture data stream
Function getpic(url)
on error resume next
dim http
set http=server.createobject("MSXML2.XMLHTTP")'Use xmlhttp method to get the content of the image
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getpic=Http.responseBody
set http=nothing
if err.number<>0 then
getpic = "The server got error in getting the file content"
err.Clear
End if
End Function
'Open file (text form)
Function OpenFile(fileurl)' file relative path
Dim Filename,fso,hndFile
Filename = fileurl
Filename = Server.MapPath(Filename)
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.FileExists(Filename) Then
set hndFile = objfso.OpenTextFile(Filename)
OpenFile = hndFile.ReadAll
Else
OpenFile = "File Read Error"
End If
Set hndFile = Nothing
Set objfso = Nothing
End Function
'Get the file's suffix name
function getFileExtName(fileName)
dim pos
pos=instrrev(filename,".")
if pos>0 then
getFileExtName=mid(fileName,pos+1)
else
getFileExtName=""
end if
end function
%>
The above is a complete collection of ASP FSO file processing functions introduced by the editor of Foxin Technology Channel. I believe everyone has a certain understanding. Foxin Technology Channel will share more knowledge with you, making you better and better in this industry.