|
server
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
reading a "counter" file and incrementing based on # in file.
I would like to have a script open a file (the file will only have a single
number in it). If it sees a 1 or 2 it should increment the number by 1 - a 1 becomes a 2 2 becomes a 3. If it sees a 3 it should reset to 0. Any help would be greatly appreciated. On May 1, 4:51 pm, mjm <m***@discussions.microsoft.com> wrote:
> I would like to have a script open a file (the file will only have a single Try something like this ...> number in it). If it sees a 1 or 2 it should increment the number by 1 - a 1 > becomes a 2 2 becomes a 3. If it sees a 3 it should reset to 0. > Any help would be greatly appreciated. ' note: this is untested. Use at you own risk sFilename = "X:\Somewhere\somename.txt" set fso = createobject("scripting.filesystemobject") n = cSng(fso.opentextfile(sFilename , 1).readall) if n < 3 then n = n+1 else n = 0 end if fso.opentextfile(sFilename , 2, true).write n Tom Lavedas ========== http://members.cox.net/tglbatch/wsh/
Show quote
"Tom Lavedas" <tglba***@cox.net> wrote in message My preference would be:news:1178053437.089123.221050@c35g2000hsg.googlegroups.com... > On May 1, 4:51 pm, mjm <m***@discussions.microsoft.com> wrote: >> I would like to have a script open a file (the file will only have a >> single >> number in it). If it sees a 1 or 2 it should increment the number by 1 - >> a 1 >> becomes a 2 2 becomes a 3. If it sees a 3 it should reset to 0. >> Any help would be greatly appreciated. > > Try something like this ... > > ' note: this is untested. Use at you own risk > sFilename = "X:\Somewhere\somename.txt" > set fso = createobject("scripting.filesystemobject") > n = cSng(fso.opentextfile(sFilename , 1).readall) > if n < 3 then > n = n+1 > else > n = 0 > end if > fso.opentextfile(sFilename , 2, true).write n n = n + 1 if n > 3 then n = 0 end if but both our solutions, as well as the statement of the problem, fail to recognize the possibility of the number being either less than 1 or greater than 3. If zero should be incremented to 1, that is what our solutions do, but perhaps it is supposed to stay at zero. Same if the number is already greater than 3, it will keep incrementing forever. /Al
Show quote
On May 1, 10:00 pm, "Al Dunbar" <AlanD***@hotmail.com.nospaam> wrote: I don't believe your last statement is true: "Same if the number is> "Tom Lavedas" <tglba***@cox.net> wrote in message > > news:1178053437.089123.221050@c35g2000hsg.googlegroups.com... > > > > > On May 1, 4:51 pm, mjm <m***@discussions.microsoft.com> wrote: > >> I would like to have a script open a file (the file will only have a > >> single > >> number in it). If it sees a 1 or 2 it should increment the number by 1 - > >> a 1 > >> becomes a 2 2 becomes a 3. If it sees a 3 it should reset to 0. > >> Any help would be greatly appreciated. > > > Try something like this ... > > > ' note: this is untested. Use at you own risk > > sFilename = "X:\Somewhere\somename.txt" > > set fso = createobject("scripting.filesystemobject") > > n = cSng(fso.opentextfile(sFilename , 1).readall) > > if n < 3 then > > n = n+1 > > else > > n = 0 > > end if > > fso.opentextfile(sFilename , 2, true).write n > > My preference would be: > > n = n + 1 > if n > 3 then > n = 0 > end if > > but both our solutions, as well as the statement of the problem, fail to > recognize the possibility of the number being either less than 1 or greater > than 3. If zero should be incremented to 1, that is what our solutions do, > but perhaps it is supposed to stay at zero. Same if the number is already > greater than 3, it will keep incrementing forever. > > /Al already greater than 3, it will keep incrementing forever." I specifically set the test to handle >3, but assumed it should be reset. Your solution does the same, I believe. Neither of our approaches address numbers less than zero, as you stated. The other problem I recognized and chose not to address is if the file contains non-numeric data. General error handling just seemed to be outside of the scope of the request. Tom Lavedas ============ http://members.cox.net/tglbatch/wsh/
Show quote
"Tom Lavedas" wrote: n=(n+1) mod 4> On May 1, 10:00 pm, "Al Dunbar" <AlanD***@hotmail.com.nospaam> wrote: > > "Tom Lavedas" <tglba***@cox.net> wrote in message > > > > news:1178053437.089123.221050@c35g2000hsg.googlegroups.com... > > > > > > > > > On May 1, 4:51 pm, mjm <m***@discussions.microsoft.com> wrote: > > >> I would like to have a script open a file (the file will only have a > > >> single > > >> number in it). If it sees a 1 or 2 it should increment the number by 1 - > > >> a 1 > > >> becomes a 2 2 becomes a 3. If it sees a 3 it should reset to 0. > > >> Any help would be greatly appreciated. > > > > > Try something like this ... > > > > > ' note: this is untested. Use at you own risk > > > sFilename = "X:\Somewhere\somename.txt" > > > set fso = createobject("scripting.filesystemobject") > > > n = cSng(fso.opentextfile(sFilename , 1).readall) > > > if n < 3 then > > > n = n+1 > > > else > > > n = 0 > > > end if > > > fso.opentextfile(sFilename , 2, true).write n > > > > My preference would be: > > > > n = n + 1 > > if n > 3 then > > n = 0 > > end if > > > > but both our solutions, as well as the statement of the problem, fail to > > recognize the possibility of the number being either less than 1 or greater > > than 3. If zero should be incremented to 1, that is what our solutions do, > > but perhaps it is supposed to stay at zero. Same if the number is already > > greater than 3, it will keep incrementing forever. > > > > /Al > > I don't believe your last statement is true: "Same if the number is > already greater than 3, it will keep incrementing forever." > > I specifically set the test to handle >3, but assumed it should be > reset. Your solution does the same, I believe. > > Neither of our approaches address numbers less than zero, as you > stated. > > The other problem I recognized and chose not to address is if the file > contains non-numeric data. General error handling just seemed to be > outside of the scope of the request. > > Tom Lavedas > ============ > http://members.cox.net/tglbatch/wsh/ > >
Show quote
"neothwin" <neoth***@discussions.microsoft.com> wrote in message 1 becomes 2 - OKnews:60DFC600-C6A3-44DF-AE02-1B6B6F3F29F1@microsoft.com... > > > "Tom Lavedas" wrote: > >> On May 1, 10:00 pm, "Al Dunbar" <AlanD***@hotmail.com.nospaam> wrote: >> > "Tom Lavedas" <tglba***@cox.net> wrote in message >> > >> > news:1178053437.089123.221050@c35g2000hsg.googlegroups.com... >> > >> > >> > >> > > On May 1, 4:51 pm, mjm <m***@discussions.microsoft.com> wrote: >> > >> I would like to have a script open a file (the file will only have a >> > >> single >> > >> number in it). If it sees a 1 or 2 it should increment the number >> > >> by 1 - >> > >> a 1 >> > >> becomes a 2 2 becomes a 3. If it sees a 3 it should reset to 0. >> > >> Any help would be greatly appreciated. >> > >> > > Try something like this ... >> > >> > > ' note: this is untested. Use at you own risk >> > > sFilename = "X:\Somewhere\somename.txt" >> > > set fso = createobject("scripting.filesystemobject") >> > > n = cSng(fso.opentextfile(sFilename , 1).readall) >> > > if n < 3 then >> > > n = n+1 >> > > else >> > > n = 0 >> > > end if >> > > fso.opentextfile(sFilename , 2, true).write n >> > >> > My preference would be: >> > >> > n = n + 1 >> > if n > 3 then >> > n = 0 >> > end if >> > >> > but both our solutions, as well as the statement of the problem, fail >> > to >> > recognize the possibility of the number being either less than 1 or >> > greater >> > than 3. If zero should be incremented to 1, that is what our solutions >> > do, >> > but perhaps it is supposed to stay at zero. Same if the number is >> > already >> > greater than 3, it will keep incrementing forever. >> > >> > /Al >> >> I don't believe your last statement is true: "Same if the number is >> already greater than 3, it will keep incrementing forever." >> >> I specifically set the test to handle >3, but assumed it should be >> reset. Your solution does the same, I believe. >> >> Neither of our approaches address numbers less than zero, as you >> stated. >> >> The other problem I recognized and chose not to address is if the file >> contains non-numeric data. General error handling just seemed to be >> outside of the scope of the request. >> >> Tom Lavedas >> ============ >> http://members.cox.net/tglbatch/wsh/ >> >> > n=(n+1) mod 4 2 becomes 3 - OK 3 becomes 0 - OK but he did not say that 0 should become 1. /Al
Show quote
"Al Dunbar" wrote: ok> > "neothwin" <neoth***@discussions.microsoft.com> wrote in message > news:60DFC600-C6A3-44DF-AE02-1B6B6F3F29F1@microsoft.com... > > > > > > "Tom Lavedas" wrote: > > > >> On May 1, 10:00 pm, "Al Dunbar" <AlanD***@hotmail.com.nospaam> wrote: > >> > "Tom Lavedas" <tglba***@cox.net> wrote in message > >> > > >> > news:1178053437.089123.221050@c35g2000hsg.googlegroups.com... > >> > > >> > > >> > > >> > > On May 1, 4:51 pm, mjm <m***@discussions.microsoft.com> wrote: > >> > >> I would like to have a script open a file (the file will only have a > >> > >> single > >> > >> number in it). If it sees a 1 or 2 it should increment the number > >> > >> by 1 - > >> > >> a 1 > >> > >> becomes a 2 2 becomes a 3. If it sees a 3 it should reset to 0. > >> > >> Any help would be greatly appreciated. > >> > > >> > > Try something like this ... > >> > > >> > > ' note: this is untested. Use at you own risk > >> > > sFilename = "X:\Somewhere\somename.txt" > >> > > set fso = createobject("scripting.filesystemobject") > >> > > n = cSng(fso.opentextfile(sFilename , 1).readall) > >> > > if n < 3 then > >> > > n = n+1 > >> > > else > >> > > n = 0 > >> > > end if > >> > > fso.opentextfile(sFilename , 2, true).write n > >> > > >> > My preference would be: > >> > > >> > n = n + 1 > >> > if n > 3 then > >> > n = 0 > >> > end if > >> > > >> > but both our solutions, as well as the statement of the problem, fail > >> > to > >> > recognize the possibility of the number being either less than 1 or > >> > greater > >> > than 3. If zero should be incremented to 1, that is what our solutions > >> > do, > >> > but perhaps it is supposed to stay at zero. Same if the number is > >> > already > >> > greater than 3, it will keep incrementing forever. > >> > > >> > /Al > >> > >> I don't believe your last statement is true: "Same if the number is > >> already greater than 3, it will keep incrementing forever." > >> > >> I specifically set the test to handle >3, but assumed it should be > >> reset. Your solution does the same, I believe. > >> > >> Neither of our approaches address numbers less than zero, as you > >> stated. > >> > >> The other problem I recognized and chose not to address is if the file > >> contains non-numeric data. General error handling just seemed to be > >> outside of the scope of the request. > >> > >> Tom Lavedas > >> ============ > >> http://members.cox.net/tglbatch/wsh/ > >> > >> > > n=(n+1) mod 4 > > 1 becomes 2 - OK > 2 becomes 3 - OK > 3 becomes 0 - OK > but he did not say that 0 should become 1. > > /Al > > > n=((n + 1) Mod 4) * -(n <> 0) regards, |
|||||||||||||||||||||||