Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem incrementing a value in a text file

Status
Not open for further replies.

soneji

IS-IT--Management
Mar 19, 2004
19
US
Hello, what I am trying to do is increment a value in a text file. Basically the text file contains numerous values like,

album1
album1
album1

album2
album2
album2

What I'm trying to do is increment the 2 to a 3, and the 1 to a 2. ie:

album2
album2
album2

album3
album3
album3

I've created a script that searches for album(x) and increments each instance, but my problem is when it reaches album9 it changes it to album20. If I run it again it'll change album20 to album30. I'm lost as to what the problem is and I'm hoping someone will spot the problem.

Here is the script:
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1
Const ForWriting = 2
Dim i
i = 10

On Error Resume Next

Do
	'Opens the file for reading and copies the contents to a string, then closes the file
	Set objFile = objFSO.OpenTextFile("C:\test.txt" , ForReading)
	strContents = objFile.ReadAll
	objFile.Close
		
	'replaces album(x-1) with album(x) 
	strNewContents = Replace(strContents, "album" & (i - 1) , "album" & i )
	i = (i - 1)
		
	'opens the file for editing, pastes in the contents of the string and closes the file
	Set objFile = objFSO.OpenTextFile("C:\test.txt" , ForWriting)
	objFile.WriteLine strNewContents
	objFile.Close
		
Loop Until i = 0
 
You may try this:
strNewContents = Replace(strContents, "album" & (i - 1) & vbCrLf, "album" & i & vbCrLf)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Let me see if I can clarify it a little more, the album lines are similar to this..

&album1imagename1...(more text)
&album1imagename2...(more text)
&album1imagename3...(more text)

and when the number is 9 it'll change it to

&album20imagename1...(more text)
&album20imagename2...(more text)
&album20imagename3...(more text)

Thanks for the quick response
 
Here is what's happening...

You change this line &album9 to &album10.
When your counter is 2, you change &album10 to &album20 becaues it thinks &album10 is &album1.

You should either read the contents of your file line by line, or put the contents into an array and iterate through the array line by line. Don't do a replace on the entire file.

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
So, with your posted samples:
strNewContents = Replace(strContents, "album" & (i - 1) & "i", "album" & i & "i")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I think your problem is in your replace. If it's looking for "album1" it'll find it in album1, album10, album11, album100, etc.

&album1imagename1...(more text)
&album10imagename1...(more text)

Those would be replaced as
&album2imagename1...(more text)
&album20imagename1...(more text)

If imagename is "imagename" and not a variable of some sort, you could use the following:
Code:
strNewContents = Replace(strContents, "album" & (i - 1) & "imagename" , "album" & i & "imagename")
Otherwise, you're going to have to code it to read the full number after "album" and replace appropriately.
 
Thank you all for the information, it gave me what I need to fix this script.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top