ASP FSO文件操作函数代码(复制文件、重命名文件(4)

接着分享几个

<%
Server.execute("redirect.asp")
username=session("username")
Dim SourceFile,TargetFile
SourceFile = "test\xx.txt"
TargetFile = "test\xx" 
TargetFile = TargetFile & "_" & username & ".txt"
' 文件已存在則先刪除
If IsExistFile(TargetFile) then
call DelFile(TargetFile)
End If 
' 複製文件
call CopyFiles(SourceFile,TargetFile)

'*******************************************************
'函數名:CopyFiles((TempSource,TempEnd)
'作 用:複製檔或者目錄
'參 數:TempSource '原始檔案名 多個用"|"隔開
' TempEnd '目的檔案名 多個用"|"隔開?
'注意:上面兩個參數請保持一致,文件路径为相对路径
'已經經過Server.MapPath方法處理過
'返回值:成功 true , 失敗 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 "複製文件或目錄","條件為空"
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(Server.MapPath(srcName)) and not CopyFSO.FileExists(Server.MapPath(tarName)) then
CopyFSO.CopyFile Server.MapPath(srcName),Server.MapPath(tarName)
CopyFiles = true
End If

IF CopyFSO.FolderExists(Server.MapPath(srcName)) and not CopyFSO.FolderExists(Server.MapPath(tarName))then
CopyFSO.CopyFolder Server.MapPath(srcName),Server.MapPath(tarName)
CopyFiles = true
End If
Next
Set CopyFSO = Nothing

If Err then 
'Err.clear()
CopyFiles = false
End If
End Function

'*******************************************************
'函數名:IsExistFile(FilePath)
'作 用: 判斷文件或目錄是否存在
'參 數:FilePath '檔路徑 多個檔用"|"隔開
'返回值:成功 true , 失敗 false
'*******************************************************
Function IsExistFile(FilePath)
On Error Resume Next
Dim fso,arrFile,i

If 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 "判斷文件或目錄是否存在失敗",""
else
IsExistFile = true
End If
End Function

'*******************************************************
'函數名:DelFile(FilePath)
'作 用: 刪除檔或目錄
'參 數:FilePath '檔路徑 多個檔用"|"隔開
'返回值:成功 true , 失敗 false
'*******************************************************
Function DelFile(FilePath)
On Error Resume Next
Dim Fso,arrFile,i

If 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 "刪除文件或目錄失敗",""
else
DelFile = true
End If
End Function
%>
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2076.html