Change value in an indexed text file

Posted on 2006-05-10
Last Modified: 2010-05-01
I have indexed text file with data as follows:

0 3051
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 10
11 1
12 25
13 1
14 1000
15 2
16 3
17 20
18 0
19 0
20 -49.73201

How can i change individual values without messing up the index number?

Question by:SINCO
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    read the file and rewrite it with the values changed

    Author Comment

    Yeah, well done. But how do I change the value. Eg. how do i change the value corresponding to index 15 from 2 to 6?
    LVL 142

    Accepted Solution

    dim fso as object
    set fso = createobject("scripting.filesystemobject")

    dim ts as object
    dim l as string
    dim p() as string
    dim d as string

    set ts = fso.OpenTextstream("yourfile", 1)
    while not ts.AtEndOfStream
      l = ts.readline
      p = split(l, " ")
      if p(0) = 15 then
        p(1) = 6
      end if
      d = d & join(p, " ") & vbcrlf
    set ts = fso.OpenTextStream("yourfile", 2)
    ts.write d

    Author Comment

    I like it. Looks simple. I'll accept yr answer when I get it to work. Thanks

    Author Comment

    Great, it works. But using OpenTextFile. Thanks again dude.
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    Glad I could help, sorry for the wrong function name (As I don't have vb here....)

    the first comment was to start the direction, so I could detect what your level of understanding of the situation was...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now