Home All Groups Group Topic Archive Search About

reading a "counter" file and incrementing based on # in file.



Author
1 May 2007 8:51 PM
mjm
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.

Author
1 May 2007 9:03 PM
Tom Lavedas
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

Tom Lavedas
==========
http://members.cox.net/tglbatch/wsh/
Author
2 May 2007 2:00 AM
Al Dunbar
Show quote
"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
Author
2 May 2007 12:45 PM
Tom Lavedas
Show quote
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/
Author
5 May 2007 5:09 AM
neothwin
Show quote
"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
Author
5 May 2007 1:44 PM
Al Dunbar
Show quote
"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
Author
6 May 2007 12:48 PM
neothwin
Show quote
"Al Dunbar" wrote:

>
> "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
>
>
>

ok

n=((n + 1) Mod 4) * -(n <> 0)

regards,

AddThis Social Bookmark Button