ファイル操作関連

ファイル存在確認

Public Function ExistsFile(ByVal FilePath As String) As Boolean

    If (Dir(FilePath) = "") Then
        ExistsFile = False
    Else
        ExistsFile = True
    End If

End Function

テキストを開く

Public Function GetLines(ByVal FilePath As String) As String()

    Dim LineCount As Long
    Dim Lines() As String
    Dim FileNumber As Integer
    
    LineCount = 0
    ReDim Lines(63)
    
    FileNumber = FreeFile()
    Open FilePath For Input As #FileNumber
        Do Until EOF(FileNumber)
            ' カウントアップと配列調整
            LineCount = LineCount + 1
            If LineCount > UBound(Lines) Then
                ReDim Preserve Lines((UBound(Lines) + 1) * 2 - 1)
            End If
            
            ' リストに追加する
            Line Input #FileNumber, Lines(LineCount - 1)
        Loop
    Close #FileNumber
    
    ReDim Preserve Lines(LineCount - 1)
    GetLines = Lines

End Function

テキストを開く(ADODB版)

' テキストを読み込む
Public Function LoadText(ByVal Filename As String, Optional ByVal Charset As String = "UNICODE") As String

    Dim Stream As New ADODB.Stream

    Stream.Open
    Stream.Type = adTypeText
    Stream.Charset = Charset
    Stream.LoadFromFile Filename
    LoadText = Stream.ReadText(adReadAll)
    
    Stream.Close
    Set Stream = Nothing

End Function

' 行単位の文字列配列を返す
Public Function LoadLines(ByVal Filename As String, Optional ByVal Charset As String = "UNICODE") As String()

    Dim Text As String
    Text = LoadText(Filename, Charset)
    Text = Replace(Text, vbCrLf, vbLf)
    Text = Replace(Text, vbCr, vbLf)
    LoadLines = Split(Text, vbLf)

End Function

  • 最終更新:2015-10-01 15:54:23

このWIKIを編集するにはパスワード入力が必要です

認証パスワード