Functions to modify file-specific content using the FSO
function Fsochange (filename,target,string)
Dim Objfso,objcountfile,filetempdata
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set objcountfile = objFSO.OpenTextFile (Server.MapPath (filename), 1,true)
Filetempdata = Objcountfile.readall
Objcountfile.close
Filetempdata=replace (filetempdata,target,string)
Set objcountfile=objfso.createtextfile (Server.MapPath (filename), True)
Objcountfile.write Filetempdata
Objcountfile.close
Set objcountfile=nothing
Set objFSO = Nothing
End Function
Functions to read the contents of a file using the FSO
function fsofileread (filename)
Dim Objfso,objcountfile,filetempdata
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set objcountfile = objFSO.OpenTextFile (Server.MapPath (filename), 1,true)
Fsofileread = Objcountfile.readall
Objcountfile.close
Set objcountfile=nothing
Set objFSO = Nothing
End Function
function to read a row of a file using the FSO
function Fsolinedit (filename,linenum)
If LineNum < 1 then Exit function
Dim fso,f,temparray,tempcnt
Set fso = server. CreateObject ("Scripting.FileSystemObject")
If not fso.fileexists (Server.MapPath (filename)) then Exit function
Set F = fso.opentextfile (Server.MapPath (filename), 1)
If not F.atendofstream then
tempcnt = F.readall
F.close
Set F = Nothing
Temparray = Split (TEMPCNT,CHR) &CHR (10))
If Linenum>ubound (temparray) +1 Then
Exit function
Else
Fsolinedit = Temparray (lineNum-1)
End If
End If
End Function
Functions that use the FSO to write a row of a file
function Fsolinewrite (filename,linenum,linecontent)
If LineNum < 1 then Exit function
Dim fso,f,temparray,tempcnt
Set fso = server. CreateObject ("Scripting.FileSystemObject")
If not fso.fileexists (Server.MapPath (filename)) then Exit function
Set F = fso.opentextfile (Server.MapPath (filename), 1)
If not F.atendofstream then
tempcnt = F.readall
F.close
Temparray = Split (TEMPCNT,CHR) &CHR (10))
If Linenum>ubound (temparray) +1 Then
Exit function
Else
Temparray (lineNum-1) = Linecontent
End If
tempcnt = Join (TEMPARRAY,CHR) &CHR (10))
Set F = fso.createtextfile (Server.MapPath (filename), true)
F.write tempcnt
End If
F.close
Set F = Nothing
End Function
function to add a new row of files using the FSO
function Fsoappline (filename,linecontent)
Dim fso,f
Set fso = server. CreateObject ("Scripting.FileSystemObject")
If not fso.fileexists (Server.MapPath (filename)) then Exit function
Set F = fso.opentextfile (Server.MapPath (filename), 8, 1)
F.write chr (&CHR) &linecontent
F.close
Set F = Nothing
End Function
function to read the last line of a file
function fsolastline (filename)
Dim fso,f,temparray,tempcnt
Set fso = server. CreateObject ("Scripting.FileSystemObject")
If not fso.fileexists (Server.MapPath (filename)) then Exit function
Set F = fso.opentextfile (Server.MapPath (filename), 1)
If not F.atendofstream then
tempcnt = F.readall
F.close
Set F = Nothing
Temparray = Split (TEMPCNT,CHR) &CHR (10))
Fsolastline = Temparray (UBound (Temparray))
End If
End Function
FSO replaces characters for the specified file
Program code:
' FSO replaces the character of the specified file
Function Fsolineedit (filename,target,string)
Dim Objfso,objcountfile,filetempdata
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set objcountfile = objFSO.OpenTextFile (Server.MapPath (filename), 1,true)
Filetempdata = Objcountfile.readall
Objcountfile.close
Filetempdata = Replace (filetempdata,target,string)
Set objcountfile = objFSO.CreateTextFile (Server.MapPath (filename), True)
Objcountfile.write Filetempdata
Objcountfile.close
Set Objcountfile = Nothing
Set objFSO = Nothing
End Function
' Response.Write Fsolineedit ("Test.txt", "The World", "Tomorrow is a good day to go")
deleting files
Program code:
' Delete file
Function Delfile (Filename)
If Filename <> "" Then
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
If FSO. FileExists (Filename) Then
Fso. DeleteFile Filename
End If
Set FSO = Nothing
End If
End Function
To determine whether a file exists
Program code:
' To determine if a file exists
Function Reportfilestatus (filespec)
Dim fso,msg
Set FSO = CreateObject ("Scripting.FileSystemObject")
If (FSO). FileExists (filespec)) Then
msg = filespec & "exists."
Else
msg = filespec & "doesn ' t exist."
End If
Reportfilestatus = Msg
End Function
Functions to modify file-specific content using the FSO
Program code:
' Functions to modify file-specific content using the FSO
Function Fsochange (filename,target,string)
Dim Objfso,objcountfile,filetempdata
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set objcountfile = objFSO.OpenTextFile (Server.MapPath (filename), 1,true)
Filetempdata = Objcountfile.readall
Objcountfile.close
Filetempdata = Replace (filetempdata,target,string)
Set objcountfile = objFSO.CreateTextFile (Server.MapPath (filename), True)
Objcountfile.write Filetempdata
Objcountfile.close
Set Objcountfile = Nothing
Set objFSO = Nothing
End Function
Functions that use the FSO to write a row of a file
Program code:
' function to write a row of a file using the FSO
Function Fsolinewrite (filename,linenum,linecontent)
If LineNum < 1 Then Exit Function
Dim fso,f,temparray,tempcnt
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
If not FSO. FileExists (Server.MapPath (filename)) Then Exit Function
Set f = FSO. OpenTextFile (Server.MapPath (filename), 1)
If not F.atendofstream Then
tempcnt = F.readall
F.close
Temparray = Split (TEMPCNT,CHR) &CHR (10))
If Linenum>ubound (temparray) +1 Then
Exit Function
Else
Temparray (lineNum-1) = Linecontent
End If
tempcnt = jo& #105 n (TEMPARRAY,CHR) &CHR (10))
Set f = fso.cr& #101; atetextfile (Server.MapPath (filename), true)
F.write tempcnt
End If
F.close
Set F = Nothing
End Function
A directory-building program, if there is a multilevel directory, then a first-level creation
Program code:
' Directory-building program, if there is a multilevel directory, then create at one level
Function Createdir (ByVal localpath)
On Error Resume Next
LocalPath = Replace (LocalPath, "\", "/")
Set FileObject = Server.CreateObject ("Scripting.FileSystemObject")
Patharr = Split (LocalPath, "/")
Path_level = UBound (Patharr)
For i = 0 to Path_level
If i=0 Then Pathtmp=patharr (0) & "/" Else pathtmp = pathtmp & Patharr (i) & "/"
CPath = Left (Pathtmp,len (pathtmp)-1)
If not fileobject.folderexists (cpath) Then Fileobject.createfolder CPath
Next
Set FileObject = Nothing
If err.number <> 0 Then
Createdir = False
Err.Clear
Else
Createdir = True
End If
End Function
Here are some of the less common but very cool features:
FSO features that are rarely understood
GetSpecialFolder method returns the path to a specific Windows folder: Windows installation directory; Windows system directory; Windows Temp directory FSO. GetSpecialFolder ([0, 1, or 2])
GetTempName method returns a randomly generated file or directory name that is used to store temporary data
Getabsolutepathname method returns the absolute path of the folder (similar to Server.MapPath).
For example, FSO. Getabsolutepathname ("region") returns a result similar to the following: "C:mydocsmyfolder egion"
Getextensionname method returns the extension of the last part of the path
(For example: Fso.getextensionname ("C:docs est.txt") will return txt)
Getbasename and Getparentfolder Methods returns the parent folder of the last part of the path
(For example: Fso.getparentfolder ("C:docsmydocs") will return to ' docs ')
The Drives property returns a collection of all locally available drives to establish a resource-browser-like user interface.
When using the above features, it is best to create a good error-handling code. Because if the required parameters do not exist, you will have trouble with the information.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.