Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
With VBScript I would like to find a string inside a text file then insert a number beside it. The numbers being the order each match was found.

Find string = xyz
For 1st match insert number 1 = xyz 1
For 2nd match insert number 2 = xyz 2
For 3rd match insert number 3 = xyz 3

I couldn't find any examples of something like this so hope someone can help. Thank You. :)
Posted
Updated 19-Dec-14 23:02pm
v2

1 solution

In order to insert anything in a text file, you have to copy the file up to that point, add the new data, then copy the rest of the file.

So read the file (either completely, or in blocks) and find the first string (if you are doing this in blocks, write the blocks up to that point as you go). Then write the new stuff, and keep going to the end of the file.
 
Share this answer
 
Comments
dalbrwn 20-Dec-14 5:36am    
Thanks for the info, am a little confused :). I can find and replace. I was trying to do it by finding a string then replacing it with the string and the number in which the match occurred.

Just haven't been able to figure out how to insert a number beside each match. The numbers should be in the order each match was found. I tried Count , but the best result was getting it to write the last match number next to the last match found.
OriginalGriff 20-Dec-14 5:54am    
How are you doing the find and replace at the moment?
dalbrwn 20-Dec-14 10:44am    
I can replace text easily enough. Just can't get the numbers correctly added. The latest I got was a message box for each match with it's count number (1 for the 1st, 2 for the 2nd etc). But when writing to the file it writes the total number of matches beside each. So if there were 8 matches found it writes 8 beside each instead of 1 for the 1st, 2 for the 2nd etc.
OriginalGriff 20-Dec-14 11:29am    
So how are you doing the replace? string.Replace? Regex? IndexOf and Substring?
Or something else?
dalbrwn 20-Dec-14 12:05pm    
Hi :) RegExp for the replace. But not really sure what I'm doing and if possible this way. Seemed to be getting somewhere so I have kept trying. And just can't find any examples for something like this to try another way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900