Results 1 to 3 of 3

Thread: File I/O Snippets

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    File I/O Snippets

    Code Snippets.
    All 100% mine. Waha

    Give me a mention if you use the code ...

    VB Code:
    1. ' Clear the contents of a file
    2. '
    3. Private Sub clearFile(ByVal strPath As String)
    4.     If Not Len(Dir(strPath)) = 0 Then
    5.         Open strPath For Output As #1
    6.         Close #1
    7.     End If
    8. End Sub
    9.  
    10. ' Is a given string contained within a given file ?
    11. '
    12. Private Function isStringInFile(ByVal strString As String, ByVal strFile As String) As Boolean
    13.     isStringInFile = InStr(returnContents(strFile), strString) <> 0
    14. End Function
    15.  
    16. ' Delete a specific line from a file (note: first line = line number 0)
    17. '
    18. Private Sub deleteLine(ByVal strFile As String, ByVal lineNumber As Long)
    19.     Dim strArrBuff() As String, i As Long
    20.     Open strFile For Input As #1
    21.         strArrBuff() = Split(input(LOF(1), 1), vbCrLf)
    22.     Close #1
    23.     Open strFile For Output As #1
    24.         For i = 0 To UBound(strArrBuff)
    25.             If Not i = lineNumber Then Print #1, strArrBuff(i)
    26.         Next
    27.     Close #1
    28. End Sub
    29.  
    30. ' Return a specific line number from a file (note: first line = line number 0)
    31. '
    32. Private Function getLine(ByVal strFile As String, ByVal lineNumber As Long) As String
    33.     Open strFile For Input As #1
    34.         getLine = Split(input(LOF(1), 1), vbCrLf)(lineNumber)
    35.     Close #1
    36. End Function
    37.  
    38. ' Append a line to the end of a file
    39. '
    40. Private Sub appendLine(ByVal strFile As String, ByVal strLineOfText As String)
    41.     Open strFile For Append As #1
    42.         Print #1, strLineOfText
    43.     Close #1
    44. End Sub
    45.  
    46. ' Insert a line of text in a file
    47. '
    48. Private Sub insertLine(ByVal strFile As String, ByVal lineNumber As Long, ByVal strLineOfText As String)
    49.     Dim strBuff() As String: strBuff = Split(returnContents(strFile), vbCrLf)
    50.     Dim i As Long
    51.     Open strFile For Output As #1
    52.         For i = 0 To UBound(strBuff)
    53.             If i = lineNumber Then Print #1, strLineOfText
    54.             Print #1, strBuff(i)
    55.         Next
    56.     Close #1
    57. End Sub
    58.  
    59. ' Insert a string of text in a file
    60. '
    61. Private Sub insertString(ByVal strFile As String, ByVal writePosition As Long, ByVal strStringOfText As String)
    62.     Dim strBuff As String: strBuff = returnContents(strFile)
    63.     Open strFile For Output As #1
    64.         Print #1, Left(strBuff, writePosition) & strStringOfText & Mid(strBuff, writePosition)
    65.     Close #1
    66. End Sub
    67.  
    68. ' Return the contents of a file
    69. '
    70. Private Function returnContents(ByVal strFile As String) As String
    71.     Open strFile For Input As #1
    72.         returnContents = input(LOF(1), 1)
    73.     Close #1
    74. End Function
    75.  
    76. ' Return the path of a given full path to a file
    77. '
    78. Private Function returnPathOfFile(ByVal strFile As String) As String
    79.     returnPathOfFile = Left(strFile, InStrRev(strFile, "\"))
    80. End Function
    81.  
    82. ' Return the filename of a given full path to a file
    83. '
    84. Private Function returnNameOfFile(ByVal strFile As String) As String
    85.     returnNameOfFile = Mid(strFile, InStrRev(strFile, "\") + 1)
    86. End Function
    87.  
    88. ' Split a file up into n byte chunks
    89. '
    90. Private Sub splitUpFile(ByVal strFile As String, ByVal nByteSize As Long)
    91.     Dim strBuff As String: strBuff = returnContents(strFile)
    92.     Dim currPos As Long, endPos As Long: currPos = 1: endPos = Len(strBuff)
    93.     Dim fileNumber As Long
    94.     While currPos <= endPos
    95.         Open Left(strFile, InStrRev(strFile, ".") - 1) & "(" & fileNumber & ")" & Mid(strFile, InStrRev(strFile, ".")) For Output As #1
    96.             If (currPos + nByteSize) > endPos Then
    97.                 Print #1, Mid(strBuff, currPos)
    98.             Else
    99.                 Print #1, Mid(strBuff, currPos, nByteSize)
    100.             End If
    101.         Close #1
    102.         fileNumber = fileNumber + 1
    103.         currPos = currPos + nByteSize
    104.     Wend
    105. End Sub
    106.  
    107. ' Merge a number of source files into a destination file
    108. '
    109. Private Sub mergeFiles(ByVal strDestinationFile As String, ParamArray strSourceFiles())
    110.     Dim i As Long, strBuff As String
    111.     Open strDestinationFile For Output As #1
    112.         For i = 0 To UBound(strSourceFiles)
    113.             Print #1, ""
    114.             Print #1, "***"
    115.             Print #1, "*** " & strSourceFiles(i)
    116.             Print #1, "***"
    117.             Print #1, returnContents(strSourceFiles(i))
    118.         Next
    119.     Close #1
    120. End Sub
    Last edited by plenderj; Jan 31st, 2002 at 11:36 AM.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Some much needed updates I suggest you use the following snippets in place of some of the ones above.
    This is because if you open a Binary file using the Input mode, it will nearly certainly fail once it hits binary characters.
    But if you open for Binary, and use the different input procedure, then you can open both text and binary files.

    Using the Get, , #FileNumber is also very fast!


    VB Code:
    1. ' Return a specific line number from a file (note: first line = line number 0)
    2. '
    3. Private Function getLine(ByVal strFile As String, ByVal lineNumber As Long) As String
    4.     Dim strBuff As String
    5.     Open strFile For Binary As #1
    6.         strBuff = Space(Lof(1))
    7.         Get #1, , strBuff
    8.         getLine = Split(strBuff, vbCrLf)(lineNumber)
    9.     Close #1
    10. End Function
    11.  
    12. ' Return a specific line number from a file (note: first line = line number 0) - a neater version.
    13. '
    14. Private Function getLine(ByVal strFile As String, ByVal lineNumber As Long) As String
    15.     getLine = Split(returnContents(strFile), vbCrLf)(lineNumber)
    16. End Function
    17.  
    18.  
    19. ' Delete a specific line from a file (note: first line = line number 0)
    20. '
    21. Private Sub deleteLine(ByVal strFile As String, ByVal lineNumber As Long)
    22.     Dim strArrBuff() As String, i As Long, strFileContent As String
    23.     strArrBuff() = Split(returnContents(strFile), vbCrLf)
    24.     strArrBuff(lineNumber) = vbNullString
    25.     Open strFile For Output As #1
    26.         Print #1, Join(strArrBuff, vbCrLf);
    27.     Close #1
    28. End Sub
    29.  
    30.  
    31. ' Return the contents of a file
    32. '
    33. Private Function returnContents(ByVal strFile As String) As String
    34.     Dim strBuff As String
    35.     Open strFile For Binary As #1
    36.         strBuff = Space(Lof(1))
    37.         Get #1, , strBuff
    38.         returnContents = strBuff
    39.     Close #1
    40. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    * 21-October-2004 - Moved to CodeBank *
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width