• 0

[VB.NET] Deleting string from text file


Question

9 answers to this question

Recommended Posts

  • 0
  Puggsley said:
I need to search through a .txt file and remove a specific string. Anything i've found so far deletes a specific line number, but the string could appear on any line.

Ok, this answer will work, but I'm sure there are less expensive ways to achive the desired result. That said... Load the entire file into a string (oh my!) and then do a String.Replace("OLD VALUE", "") and then save the string to the file.

If the file is small, this is not a big deal, if the file is large it can take a serious chunk of memory.

If I think of something else I'll post back.

  • 0

Thanks for the prompt reply!

Thats the best solution i could come up with as well. As for the file size, there is about 10 files, varying from 20kb to 5mb so it could be a problem. Time and memory isn't too much of an issue and if it becomes one then it is something to look at in the future, but right now its more important to get something working

How much of a problem do you think it will be?

  • 0

I think the 5mb file is a LITTLE bit of an issue, but since these days (as opposed to 1995) memory is cheap and plentiful it's not something I would freak about.

If you write your code well, when you find a better solution, you'll be able to change that small chunk of code and get better performance. (hack? oh a little)

If it was me, and I was out of time, I'd go with the original idea and then at a later date do the performance gain. I've done tech work for 20 years and dev for 15. *I* find it most important to get the job done, over having it done well. Done well is nice, but not if it's NOT done. (read that sentence carefully)

  • 0

Would it be possible to read the file just one line at a time? This way only one line will have to be in memory. There may be a way to loop this method until the end of file has been reached. Just search through each line one at a time for the string to remove it. After the line has been analyzed, unload it and load the next line.

I'm not sure how you would go about coding this, but perhaps the idea will spark something. :)

  • 0

The problem with the "line by line" is that VB opens access to the file and will allocate a chunk of memory near the size of the file in either case. This is a concern and I'm sure there is a better solution that I have stated above... at the moment I don't have one. (sorry)

EDIT:

I thought I'd make a comment here: Besides the coding issues another point of development work is to know which goals to reach for... I had a friend back in San Francisco that spent a month creating a reporting engine in VB. Considering his fee and the abilities of VB the time would have been much better spent purchasing Crystal Reports or some other engine and having a large number of reports created by the end of that first month. (of course there are arguments for creating the engine, but in this case it was a serious waste of the client's cash)

SOMETIMES getting something done quickly vs writing tight, memory and CPU light code is better. It's a case by case issue based on the information at hand and the time alotted.

Just food for thought.

Peace,

James Rose

New York City

Edited by jameswjrose
  • 0

OK, so i took the plunge and went with the original suggestion but am having real difficulties getting it to work

To make sure its nothing else in the program i created a new project and put the following code on the form load event and created a text file with the contents "Hello World" in the output folder

Dim FileContents as String
Dim objReader as System.IO.StreamReader
objReader - New StreamReader(Environment.CurrentDirectory & "\test.txt")
FileContents = objReader.ReadToEnd
objReader.Close
FileContents.Replace("Hello", "")
Dim objWriter as System.IO.StreamWriter
objWriter = New StreamWriter(Environment.CurrentDirectory & "\test.txt")
objWriter.Write(FileContents)
objWriter.Close

From watching in explorer the program seems to zero byte the file and then write exactly the same contents back again.

Any ideas would be greatly appreciated!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Report: Trump's T1 Mobile off to a rocky start with messy pre-orders by David Uzondu You might have heard by now that The Trump Organization, spearheaded by President Trump's sons, Donald Trump Jr. and Eric Trump, is launching yet another product to add to the collection. This time, it is a gold smartphone, the T1, and a companion wireless service. The whole operation is being pushed with the usual "America-first" bravado, but it seems they forgot to get the basics right. If you thought you could just hop online and secure your patriotic pocket computer, you are in for a nasty surprise, as the whole process appears to be fundamentally broken. A new report from 404Media details this chaos perfectly, as one of their writers tried to order one of the T1 phones. The goal was simple: pay the $100 preorder deposit and see what this thing is all about when it ships. What happened next was a masterclass in how not to conduct e-commerce. The website crashed, booted him to an error page, and then, for good measure, charged his credit card the wrong amount entirely, taking $64.70. And get this, he received a confirmation email saying his order would ship... despite never once being asked for his shipping address. It is, in his words, the "worst experience I've ever faced buying a consumer electronic product". To add insult to injury, when he tried to log into the new account, the site prompted him to create, and he was immediately met with yet another error page, locking him out. The shoddy experience is not just limited to the checkout. Neowin found a bunch of errors on the official product page. Sure, it boasts a big 6.8-inch Punch Hole AMOLED display with a 120Hz refresh rate and a 50MP main camera, which sounds nice on paper. But then you notice the company completely forgot to mention what processor powers the phone, which is probably a MediaTek. At one point, the page bizarrely listed a "5,000 mAh long life camera," though that has since been fixed. By the way, there's good reason to doubt that this phone will be made in America, despite the press releases insisting it will be. Sourcing all the necessary components without using foreign parts is unbelievably difficult and expensive, something even Apple does not do. The more likely scenario, according to Max Weinbach, is that this is simply a reskin of a much cheaper device, maybe the T-Mobile REVVL 7 Pro 5G, which retails at a fraction of the T1's $499 asking price. The T1 Mobile joins a sprawling collection of other products likely aimed at the same loyal customer. The catalog of gear for this audience already includes the gold "Never Surrender" sneakers, the "God Bless the USA" Bible, "Victory47" perfume, digital trading cards, $TRUMP memecoins, and more. It is still very early days, of course, and while one might forgive some teething issues for a new venture, this initial preorder phase has been exceptionally chaotic. Hopefully, things will become much clearer once there is a firm launch date and a physical product to test. Do you plan to buy the T1 and move to Trump Mobile?
    • Is this release set for the end of this year or for next year?
    • Windows 10 KB5063159 fixes bug that wouldn't let some Microsoft Surface devices boot by Sayan Sen Microsoft released Windows 10 Patch Tuesday updates for the month last week. The one for Windows 10 under KB5060533 / KB5060531 / KB5061010 / KB5060998 introduced a bug that would not let Surface Hub v1 devices start due to a Secure Boot validation issue. As such, Microsoft had paused the update similar to the compatibility blocks or safeguard holds it applies for major feature updates as well. This bug was uncovered after the update went live, as Microsoft later added it to the list of known issues for that update and it also put up a big notice in bold. It wrote: Earlier today, the company released an out-of-band (OOB) update to address the issue. It has been published under KB5063159 and is only being offered to Surface Hub v1 devices instead of the buggy KB5060533 Patch Tuesday one. In the description of the new OOB update, Microsoft writes: You can find the support article for KB5063159 here on Microsoft's website. It is downloaded and installed automatically but users can also manually download it from the Microsoft Update Catalog website.
    • I thought I saw that one, and yeah, it was awhile ago, too..
  • Recent Achievements

    • Week One Done
      korostelev earned a badge
      Week One Done
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
    • Veteran
      matthiew went up a rank
      Veteran
    • Enthusiast
      Motoman26 went up a rank
      Enthusiast
  • Popular Contributors

    1. 1
      +primortal
      686
    2. 2
      ATLien_0
      268
    3. 3
      Michael Scrip
      184
    4. 4
      +FloatingFatMan
      177
    5. 5
      Steven P.
      140
  • Tell a friend

    Love Neowin? Tell a friend!