![]() |
|
[.NET] Cycle through Text Files For Specific Line? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] Cycle through Text Files For Specific Line? (/showthread.php?tid=5726998) |
Cycle through Text Files For Specific Line? - Haters Ganna Hate - Sep 28, 2017 So I have a very large 10GB text file that I split into 130 80MB text files. I need to cycle through all of them until I find a certain name. I know by looking at one text file, I can use this: Code Using reader As New StreamReader("Test.txt")But I need it to go to the next text file if the first text file does not have it. Luckily, the text files all start with the name "split" and their names are like:
I just need a loop or something that checks every single "split" text file for a keyword until it found it. Got a code example? * - DBots - Sep 28, 2017 Directory.GetFiles() has an option to set a filter. Here's what I did:
* - Haters Ganna Hate - Sep 29, 2017 (Sep 28, 2017 10:14 PM)DBots Wrote: Directory.GetFiles() has an option to set a filter. Does not seem to work. Here is my Form: ![]() And here is my code: https://pastebin.com/w94pEey4 I have two files called "split1" which contains: Quote:John Smith And "split2" which contains: Quote:Naing Man But when I search up the term "Haha Rob", it says it finds it in both split1 and split2 repeatedly. Im a little tired now and its dark so maybe I am not seeing something? * - DBots - Sep 29, 2017 after you .ReadLine, you need to compare it to the target string like If sLine.Equals(sTargetString) Then.. So yeah it seems you overlooked that part which you actually had something like that in your original code. * - Owl - Sep 29, 2017 Code Dim target As String = "Haha Rob"Works fine. * - DBots - Sep 29, 2017 OrKa got it! But man you should have let him try a little more he almost had it lol. * - joey karate - Sep 29, 2017 open up notepad++ http://prntscr.com/grabni * - dicktembe - Sep 30, 2017 I cycle through text files like this: Code If IO.File.Exists(datafilepath) ThenExample, here I open a file and get to a certain line that contains 'MouseSensitiveList=', I then get values of certain settings and replace them because I know they are 8 chars long. However you can do anything you like: Code If IO.File.Exists(configpath) Then* - Ninja - Oct 1, 2017 (Sep 30, 2017 08:38 AM)dicktembe Wrote: - SNIP - You read all contents of the text file at once into the program. This is not a good solution at all for handling large text files like OP is (130x 80MB). @OP; if you need random access I suggest using the MemoryMappedFile class, so you can get complete, and random access to a file without the need for seeking. If you don't, then a StreamReader and reading line by line like you already have is fine. RE: Cycle through Text Files For Specific Line? - Cairns - Nov 7, 2017 If you're reading a 10GB file that wont go too smoothly with any of those methods. I suggest you read x amount of lines (5000 lines for example) and append it to a stringbuilder, then check the stringbuilder.tostring if it contains the line text. If not, then continue running the cycle/loop until done. Do not read a 10GB file into memory at once and continuously check EACH line because it's gonna be a memory killer and its gonna take forever. RE: Cycle through Text Files For Specific Line? - gigajew - Nov 7, 2017 Here's how I do it Code using (var reader = new StreamReader("file.txt")) {RE: Cycle through Text Files For Specific Line? - 0xDEAD10CC - Nov 7, 2017 None of the above solutions is going to work very fast without dealing with mapping files into memory and reading things that way. When you start dealing with GB's the naive and most obvious approaches are the worst. That class posted by Ninja is essentially a wrapper around native functions like MapViewOfFile(), which is what I personally use to read large files > 1GB in fractions of the time it takes using normal (portable) methods. (mmap() for those on Linux.) That class makes it much easier than using the functions directly though because otherwise you'll be calculating pointer offsets, allocating to match system granularity, among other things. |