< s c r i p t language=vb runat=server id="modFileTool"> ' 發展單位:風禹科技驗證有限公司 ' 撰寫人:鄭子璉(Tzu-Lien, Cheng, 璉璉) ,成大水利博肄,微軟最有價值專家 ' Web: http://tlcheng.twbbs.org/TLCheng/ E-Mail: qvb3377@ms5.hinet.net ' -------------------------------------------------------------------------------------- Public Enum enuNoGetData As Integer No_Default = 0 No_Common = 1 No_Null_Line = 2 No_NotData = No_Common Or No_Null_Line End Enum Public Function MyGetPathName(ByVal strFilename As String) As String strFilename = Replace(strFilename, "/", "\") Dim nLoc As Integer = InStrRev(strFilename, "\") If nLoc > 0 Then Return Left(strFilename, nLoc - 1) Else Return "" End If End Function Public Function MyGetFileName(ByVal strFilename As String) As String Dim nLoc As Integer = InStrRev(strFilename, "\") If nLoc > 0 Then Return Mid(strFilename, nLoc + 1) Else Return strFilename End If End Function Public Function MyGetFileFirstName(ByVal strFilename As String) As String strFilename = MyGetFileName(strFilename) Dim nLoc As Integer = InStrRev(strFilename, ".") If nLoc > 0 Then Return Left(strFilename, nLoc - 1) Else Return strFilename End If End Function Public Function MyGetFileExtendName(ByVal strFilename As String) As String Dim nLoc As Integer = InStrRev(strFilename, ".") If nLoc > 0 Then Return Mid(strFilename, nLoc + 1) Else Return "" End If End Function Public Function MyCreateDirectory(ByVal strPath As String) As Boolean Try System.IO.Directory.CreateDirectory(strPath) Catch Return False End Try Return True End Function Public Function MyDir(ByVal strFilename As String, Optional ByVal Attributes As FileAttribute = FileAttribute.Normal Or FileAttribute.ReadOnly Or FileAttribute.Hidden Or FileAttribute.System Or FileAttribute.Directory Or FileAttribute.Archive, Optional ByVal bSearchSub As Boolean = False) As Object Dim tFile As String Dim sAllFile As String() Dim nFile As Integer = -1 ReDim sAllFile(0) Dim ibf, ibs As Integer Dim objSub() As Object Dim strRootPath As String = MyGetPathName(strFilename) Dim strDirName As String = MyGetFileName(strFilename) If Right(strFilename, 1) = "\" Then strFilename = strFilename & "*.*" ElseIf InStrRev(strFilename, ".") > 0 Or InStrRev(strFilename, "*") > 0 Or InStrRev(strFilename, "?") > 0 Then ' 跳過不處理 ElseIf GetAttr(strFilename) And FileAttribute.Directory Then strFilename = strFilename & "\*.*" End If tFile = Dir(strFilename, Attributes) If tFile <> "" Then Do Until tFile = "" nFile = nFile + 1 ReDim Preserve sAllFile(nFile) sAllFile(nFile) = tFile tFile = Dir() Loop If bSearchSub Then ReDim objSub(nFile) For ibf = 0 To nFile If GetAttr(strRootPath & "\" & sAllFile(ibf)) And FileAttribute.Directory Then objSub(ibf) = MyDir(strRootPath & "\" & sAllFile(ibf) & "\" & strDirName, Attributes, bSearchSub) If Not IsNothing(objSub(ibf)) Then For ibs = 0 To UBound(objSub(ibf)) objSub(ibf)(ibs) = sAllFile(ibf) & "\" & objSub(ibf)(ibs) Next End If End If Next For ibf = 0 To UBound(objSub) If Not IsNothing(objSub(ibf)) Then For ibs = 0 To UBound(objSub(ibf)) nFile = nFile + 1 ReDim Preserve sAllFile(nFile) sAllFile(nFile) = objSub(ibf)(ibs) Next End If Next End If Return sAllFile Else Return Nothing End If End Function Public Function MyDeleteFile(ByVal strFilename As String) As Boolean Dim fi As New System.IO.FileInfo(strFilename) MyDeleteFile = False If fi.Exists Then fi.Delete() End If End Function Public Function IsFileExist(ByVal lpFilename As String) As Boolean Dim fi As New System.IO.FileInfo(lpFilename) Return fi.Exists End Function Public Function IsDirectoryExist(ByVal lpPathname As String) As Boolean Return IO.Directory.Exists(lpPathname) End Function Public Function MySaveBinaryFile(ByVal arrBytes As Byte(), ByVal lpFilename As String, Optional ByVal hFileSeek As Long = 0) As Object Dim strPath As String = MyGetPathName(lpFilename) If Not IsDirectoryExist(strPath) Then MyCreateDirectory(strPath) End If Dim fs As New System.IO.FileStream(lpFilename, System.IO.FileMode.Create) fs.Write(arrBytes, hFileSeek, UBound(arrBytes) + 1) fs.Close() End Function Public Function MyGetBinaryFile(ByVal lpFilename As String, Optional ByVal hFileSeek As Long = 0, Optional ByVal GetLen As Long = 0) As Object Dim fi As New System.IO.FileInfo(lpFilename) Dim fs As System.IO.FileStream If Not fi.Exists Then Return Nothing Else Dim fLen As Long fLen = fi.Length If fLen = 0 Then Return Nothing End If If GetLen > 0 Then fLen = fLen - hFileSeek If fLen > GetLen Then fLen = GetLen End If End If fs = fi.Open(IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite) Dim FileBuffer(fLen - 1) As Byte Dim nBytesRead As Integer = fs.Read(FileBuffer, hFileSeek, fLen) fs.Close() Return FileBuffer End If End Function Public Function MyGetFullTextFile(ByVal lpFilename As String, Optional ByVal srcCodePage As enuStandardCodePages = enuStandardCodePages.SCP_big5) As String Return BytesToString(BytesChangeCodePages(MyGetBinaryFile(lpFilename), srcCodePage)) End Function Public Function MyGetMultiLineFile(ByVal lpFilename As String, Optional ByVal wFlags As enuNoGetData = enuNoGetData.No_Default, Optional ByVal srcCodePage As enuStandardCodePages = enuStandardCodePages.SCP_big5) As String() Dim sBufLine() As String sBufLine = RemoveUnSelectLine(Split(MyGetFullTextFile(lpFilename, srcCodePage), vbNewLine), wFlags) Return sBufLine End Function Public Function RemoveUnSelectLine(ByVal arrMultiLine As String(), Optional ByVal wFlags As enuNoGetData = enuNoGetData.No_Default) As String() Dim sJoinLine As String Dim bAdd, bDel As Boolean Dim ibl, ubl, tLoc, nCount, tLen As Integer Dim tmpMultiLine As String() ubl = UBound(arrMultiLine) ReDim tmpMultiLine(ubl) nCount = -1 For ibl = 0 To ubl bAdd = True If wFlags And enuNoGetData.No_Common Then bDel = False tLoc = InStr(arrMultiLine(ibl), "'") If tLoc > 0 Then bDel = True arrMultiLine(ibl) = Left(arrMultiLine(ibl), tLoc - 1) End If tLoc = InStr(LCase(arrMultiLine(ibl)), "rem ") If tLoc > 0 Then bDel = True arrMultiLine(ibl) = Left(arrMultiLine(ibl), tLoc - 1) End If If bDel And Len(Trim(arrMultiLine(ibl))) = 0 Then bAdd = False End If End If If wFlags And enuNoGetData.No_Null_Line Then If Len(Trim(Replace(arrMultiLine(ibl), Chr(0), ""))) = 0 Then bAdd = False End If End If If bAdd Then nCount += 1 tmpMultiLine(nCount) = arrMultiLine(ibl) End If Next ReDim Preserve tmpMultiLine(nCount) Return tmpMultiLine End Function < / s c r i p t>