Let's introduce the FSO file operation function in ASP. He can implement copy files, renamed files, delete files, and replace files.
FSO file (File) object attribute
DateCreated returns the creation date and time of the folder
DatelaastaccessEd returns the date and time of the last visit to the file
Datelaastmodify returns the date and time of the last modification of the file
Drive returns the Drive object of the drive where the file is located
Name settings or return file names
Parentfolder returns the Folder object of the parent folder of the file
PATH returns the absolute path of the file, you can use the long file name
Shortname returns the DOS -style 8.3 form of file name
Shortpath returns DOS -style 8.3 form of file absolute path
SIZE returns the size of the file (byte)
If Type may, return a file type instruction string
FSO file (File) object method
FSO file object method use
Copyfile copy one or more files to the new path
CreatetextFile creates a file and returns a TextStream object
Deletefile delete a file
OpenTextFile opens the file and returns the TextStream object to read or add
Rena name file:
The code is as follows:
Function rename (SourceName, Destname)
dim ofso, ofile
set offso = server.createObject (scripting.filesystemObject)
set ofile = Offso.getfile (server.mappath (source)))
ofile.name = Destname
Set offso = nothing
Set ofile = nothing
End function
Delete files:
The code is as follows:
Function FSODEL (FILENAME)
dim FSO, F
set fso = server.createObject (scripting.filesystemObject)
F = Server.mappath (FILENAME)
if fso.fileexist (f) then
FSO.Deletefile F, TRUE
end if
set f = Nothing
set fSO = Nothing
End function
Change the string in the file:
The code is as follows:
Function FSOREPLACE (FILENAME, TARGET, Repstring)
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, Repstring)
Set objcountfile = Objfso.CreatetextFile (server.mappath (filename), true)
objcountfile.writ fileTempdata
objcountfile.close
Set objcountfile = Nothing
Set objfso = nothing
End function
<%
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: CreateFolder (spAd)
'Function: Create a directory
'Parameters: Spath: The relative directory path created
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Response.write createfolder (/dgsunshine/uploadFile/Demo1/)
Function Createfolder (Spi)
On error resume next
Dim FSO, Arrfolder, Folder, I, J
If space = then
Createfolder = false
Exit function
End if
If left (spath, 1) = / then
Folder = /
Spath = MID (Spath, 2, Len (SpATH)))
Else
Folder = ./
End if
if right (spath, 1) = / then spath = left (spAd, len (spAd) -1)
Arrfolder = split (spath,/)
Set fso = server.createObject (scripting.filesystemObject)
For i = 0 to ubound (Arrfolder)
If i = 0 THEN
Folder = Folder & Arrfolder (i) & /
Else
Folder = Folder & Arrfolder (i) & /
End if
If fso.foldrexists (server.mappath (folder)) = false then
response.write server.mappath (folder)
Fso.createfolder (server.mappath (folder))
End if
Next
Set fSO = Nothing
If err.number <> 0 then
Err.clear ()
Createfolder = false
Else
Createfolder = true
End if
End function
Function getfile (paramfilepath)
Set fso = server.createObject (scripting.filesystemObject)
Set fSO_READ = fso.OpenTextFile (server.mappath (paramfilepath), 1, FALSE, -2)
getfile = fso_read.readall
Set fSO_READ = Nothing
Set fSO = Nothing
End function
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: Createfile (ParamfileContent, ParamfilePath)
'Function: Create a file
'Parameter: ParamfileContent' file content
'ParamfilePath' file name (excluding path)
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
Function Createfile (ParamfileContent, ParamfilePath)
On error resume next
Dim fso, fwrite
Set fso = server.createObject (scripting.filesystemObject)
Set fwrite = fso.createtextFile (server.mappath (paramfilepath), true)
fwrite.Write ParamfileContent
fwrite.close ()
Set fwrite = nothing
Set fSO = Nothing
If err.number <> 0 then
Err.clear ()
Createfile = false
Else
Createfile = true
End if
End function
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: Delfile (FilePath)
'Function: Delete files
'Parameter: FilePath' file path Multiple files for multiple files | separate
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
Function Delfile (FilePath)
On error resume next
Dim FSO, Arrfile, I
If GETSAFESTR (FilePath,) = THEN
Createfolder = false
Exit function
End if
Arrfile = split (filepath, | |)
Set fso = server.createObject (scripting.filesystemObject)
for i = 0 to ubound (Arrfile)
Filepath = Arrfile (i)
If fso.fileexist (server.mappath (filepath) then) then
FSO.Deletefile Server.MAPPATH (FilePath)
End if
Next
Set fSO = Nothing
If err then
Err.clear ()
Delfile = false
Else
Delfile = TRUE
End if
End function
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: Delfolder (FOLDERPATH)
'Function: Delete the directory
'Parameter: FolderPath' Directory Paths' Multi -Directory Elimination | Separation
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
Function Delfolder (FOLDERPATH)
On error resume next
DIM FSO, Arrfolder, I
If GETSAFESTR (FolderPath,) = then
Delfolder = false
Exit function
End if
Arrfolder = Split (FolderPath, |)
Set fso = server.createObject (scripting.filesystemObject)
For i = 0 to ubound (Arrfolder)
Folderpath = Arrfolder (i)
If fso.foldrexists (server.mappath (folderpath) then) then
FSO.Deletefolder Server.mappath (FolderPath)
End if
Next
If err then
Err.clear ()
Delfolder = false
'Showerror delete the directory failed,
else
Delfolder = TRUE
End if
End function
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: ISEXISTFILE (FilePath)
'Function: Determine whether the file or directory exists
'Parameter: FilePath' file path Multiple files for multiple files | separate
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
Function ISEXISTFILE (FilePath)
On error resume next
Dim FSO, Arrfile, I
If GETSAFESTR (FilePath,) = THEN
Isexistfile = false
End if
Arrfile = split (filepath, | |)
Set fso = server.createObject (scripting.filesystemObject)
for i = 0 to ubound (Arrfile)
Filepath = Arrfile (i)
If fso.fileexist (server.mappath (filepath) then) then
IsexistFile = TRUE
End if
If fso.foldrexist (server.mappath (filepath)) then
IsexistFile = TRUE
End if
Next
Set fSO = Nothing
If err then
Err.clear ()
Isexistfile = false
'Showerror judging whether the file or directory fails,
else
IsexistFile = TRUE
End if
End function
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: Delfile (FilePath)
'Function: Delete files or directory
'Parameter: FilePath' file path Multiple files for multiple files | separate
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
Function Delfile (FilePath)
On error resume next
Dim FSO, Arrfile, I
If GETSAFESTR (FilePath,) = THEN
Createfolder = false
End if
Arrfile = split (filepath, | |)
Set fso = server.createObject (scripting.filesystemObject)
for i = 0 to ubound (Arrfile)
Filepath = Arrfile (i)
If fso.fileexist (server.mappath (filepath) then) then
FSO.Deletefile Server.MAPPATH (FilePath)
End if
If fso.foldrexist (server.mappath (filepath)) then
FSO.Deletefolder Server.mappath (FilePath)
End if
Next
Set fSO = Nothing
If err then
Err.clear ()
Delfile = false
'Showerror delete file or directory failed,
else
Delfile = TRUE
End if
End function
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: Renamefile (OldName, NewName)
'Function: Rename file or directory
'Parameter: Stroldname' Original file name multi -use | separate
'STRNEWNAME' New File Name multi -use | separate
'Please keep the same two parameters consistent
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
Function renamefile (StRoldName, Strnewname)
On error resume next
Dim FSO, Arrold, ArrNew, I, OldName, NewName
OLD = GetsaFestr (Stroldname,)
Newfile = GetsaFestr (Strnewname,)
If OLD = Or newfile = then
Renamefile = false
Exit function
End if
arold = Split (Stroldname, |)
arrNew = Split (Strnewname, |)
If ubound (Arrold) <> ubound (ArrNew) then
Renamefile = false
Exit function
End if
Set fso = server.createObject (scripting.filesystemObject)
for i = 0 to ubound (Arrow)
OldName = Server.mappath (Arrold (I))
newName = Server.mappath (ArrNew (I))
If fso.fileexist (OldName) and not fso.fileexists (newName) then
fso.movefile oldName, newName
'Renamefile = TRUE
End if
Next
Set fSO = Nothing
If err.number <> 0 then
Err.clear ()
Renamefile = false
Else
Renamefile = true
End if
End function
'***********************************************************************************************************************************************************************************************************************************************************************, ******
'Function name: Copyfiles (Tempsource, Tempend)
'File: Copy file or directory
'Parameter: Tempsource' Source file name Multi -purpose | separate
'Tempend' Destable file name multi -purpose | separate
'Note: Please keep the same two parameters above, and both are the complete path,
'The server.mappath method has been processed
'Return value: Success TRUE, Fail False
'***********************************************************************************************************************************************************************************************************************************************************************, ******
Function Copyfiles (Tempsource, Tempend)
On error resume next
DIM COPYFSO, ARRSOURCE, Arrend
Copyfiles = false
Set copyfso = server.createObject (scripting.filesystemObject)
If tempSource = or tempend = then
Errraise copy file or directory, the condition is empty
Copyfiles = false
Exit function
End if
Arrsource = Split (Tempsource, |)
Arrend = Split (Tempend, |)
If ubound (arrsource) <> ubound (arbit) then
Copyfiles = false
Exit function
End if
for i = 0 to ubound (arrsource)
srcname = Arrsource (i)
tarName = Arrend (i)
If Copyfso.filexists (srcname) and not Copyfso.fileexist (tarname) then
Copyfso.copyfile srcname, tarname
Copyfiles = TRUE
End if
If Copyfso.FoldRexist (srcname) and not Copyfso.foldrexist (tarname) then
Copyfso.copyfolder srcname, tarname
Copyfiles = TRUE
End if
Next
Set Copyfso = Nothing
If err then
'Err.clear ()
Copyfiles = false
End if
End function
%>