Share the FSO code

Source: Internet
Author: User

Switch from ---> big it _ rest place -- Huo Jianwei

They are high people

 

Some FSO code added to the favorites will be shared.
Good codes will be added one after another.

Many people have asked questions about the dynamic include file.

Program code 'uses FSO to dynamically call a file
Function include (filename)
Dim re, content, FSO, F, aspstart, aspend
Set FSO = Createobject ("scripting. FileSystemObject ")
Set F = FSO. opentextfile (server. mappath (filename ))
Content = f. readall
F. Close
Set = nothing
Set FSO = nothing
Set Re = new Regexp
Re. pattern = "/S * ="
Aspend = 1
Aspstart = instr (aspend, content, "<%") + 2
Do While aspstart> aspend + 1
Response. Write mid (content, aspend, aspStart-aspEnd-2)
Aspend = instr (aspstart, content, "%/>") + 2
Execute (Re. Replace (mid (content, aspstart, aspEnd-aspStart-2), "response. Write "))
Aspstart = instr (aspend, content, "<%") + 2
Loop
Response. Write mid (content, aspend)
Set Re = nothing
End Function

Delete file 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

Determine whether the file exists program code 'to determine whether the 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

Use FSO to modify the function program code of a specific file 'use FSO to modify the function of a specific File Content
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

Use FSO to write a row of Function Code 'Use FSO to write a row of Function
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 (13) & CHR (10 ))
If linenum> ubound (temparray) + 1 then
Exit Function
Else
Temparray (lineNum-1) = linecontent
End if
Tempcnt = Jo & #105; n (temparray, CHR (13) & CHR (10 ))
Set F = FSO. CR & #101; atetextfile (server. mappath (filename), true)
F. Write tempcnt
End if
F. Close
Set F = nothing
End Function

FSO replaces the character program code of the specified file. '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", "world", "Tomorrow is a good day ")

Function program code for reading file content using FSO 'function for reading file content using 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 program code for reading a row of a file using FSO 'Use FSO to read a function of a row of the file
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 (13) & CHR (10 ))
If linenum> ubound (temparray) + 1 then
Exit Function
Else
Fsolinedit = temparray (lineNum-1)
End if
End if
End Function

The function program code in the last row of the Read File 'reads the function in the last row of the 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 (13) & CHR (10 ))
Fsolastline = temparray (ubound (temparray ))
End if
End Function

Function program code for adding a new file line using FSO 'Use FSO to add a new file line function
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 (13) & CHR (10) & linecontent
F. Close
Set F = nothing
End Function

Create a directory program. If there is a multi-level directory, create a program code at the first level to create a directory program. If there is a multi-level directory, create a program at the first 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

Copy all files in a directory to the specified directory program code. Copy all files in a directory to the specified directory.
Set FSO = Createobject ("scripting. FileSystemObject ")
Set cur_folder = FSO. getfolder (server. mappath (sourcepath) ''source folder
Distination_path = "C:/test"
Set sub_files = cur_folder.files
For each each_file in sub_files
Sub_file_path = each_file.name
FSO. copyfile server. mappath (sourcepath) & "/" & sub_file_path, distination_path & "/" & sub_file_path, true
Next

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.