Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188596
  • 博文数量: 106
  • 博客积分: 3810
  • 博客等级: 中校
  • 技术积分: 1007
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-18 13:35
文章分类

全部博文(106)

文章存档

2014年(17)

2011年(5)

2010年(75)

2009年(9)

我的朋友

分类:

2010-04-24 11:28:37

Option Explicit
'对文件进行操作的代码

Public Sub 同步(文件1 As String, 文件2 As String)

End Sub

'判断文件或者文件夹是否存在
Public Function 存在(Path As String) As Boolean
    If Right(Path, 1) = "\" Then Path = Left(Path, Len(Path) - 1)
    If Len(Trim(Path)) < 1 Then Exit Function
    '判断是否存在
    If Dir(Path, vbDirectory Or vbHidden Or vbSystem Or vbNormal Or vbReadOnly) <> "" Then
        '判断是否文件夹
        If (GetAttr(Path) And vbDirectory) <> vbDirectory Then
            存在 = True '是文件
        Else
            存在 = False
        End If
    End If
End Function


Public Sub StringSave(S As String, sPath As String)
Dim BB() As Byte, I As Long, C As Long
    S = StrConv(S, vbFromUnicode)
    BB = S
    For I = UBound(BB) To 0 Step -1
        If BB(I) <> 0 Then
            C = I
            Exit For
        End If
    Next I
    ReDim Preserve BB(C)
    Call BinSave(BB, sPath)
End Sub

'保存二进制到文件
Public Sub BinSave(B() As Byte, sPath As String)
Dim FreeNum As Integer
FreeNum = FreeFile()
If Dir(sPath) <> "" Then Kill (sPath)
Open sPath For Binary As #FreeNum
    Put #FreeNum, , B
Close #FreeNum
End Sub

Public Function ToBin(Path As String) As Byte()
    Dim FreeNum As Integer
    FreeNum = FreeFile()
    ReDim ToBin(FileLen(Path))
    Open Path For Binary As #FreeNum
        Get #FreeNum, , ToBin
    Close #FreeNum
End Function


'返回文件的文本内容,已经自动转换为Unicode编码
Public Function ToString(Path As String) As String
    ToString = ToBin(Path)
    ToString = StrConv(ToString, vbUnicode)
End Function



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'功能:文件名处理,如文件名为"c:\VbStudy\va3.exe" //

Public Function 路径(Path As String) As String
    路径 = Left(Path, InStrRev(Path, "\"))
End Function

'c:\VbStudy\va3
Public Function 无扩展名全路径(Path As String) As String
    无扩展名全路径 = Left(Path, Len(Path) - Len(扩展名(Path)))
End Function

Public Function 文件名(Path As String) As String
    文件名 = Right(Path, Len(Path) - Len(路径(Path)))
End Function

Public Function 扩展名(Path As String) As String
    Dim temp() As String
    If InStr(Path, ".") = 0 Then Exit Function
    temp = Split(Path, ".")
    扩展名 = "." & temp(UBound(temp))
End Function

Public Function 盘符(Path As String) As String
    盘符 = Left(Path, 1)
End Function


阅读(315) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~