Results 1 to 1 of 1
  1. #1

    Thread Starter
    Join Date
    Feb 2006
    East of NYC, USA

    Classic VB - How to edit a text file without opening it?

    The question of changing text files comes up every now and then. The usual form of the question is how to change the text in the file without loading the file into memory, the same way you can change the text in a text box without having to "load" it in any way. ("Text" files include any file in which the contents is text - like Notepad files and .ini files).

    The text in a text box is already in memory - Text1.Text is effectively a pointer to the memory location of the text in the text box. The text in a text file is on the disk drive - the only way a program can access that text is to read the file into memory (or map an area of memory to the area of drive on which the file sits, which is much more complicated than reading the file).

    This means that it is not possible to edit/read a file without opening it - but you can do it without showing the file.

    The easiest way to read a text file into a variable (which puts it into memory, so you can manipulate the text) is:
    VB Code:
    1. Dim s As String
    2.   Open "C:\File.txt" For Input As #1
    3.   s = Input(LOF(1), 1)
    4.   Close #1
    This will read the entire file into the variable s at one time. If you want an array of lines, one line in each element of the array, you can use the Split function like this:
    VB Code:
    1. Dim s() As String
    2.   Open "C:\File.txt" For Input As #1
    3.   s = Split(Input(LOF(1), 1), vbCrLf)
    4.   Close #1
    s(0) will be the first line, s(1) will be the second line, etc.

    You can directly (if you access the drive directly, which you haven't been able to do since Windows 98) change a character in the file, say 'x' to 'X'. What you absolutely can not do is add or delete characters directly on the drive.

    It has been asked how one can add text to a blank line on the drive, directly. A blank line, in the file, is nothing more than 2 vbCrLfs in a row. There's no blank space between them into which you can drop characters. If you could access the drive directly, you could read the entire file, from the second set of vbCrLf (vbCrLf is 2 characters - vbCr and vbLf) to the end of the file, into a string variable, put your new text after the first vbCrLf, then add the string variable after the end of your new characters. If the file isn't long enough for them you'd have to go to the directory and FAT (or equivalent in NTFS or whatever file system your operating systerm uses) , add another cluster to the file, and add your variable there. Very complicated, compared to reading the file, changing the array and writing the array back to the disk.

    Let's assume that the array, after we read the file, looks like this:
    s(0) = "This is the first line."
    s(1) = "This is the second line."
    s(2) = "This is the fourth line."
    s(3) = ""
    Hmmm ... something's wrong, isn't it? Let's try to fix it.
    VB Code:
    1. Dim i As Integer, sTemp As String
    2. 'first read the file into s() with the above code
    3. Redim Preserve s(UBound(s) + 1) 'We're going to need another element to add a line
    4. For i = UBound(s) To 3 Step -1
    5.   s(i) = s(i - 1) 'move the lines to where they belong
    6. Next i
    7. 'Now the fourth element reads "This is the fourth line."
    8. 'let's put some text into the 5th line
    9. s(4) = "We added this text."
    10. 'and s(2) should be blank
    11. s(2) = ""
    13. 'Now let's put this back on the disk
    14.   For i = 0 To UBound(s)
    15.     Print #1, s(i)
    16.   Next i
    17.   Close #1
    A lot easier than learning the disk structure and a method of gaining low-level access to the drive, and then manipulating the bytes as they sit on the drive.
    Last edited by si_the_geek; Aug 7th, 2006 at 03:12 PM.

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

By using this site, you agree to the Privacy Policy