FSO means FileSystemObject, which is the file system object. The FSO object model is included in the Scripting type library (Scrrun.Dll). It also contains five objects: Drive, Folder, File, FileSystemObject and TextStream. It is very convenient to operate files and folders. FSO file (File) object properties
DateCreated returns the creation date and time of the folder
DateLastAccessed returns the date and time the file was last accessed
DateLastModified returns the date and time the file was last modified
Drive returns the Drive object of the drive where the file is located.
Name sets or returns the name of the file
ParentFolder returns the Folder object of the file's parent folder
Path returns the absolute path of the file, long file names can be used
ShortName returns the DOS-style 8.3 form of the file name.
ShortPath returns a DOS-style 8.3-style absolute path to a file
Size returns the size of the file (bytes)
Type If possible, returns a string describing the file type.
FSO file (File) object methods
FSO file object method usage
CopyFile copies one or more files to a new path
CreateTextFile creates a file and returns a TextStream object
DeleteFile deletes a file
OpenTextFile opens a file and returns a TextStream object for reading or appending
Rename the file:
Copy the code code as follows:
Function reName(sourceName,destName)
dim oFso,oFile
set oFso=server.createobject(Scripting.FileSystemObject)
set oFile=oFso.getFile(Server.mappath(sourceName))
oFile.Name=destName
Set oFso=Nothing
Set oFile=Nothing
End Function
Delete files:
Copy the code code as follows:
Function FSOdel(fileName)
dim fso,f
set fso = server.CreateObject(scripting.filesystemobject)
f=server.MapPath(fileName)
if fso.FileExists(f) then
fso.DeleteFile f,true
end if
set f = nothing
set fso = nothing
End Function
Replace a string in a file:
Copy the code code 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.Write FiletempData
objCountFile.Close
Set objCountFile=Nothing
Set objFSO = Nothing
End Function
<%
'************************************************ ****
'Function name: CreateFolder(sPath)
'Function: Create directory
'Parameter: sPath: relative directory path created
'Return value: true on success, false on failure
'************************************************ ****
'response.Write createfolder(/dgsunshine/UploadFile/demo1/)
Function CreateFolder(sPath)
On Error Resume Next
Dim Fso,Arrfolder,Folder,i,j
If sPath= 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(sPath,Len(sPath)-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.folderExists(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 file
'Parameter: paramFileContent 'The content of the file
' paramFilePath 'File name (excluding path)
'Return value: true on success, false on failure
'************************************************ ****
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 separated by |
'Return value: true on success, false on failure
'************************************************ ****
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.FileExists(Server.MapPath(FilePath)) 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 directory
'Parameter: FolderPath 'Directory path' Multiple directories are separated by |
'Return value: true on success, false on failure
'************************************************ ****
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.folderexists(Server.MapPath(FolderPath)) then
Fso.deleteFolder Server.MapPath(FolderPath)
End If
Next
If Err then
Err.clear()
DelFolder = false
'ShowError Failed to delete directory,
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 separated by |
'Return value: true on success, false on failure
'************************************************ ****
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.FileExists(Server.MapPath(FilePath)) then
IsExistFile = True
End If
If Fso.folderexists(Server.MapPath(FilePath)) then
IsExistFile = True
End If
Next
Set fso = nothing
If Err then
Err.clear()
IsExistFile = false
'ShowError Failed to determine whether the file or directory exists,
else
IsExistFile = true
End If
End Function
'************************************************ ****
'Function name: DelFile(FilePath)
'Function: delete files or directories
'Parameter: FilePath 'File path multiple files separated by |
'Return value: true on success, false on failure
'************************************************ ****
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.FileExists(Server.MapPath(FilePath)) then
Fso.DeleteFile Server.MapPath(FilePath)
End If
If Fso.folderexists(Server.MapPath(FilePath)) then
Fso.deleteFolder Server.MapPath(FilePath)
End If
Next
Set fso = nothing
If Err then
Err.clear()
DelFile = false
'ShowError Deletion of file or directory failed,
else
DelFile = true
End If
End Function
'************************************************ ****
'Function name: ReNameFile((oldName,newName)
'Function: Rename files or directories
'Parameter: strOldName 'Multiple original file names are separated by |
' strNewName ' Multiple new file names are separated by |
' Please keep the above two parameters consistent
'Return value: true on success, false on failure
'************************************************ ****
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
arrOld = 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(arrOld)
oldName = Server.MapPath(arrOld(i))
newName = Server.MapPath(arrNew(i))
If Fso.FileExists(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)
'Function: Copy files or directories
'Parameter: TempSource 'Multiple source file names are separated by |
'TempEnd' Multiple destination file names are separated by |
' Note: Please keep the above two parameters consistent and both are full paths.
'Has been processed by Server.MapPath method
'Return value: true on success, false on failure
'************************************************ ****
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 files or directories, the condition is empty
CopyFiles = false
Exit Function
End If
arrSource = Split(TempSource,|)
arrEnd = Split(TempEnd,|)
If UBound(arrSource) <> UBound(arrEnd) then
CopyFiles= false
Exit Function
End If
for i=0 to UBound(arrSource)
srcName = arrSource(i)
tarName = arrEnd(i)
IF CopyFSO.FileExists(srcName) and not CopyFSO.FileExists(tarName) then
CopyFSO.CopyFile srcName,tarName
CopyFiles = true
End If
IF CopyFSO.FolderExists(srcName) and not CopyFSO.FolderExists(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
%>