Jun 8th, 2006, 02:44 PM
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:
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:
Dim s As String
Open "C:\File.txt" For Input As #1
s = Input(LOF(1), 1)
s(0) will be the first line, s(1) will be the second line, etc.
Dim s() As String
Open "C:\File.txt" For Input As #1
s = Split(Input(LOF(1), 1), vbCrLf)
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:
Hmmm ... something's wrong, isn't it? Let's try to fix it.
s(0) = "This is the first line."
s(1) = "This is the second line."
s(2) = "This is the fourth line."
s(3) = ""
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.
Dim i As Integer, sTemp As String
'first read the file into s() with the above code
Redim Preserve s(UBound(s) + 1) 'We're going to need another element to add a line
For i = UBound(s) To 3 Step -1
s(i) = s(i - 1) 'move the lines to where they belong
'Now the fourth element reads "This is the fourth line."
'let's put some text into the 5th line
s(4) = "We added this text."
'and s(2) should be blank
s(2) = ""
'Now let's put this back on the disk
For i = 0 To UBound(s)
Print #1, s(i)
Last edited by si_the_geek; Aug 7th, 2006 at 04:12 PM.
Click Here to Expand Forum to Full Width
Survey posted by VBForums.