+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, VBS Change File on a Certain Line? in Coding and Web Development; Hi there I have a Comma delimited file, which my script reads through, finds the line it wants and loads ...
  1. #1
    Samson's Avatar
    Join Date
    Mar 2007
    Location
    Wales
    Posts
    201
    Thank Post
    7
    Thanked 1 Time in 1 Post
    Rep Power
    17

    VBS Change File on a Certain Line?

    Hi there

    I have a Comma delimited file, which my script reads through, finds the line it wants and loads the data from it... however, I want to change this line from within the script, how would I go about removing the line I've just read, and readding it with the new data?

    For example:

    File.txt
    Code:
    "C:\test1.exe",1,10
    "C:\moo.exe",3,10
    "C:\test2.txt",0,10
    If my script finds the 2nd line is the one it wants, I then want to change the 3 to a 4, or vice versa, without affecting the rest of the file.

    It has really stumped me how I'd do this!

    Any advice would be great!

  2. #2
    Samson's Avatar
    Join Date
    Mar 2007
    Location
    Wales
    Posts
    201
    Thank Post
    7
    Thanked 1 Time in 1 Post
    Rep Power
    17

    Re: VBS Change File on a Certain Line?

    Am still struggling with this if anyone can provide any help it would be appreciated.

    I had the idea of just loading the entire file into an array, locating the data that needs to be changed, change it and then write the entire file back, but this seems like a tedious and in-efficient way of doing it.

    Thanks in advance

  3. #3
    ajbritton's Avatar
    Join Date
    Jul 2005
    Location
    Wandsworth
    Posts
    1,632
    Thank Post
    23
    Thanked 75 Times in 45 Posts
    Rep Power
    35

    Re: VBS Change File on a Certain Line?

    If you have to keep the file as a CSV, then I think you have a couple of possibilities;

    1 - Do as you suggest and read the entire file into memory in some way. Make the changes then write the entire file back out again. This is OK as long as the file is not too large and time/resources are not in short supply!

    2 - Another to do this would be to copy the file line by line into a new file in the same location (or even a temp file). When you reach the line you are looking for, make the change to the line before writing it out to the temp file. When you reach the end of the file, delete the original file and replace it with the temp file.

    Both these options make the assumption that only one process will access the file at any one time. (So it's not a Startup/Logon Script for PCs for example).

    Your other option would be to change the format of the file. If you can recode the file to something like an .INI, then there are ways of accessing and updating these from code which are fairly simple.

    One final option which I hesitate to suggest is to use XML. This is a really useful way of storing information, but the code to access it tends to be more complex.

  4. #4
    Samson's Avatar
    Join Date
    Mar 2007
    Location
    Wales
    Posts
    201
    Thank Post
    7
    Thanked 1 Time in 1 Post
    Rep Power
    17

    Re: VBS Change File on a Certain Line?

    I have opted with the 1st option, as the CSV is already in place.

    Its not a logon script, but it will be executed every time someone runs a program, so I have made the loading/writing of the file as quick as possible and added 2 random Wait's of between 500ms and 2000ms so that if a lot of users run a program simultaneously theres less chance of 2 machines trying to write to the file at once.

    Thanks for the advice.

  5. #5

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125

    Re: VBS Change File on a Certain Line?

    I don't know how many people are likely to run this but it's not going to scale if you have many users; you really do want to put this in a database of some sort (Access might do; SQL would be *much* better).

    If you don't have a SQL server then MySQL or Microsoft SQL 2005 Express are free and it's pretty simple to do the sort of thing you want. Learning SQL might also be fun :-)

  6. #6
    Samson's Avatar
    Join Date
    Mar 2007
    Location
    Wales
    Posts
    201
    Thank Post
    7
    Thanked 1 Time in 1 Post
    Rep Power
    17

    Re: VBS Change File on a Certain Line?

    Anywhere between 1 and around 50 users.

    Why wont it scale very well?

  7. #7
    ajbritton's Avatar
    Join Date
    Jul 2005
    Location
    Wandsworth
    Posts
    1,632
    Thank Post
    23
    Thanked 75 Times in 45 Posts
    Rep Power
    35

    Re: VBS Change File on a Certain Line?

    The more users that run it, the greater the likelihood of a clash. You would need to build code to detect clashes and to back off for a random amount of time before retrying.

  8. #8
    ajbritton's Avatar
    Join Date
    Jul 2005
    Location
    Wandsworth
    Posts
    1,632
    Thank Post
    23
    Thanked 75 Times in 45 Posts
    Rep Power
    35

    Re: VBS Change File on a Certain Line?

    Another possibility would be to use SQLLITE. This will allow multiple processes to access a single file (according to this).

    If you can use AutoIt, then there is SQLLITE functionality built in.

    Even with this, though, you will probably have to code for clashes.



SHARE:
+ Post New Thread

Similar Threads

  1. Line Rider
    By timbo343 in forum Windows
    Replies: 18
    Last Post: 26th November 2007, 05:54 PM
  2. Replies: 5
    Last Post: 10th January 2007, 03:55 PM
  3. Replies: 4
    Last Post: 23rd March 2006, 07:27 AM
  4. Who's your line manager?
    By steve in forum General Chat
    Replies: 25
    Last Post: 13th March 2006, 08:25 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •