asp中檔案與檔案夾常用處理函數(檔案尾碼、建立檔案等)

來源:互聯網
上載者:User

複製代碼 代碼如下:'=====================================
'獲得檔案尾碼
'=====================================
Function Get_Filetxt(ByVal t0)
Dim t1
IF Len(t0)<2 Or Instr(t0,".")=0 Then Get_Filetxt=False:Exit Function
t1=Split(t0,".")
Get_Filetxt=Lcase(t1(Ubound(t1)))
End Function

'=====================================
'讀取任何檔案的純程式碼
'=====================================
Function LoadFile(ByVal t0)
IF Len(t0)=0 Then Exit Function
IF Sdcms_Cache Then
IF Check_Cache("LoadFile_"&t0) Then
Create_Cache "LoadFile_"&t0,LoadFile_Cache(t0)
End IF
LoadFile=Load_Cache("LoadFile_"&t0)
Else
LoadFile=LoadFile_Cache(t0)
End IF
End Function

Function LoadFile_Cache(ByVal t0)
Dim t1,stm
On Error Resume Next
IF Len(t0)=0 Then Exit Function
t1=Empty
Set Stm=Server.CreateObject("Adodb.Stream")
With Stm
.Type=2'以本模式讀取
.mode=3
.charset=CharSet
.Open
.loadfromfile Server.MapPath(t0)
t1=.readtext
.Close
End With
Set Stm=Nothing
IF Err Then
LoadFile_Cache="“"&t0&"”"&Err.Description:Err.Clear
Else
LoadFile_Cache=t1
End IF
End Function

'=====================================
'檢查檔案是否存在
'=====================================
Function Check_File(ByVal t0)
Dim Fso
t0=Server.MapPath(t0)
Set Fso=CreateObject("Scripting.FileSystemObject")
Check_File=Fso.FileExists(t0)
Set Fso=Nothing
End Function

'=====================================
'檢查檔案夾是否存在
'=====================================
Function Check_Folder(ByVal t0)
Dim Fso
t0=Server.MapPath(t0)
Set Fso=CreateObject("Scripting.FileSystemObject")
Check_Folder=Fso.FolderExists(t0)
Set Fso=Nothing
End Function

'=====================================
'建立檔案夾(無限級)
'=====================================
Function Create_UpFile(ByVal t0)
Dim t1,t2,objFSO,i
On Error Resume Next
t0=Server.MapPath(t0)
IF InStr(t0,"\")<=0 Or InStr(t0,":")<=0 Then:Create_upfile=False:Exit Function
Set objFSO=CreateObject("Scripting.FileSystemObject")
IF objFSO.FolderExists(t0) Then:Create_upfile=True:Exit Function
t1=Split(t0,"\"):t2=""
For i=0 To UBound(t1)
t2=t2&t1(i)&"\"
IF Not objFSO.FolderExists(t2) Then objFSO.CreateFolder(t2)
Next
Set objFSO=Nothing
IF Err=0 Then Create_upfile=True:Else Create_upfile=False:Echo "Create_upfile:"&Err.Description&"<br>":Err.Clear
End Function

Sub SaveFile(ByVal t0,ByVal t1,ByVal t2)
Dim objFSO,t3
Set objFSO=CreateObject("Scripting.FileSystemObject")
IF t0="" Then Echo "目錄不可為空!":Died
t3=Server.MapPath(t0)
IF t2="" Or IsNull(t2) Then t2=""
IF objFSO.FolderExists(t3)=False Then Create_upfile(t0)
BuildFile t3&"\"&Trim(t1),t2
Set objFSO=Nothing
End Sub

Function BuildFile(ByVal t0,ByVal t1)
Dim Stm
On Error Resume Next
Set Stm=Server.CreateObject("Adodb.Stream")
With Stm
.Type=2 '以本模式讀取
.Mode=3
.Charset=CharSet
.Open
.WriteText t1
.SaveToFile t0,2
.Close
End With
Set Stm=Nothing
IF Err Then Echo "BuildFile:"&Err.Description&"<br>":Err.Clear
End Function

'=====================================
'重新命名檔案夾
'=====================================
Sub RenameFile(ByVal t0,ByVal t1)
Dim Fso
On Error Resume Next
Set Fso=Server.CreateObject("Scripting.FileSystemObject")
IF Fso.FolderExists(Server.MapPath(t0)) Then
Fso.MoveFolder Server.MapPath(t0),Server.MapPath(t1)
End IF
Set Fso=Nothing
IF Err Then Echo "Renamefile:"&Err.Description&"<br>":Err.Clear
End Sub

'=====================================
'重新命名檔案
'=====================================
Sub RenameHtml(ByVal t0,ByVal t1)
Dim Fso
On Error Resume Next
Set Fso=Server.CreateObject("Scripting.FileSystemObject")
IF Fso.FileExists(Server.MapPath(t0)) Then
Fso.MoveFile Server.MapPath(t0),Server.MapPath(t1)
End IF
Set Fso=Nothing
IF Err Then Echo "Renamehtml:"&Err.Description&"<br>":Err.Clear
End Sub

'=====================================
'刪除檔案夾
'=====================================
Sub DelFile(ByVal t0)
Dim Fso,F
On Error Resume Next
Set Fso=Server.CreateObject("Scripting.FileSystemObject")
Set F=fso.GetFolder(Server.MapPath(t0))
IF Not IsNull(t0) Then F.Delete True
IF Err Then Echo "Delfile:"&Err.Description&"<br>":Err.Clear
End Sub

'=====================================
'刪除檔案
'=====================================
Sub DelHtml(ByVal t0)
Dim Fso
On Error Resume Next
Set Fso=Server.CreateObject("Scripting.FileSystemObject")
IF Fso.FileExists(Server.MapPath(t0)) Then Fso.DeleteFile Server.MapPath(t0)
IF Err Then Echo "DelHtml:"&Err.Description&"<br>":Err.Clear
End Sub

Function Re_FileName(ByVal t0)
Dim t1
t0=Lcase(t0)
IF Len(t0)=0 Then Re_FileName="{id}":Exit Function
t1=Now()
'處理自訂檔案名稱

'IF Instr(t0,"{")>0 And Instr(t0,"}")>0 Then
'IF Instr(t0,"{id}")=0 Then
't0=t0&"{id}"'盡量防止重複
'End IF
'End IF
t0=Replace(t0,"{y}",Year(t1))
t0=Replace(t0,"{m}",Right("0"&Month(t1),2))
t0=Replace(t0,"{d}",Right("0"&Day(t1),2))
t0=Replace(t0,"{h}",Right("0"&Hour(t1),2))
t0=Replace(t0,"{mm}",Right("0"&Minute(t1),2))
t0=Replace(t0,"{s}",Right("0"&Second(t1),2))
Re_FileName=t0
End Function

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.