Editing specific column on specific rows of multiple text files in a folder

NPC1977

Member
Joined
Sep 30, 2021
Messages
5
Programming Experience
Beginner
Hello. Looking for some pointers on a project I am working on. I have multiple text files (not using the .txt extension as they are bespoke to the system I am using and can be anything from .A01 to .A99) in a folder (for arguments sake c:\temp\textfiles\). I'm looking to use a button on a form that when pressed scans each file in that folder and reads a specific column (say, colum 10) on the first, 3rd, bottom, 3rd bottom, 4th bottom and 6th bottom rows only and then set the value to 0 (it will either be a 1 or a 0 anyway), save the file, then display a text box that its complete. Researching in google is returning how to read a file and write a file but I cant find how to be so specific. My level of coding experience is probably Kindergarten at best (although I am now enrollled on a c# course) and i was wondering is this is possible, and if so - how? I'm sure it is as I sometimes use VB6 apps to do similar but the code has long been lost... Thanks in advance
 
In general text files have to have all the lines read into memory, then you make your modifications, and then you overwrite the original file with the lines from memory.

There are special cases like yours where the file size won't change and all you need to do is replace 1's with 0's. In those cases, you can write in place.

Furthermore, if the files lines are fixed size, then you can compute where to seek into the file, and do the replacements there. If the lines are not fixed size, then you'll need to scan through the file to find the line breaks.

Personally, I would recommend going for the simplest approach which is to read in all the lines into memory, change the appropriate lines, and then overwrite the original file. It's nice and easy to understand, but at the price of sacrificing memory and CPU (and if you have the files an SSD, block life).
 

Latest posts

Back
Top Bottom