Use FSO to modify file-specific contents

xiaoxiao2021-03-06  41

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

'' 'Uses FSO to read the function of the file content

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

'' 'Uses FSO to read the function of a line of files

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

'' 'Uses FSO to write a function of a certain line

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 = JOIN (Temparray, Chr (13) & chr (10))

Set f = fso.createtextfile (server.mappath (filename), true)

F.Write Tempcnt

END IF

f.close

Set f = Nothing

END FUNCTION

'' 'Uses FSO to add files to new line functions

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

'' '' Read the last line 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 (TempaRay))

END IF

END FUNCTION

'Also, create a folder:

SUB CREATEFOLDER (FolderName)

SET AFSO = Server.createObject ("scripting.filesystemObject")

IF AFSO.FOLDEREXISTS (Server.Mappath (FolderName)) = true kil

Else

AFSO.CREATEFOLDER (server.mappath (foldername))

END IF

Set Afso = Nothing Sub

'Usage, CreateFolder (FolderName)

Traversal Directory and the function of the files under the directory

Function Bianli (PATH)

SET FSO = Server.createObject ("scripting.filesystemObject")

ON Error ResMe next

Set objfolder = fso.getfolder (PATH)

Set objsubfolders = Objfolder.subfolders

For Each Objsubfolder In Objsubfolders

NowPath = PATH "/" Objsubfolder.name

Response.write nowpath

Set Objfiles = Objsubfolder.files

For Each Objfile in Objfiles

Response.write "
---"

Response.write objfile.name

NEXT

Response.write "

"

Bianli (nowPath) 'recursive

NEXT

Set objfolder = Nothing

Set objsubfolders = Nothing

SET FSO = Nothing

END FUNCTION

%>

<%

'Bianli ("D:")' Traversed D: Disc

%>

<%

'Replace the function of specifying the string of the file

Function FsolineEd (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")

%>

Keep the value in the refresh text box