<p>FSO文件(File)对象属性<br />
DateCreated 返回该文件夹的创建<a href="http://www.111cn.net/tags.php/日期和时间">日期和时间</a><br />
DateLastAccessed 返回最后一次访问该文件的日期和时间<br />
DateLastModified 返回最后一次修改该文件的日期和时间<br />
Drive 返回该文件所在的驱动器的Drive对象<br />
Name 设定或返回文件的名字<br />
ParentFolder 返回该文件的父文件夹的Folder对象<br />
Path 返回文件的绝对路径,可使用长文件名<br />
ShortName 返回DOS风格的8.3形式的文件名<br />
ShortPath 返回DOS风格的8.3形式的文件绝对路径<br />
Size 返回该文件的大小(字节)<br />
Type 如果可能,返回一个文件类型的说明字符串<br />
FSO文件(File)对象方法<br />
FSO文件对象方法 用途<br />
CopyFile 拷贝一个或者多个文件到新路径<br />
CreateTextFile 创建文件并且返回一个TextStream对象<br />
DeleteFile 删除一个文件<br />
OpenTextFile 打开文件并且返回TextStream对象,以便读取或者追加</p>
<p></p>
<blockquote>
<p><%<br />
'================================================================<br />
'= 系统版本:1.0 =<br />
'= 文件版本:1.0 =<br />
'= 文本创建日期:2005-10-31 =<br />
'================================================================<br />
Class Cls_IO<br />
Private FileSystemObject</p>
<p>Private Sub Class_Terminate()<br />
If IsObject(FileSystemObject) Then<br />
Set FileSystemObject = Nothing<br />
End If<br />
End Sub</p>
<p>'//创建FileSystemObject对象<br />
Public Function Init_Object()<br />
On Error Resume Next<br />
If Not IsObject(FileSystemObject) Then<br />
Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")<br />
If Err.Number <> 0 Then<br />
Init_Object = False<br />
Err.Clear<br />
Exit Function<br />
Else<br />
Init_Object = True<br />
End If<br />
End If<br />
End Function</p>
<p>Public Sub DeleteFile(ByVal Str)<br />
On Error Resume Next<br />
Call FileSystemObject.DeleteFile(Server.MapPath("../UpLoadFile/"&Str))<br />
End Sub</p>
<p>Public Function IsFolder(ByVal Str)<br />
IsFolder = FileSystemObject.FolderExists(Str)<br />
End Function</p>
<p><br />
Public Function ShowFolder(ByVal Str)<br />
Dim Obj_Folder,Obj_File<br />
Set Obj_Folder = FileSystemObject.GetFolder(Server.MapPath(Str))<br />
Set ShowFolder = Obj_Folder.Files<br />
Set Obj_Folder = Nothing<br />
End Function</p>
<p>End Class<br />
%></p>
<p>重命名文件:<br />
复制代码 代码如下:<br />
Function reName(sourceName,destName)<br />
dim oFso,oFile<br />
set oFso=server.createobject("Scripting.FileSystemObject")<br />
set oFile=oFso.getFile(Server.mappath(sourceName))<br />
oFile.Name=destName<br />
Set oFso=Nothing<br />
Set oFile=Nothing<br />
End Function</p>
<p>删除文件:<br />
复制代码 代码如下:<br />
Function FSOdel(fileName)<br />
dim fso,f<br />
set fso = server.CreateObject("scripting.filesystemobject")<br />
f=server.MapPath(fileName)<br />
if fso.FileExists(f) then<br />
fso.DeleteFile f,true<br />
end if<br />
set f = nothing<br />
set fso = nothing<br />
End Function</p>
<p>替换文件中的字符串:<br />
复制代码 代码如下:<br />
Function FSOreplace(fileName,Target,repString)<br />
Dim objFSO,objCountFile,FiletempData<br />
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")<br />
Set objCountFile = objFSO.OpenTextFile(Server.MapPath(fileName),1,True)<br />
FiletempData = objCountFile.ReadAll<br />
objCountFile.Close<br />
FiletempData=Replace(FiletempData,Target,repString)<br />
Set objCountFile=objFSO.CreateTextFile(Server.MapPath(fileName),True)<br />
objCountFile.Write FiletempData<br />
objCountFile.Close<br />
Set objCountFile=Nothing<br />
Set objFSO = Nothing<br />
End Function</p>
<p><%</p>
<p>'*******************************************************<br />
'函数名:CreateFolder(sPath)<br />
'作 用: 创建目录<br />
'参 数:sPath : 创建的相对目录路径<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
'response.Write createfolder("/dgsunshine/UploadFile/demo1/")<br />
Function CreateFolder(sPath)<br />
On Error Resume Next</p>
<p>Dim Fso,Arrfolder,Folder,i,j</p>
<p>If sPath="" then<br />
CreateFolder = False<br />
Exit Function<br />
End If</p>
<p>If Left(sPath,1) = "/" Then<br />
Folder = "/"<br />
sPath = Mid(sPath,2,Len(sPath))<br />
Else<br />
Folder = "./"<br />
End If</p>
<p>if Right(sPath,1) = "/" then sPath = Left(sPath,Len(sPath)-1)</p>
<p>ArrFolder = Split(sPath,"/")</p>
<p>Set Fso = Server.CreateObject("Scripting.FileSystemObject")</p>
<p>For i = 0 To Ubound(ArrFolder)<br />
If i = 0 then<br />
Folder = Folder & ArrFolder(i) & "/"<br />
Else<br />
Folder = Folder & ArrFolder(i) & "/"<br />
End If</p>
<p>If Fso.folderExists(Server.MapPath(Folder)) = False then<br />
response.Write server.MapPath(folder)<br />
Fso.createFolder(Server.MapPath(Folder))<br />
End If</p>
<p>Next<br />
Set Fso = nothing</p>
<p>If Err.Number <> 0 then<br />
Err.clear()<br />
CreateFolder = False<br />
Else<br />
CreateFolder = True<br />
End If<br />
End function</p>
<p><br />
Function getFile(paramFilePath)<br />
Set Fso = Server.CreateObject("Scripting.FileSystemObject")<br />
Set Fso_Read = fso.OpenTextFile(Server.MapPath(paramFilePath),1,false,-2)<br />
getFile = Fso_Read.readall<br />
Set Fso_Read = Nothing<br />
Set Fso = Nothing<br />
End Function</p>
<p>'*******************************************************<br />
'函数名:CreateFile(paramFileContent,paramFilePath)<br />
'作 用: 创建文件<br />
'参 数:paramFileContent '文件的内容<br />
' paramFilePath '文件名(不包括路径)<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
Function CreateFile(paramFileContent,paramFilePath)<br />
On Error Resume Next<br />
Dim Fso,fWrite</p>
<p>Set Fso = Server.CreateObject("Scripting.FileSystemObject")<br />
Set fWrite = Fso.CreateTextFile(Server.Mappath(paramFilePath),true)</p>
<p>fWrite.write paramFileContent<br />
fWrite.close()<br />
Set fWrite = nothing<br />
Set Fso = nothing</p>
<p>If Err.number <> 0 Then<br />
Err.clear()<br />
CreateFile = False<br />
Else<br />
CreateFile = True<br />
End If<br />
End Function</p>
<p>'*******************************************************<br />
'函数名:DelFile(FilePath)<br />
'作 用: 删除文件<br />
'参 数:FilePath '文件路径 多个文件用"|"隔开<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
Function DelFile(FilePath)<br />
On Error Resume Next<br />
Dim fso,arrFile,i</p>
<p>If GetSafeStr(FilePath,"")="" then<br />
CreateFolder = false<br />
Exit Function<br />
End If</p>
<p>arrFile = Split(FilePath,"|")<br />
Set Fso = Server.CreateObject("Scripting.FileSystemObject")</p>
<p>for i=0 to UBound(arrFile)<br />
FilePath = arrFile(i)<br />
If Fso.FileExists(Server.MapPath(FilePath)) then<br />
Fso.DeleteFile Server.MapPath(FilePath)<br />
End If<br />
Next<br />
Set fso = nothing</p>
<p>If Err then<br />
Err.clear()<br />
DelFile = false<br />
Else<br />
DelFile = true<br />
End If<br />
End Function</p>
<p>'*******************************************************<br />
'函数名:DelFolder(FolderPath)<br />
'作 用: 删除目录<br />
'参 数:FolderPath '目录路径 '多个目录用"|"分隔<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
Function DelFolder(FolderPath)<br />
On Error Resume Next<br />
Dim Fso,arrFolder,i</p>
<p>If GetSafeStr(FolderPath,"")="" then<br />
DelFolder = false<br />
Exit Function<br />
End If</p>
<p>arrFolder = Split(FolderPath,"|")<br />
Set Fso = Server.CreateObject("Scripting.FileSystemObject")</p>
<p>For i=0 to UBound(arrFolder)<br />
FolderPath = arrFolder(i)<br />
If Fso.folderexists(Server.MapPath(FolderPath)) then<br />
Fso.deleteFolder Server.MapPath(FolderPath)<br />
End If<br />
Next</p>
<p>If Err then<br />
Err.clear()<br />
DelFolder = false<br />
'ShowError "删除目录失败",""<br />
else<br />
DelFolder = true<br />
End If<br />
End Function</p>
<p><br />
'*******************************************************<br />
'函数名:IsExistFile(FilePath)<br />
'作 用: 判断文件或目录是否存在<br />
'参 数:FilePath '文件路径 多个文件用"|"隔开<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
Function IsExistFile(FilePath)<br />
On Error Resume Next<br />
Dim fso,arrFile,i</p>
<p>If GetSafeStr(FilePath,"")="" then<br />
IsExistFile = false<br />
End If</p>
<p>arrFile = Split(FilePath,"|")<br />
Set Fso = Server.CreateObject("Scripting.FileSystemObject")</p>
<p>for i=0 to UBound(arrFile)<br />
FilePath = arrFile(i)<br />
If Fso.FileExists(Server.MapPath(FilePath)) then<br />
IsExistFile = True<br />
End If<br />
If Fso.folderexists(Server.MapPath(FilePath)) then<br />
IsExistFile = True<br />
End If<br />
Next<br />
Set fso = nothing</p>
<p>If Err then<br />
Err.clear()<br />
IsExistFile = false<br />
'ShowError "判断文件或目录是否存在失败",""<br />
else<br />
IsExistFile = true<br />
End If<br />
End Function</p>
<p><br />
'*******************************************************<br />
'函数名:DelFile(FilePath)<br />
'作 用: 删除文件或目录<br />
'参 数:FilePath '文件路径 多个文件用"|"隔开<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
Function DelFile(FilePath)<br />
On Error Resume Next<br />
Dim fso,arrFile,i</p>
<p>If GetSafeStr(FilePath,"")="" then<br />
CreateFolder = false<br />
End If</p>
<p>arrFile = Split(FilePath,"|")<br />
Set Fso = Server.CreateObject("Scripting.FileSystemObject")</p>
<p>for i=0 to UBound(arrFile)<br />
FilePath = arrFile(i)<br />
If Fso.FileExists(Server.MapPath(FilePath)) then<br />
Fso.DeleteFile Server.MapPath(FilePath)<br />
End If<br />
If Fso.folderexists(Server.MapPath(FilePath)) then<br />
Fso.deleteFolder Server.MapPath(FilePath)<br />
End If<br />
Next<br />
Set fso = nothing</p>
<p>If Err then<br />
Err.clear()<br />
DelFile = false<br />
'ShowError "删除文件或目录失败",""<br />
else<br />
DelFile = true<br />
End If<br />
End Function</p>
<p><br />
'*******************************************************<br />
'函数名:ReNameFile((oldName,newName)<br />
'作 用: 重命名文件或目录<br />
'参 数:strOldName '原文件名 多个用"|"隔开<br />
' strNewName '新文件名 多个用"|"隔开<br />
' 上面两个参数请保持一致<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
Function ReNameFile(strOldName,strNewName)<br />
On Error Resume Next<br />
Dim fso,arrOld,arrNew,i,oldName,newName</p>
<p>old = GetSafeStr(strOldName,"")<br />
Newfile = GetSafeStr(strNewName,"")</p>
<p>If old ="" or Newfile = "" then<br />
ReNameFile = false<br />
Exit Function<br />
End If</p>
<p>arrOld = Split(strOldName,"|")<br />
arrNew = Split(strNewName,"|")</p>
<p>If UBound(arrOld)<> UBound(arrNew) then<br />
ReNameFile = false<br />
Exit Function<br />
End If</p>
<p>Set Fso = Server.CreateObject("Scripting.FileSystemObject")<br />
for i=0 to UBound(arrOld)<br />
oldName = Server.MapPath(arrOld(i))<br />
newName = Server.MapPath(arrNew(i))<br />
If Fso.FileExists(oldName) and not Fso.FileExists(newName) then<br />
fso.MoveFile oldName,newName<br />
'ReNameFile = True<br />
End If<br />
Next<br />
Set fso = nothing</p>
<p>If Err.Number <> 0 Then<br />
Err.clear()<br />
ReNameFile = false<br />
Else<br />
ReNameFile = True<br />
End If<br />
End Function</p>
<p><br />
'*******************************************************<br />
'函数名:CopyFiles((TempSource,TempEnd)<br />
'作 用: 复制文件或者目录<br />
'参 数:TempSource '源文件名 多个用"|"隔开<br />
' TempEnd '目的文件名 多个用"|"隔开<br />
' 注意:上面两个参数请保持一致,并且都为完整路径,<br />
' 已经经过Server.MapPath方法处理过<br />
'返回值:成功 true , 失败 false<br />
'*******************************************************<br />
Function CopyFiles(TempSource,TempEnd)<br />
On Error Resume Next<br />
Dim CopyFSO,arrSource,arrEnd</p>
<p>CopyFiles = false<br />
Set CopyFSO = Server.CreateObject("Scripting.FileSystemObject")</p>
<p>If TempSource ="" or TempEnd = "" then<br />
ErrRaise "复制文件或目录","条件为空"<br />
CopyFiles = false<br />
Exit Function<br />
End If</p>
<p>arrSource = Split(TempSource,"|")<br />
arrEnd = Split(TempEnd,"|")<br />
If UBound(arrSource) <> UBound(arrEnd) then<br />
CopyFiles= false<br />
Exit Function<br />
End If</p>
<p>for i=0 to UBound(arrSource)<br />
srcName = arrSource(i)<br />
tarName = arrEnd(i)<br />
IF CopyFSO.FileExists(srcName) and not CopyFSO.FileExists(tarName) then<br />
CopyFSO.CopyFile srcName,tarName<br />
CopyFiles = true<br />
End If</p>
<p>IF CopyFSO.FolderExists(srcName) and not CopyFSO.FolderExists(tarName)then<br />
CopyFSO.CopyFolder srcName,tarName<br />
CopyFiles = true<br />
End If<br />
Next<br />
Set CopyFSO = Nothing</p>
<p>If Err then<br />
'Err.clear()<br />
CopyFiles = false<br />
End If<br />
End Function<br />
%></p>
</blockquote>