Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help on evaluating the first number

39 views
Skip to first unread message

justaguy

unread,
Apr 8, 2012, 12:59:48 PM4/8/12
to
Hi guys,

One of you kindly helped me with a script of generating a bunch of 5
random numbers.
Here's the code:

@echo off>uniqlist.txt
setlocal
set count=66
set n=1
:loop
set "uniq=0000%random%"
set "uniq=%uniq:~-5%"
find "%uniq" uniqlist.txt>nul
if %errorlevel% equ 0 goto :loop
echo %uniq%
echo %uniq%~>>uniqlist.txt
set /a n+=1
if not %n% gtr %count% goto :loop
notepad uniqlist.txt

Now, I need to ensure that the very first number is not 0,
so, before the line: echo %uniq% ~>>uniqlist.txt
we need to check if the first value of the uniq var is 0, how do we do
that?

Many thanks as usual.

justaguy

unread,
Apr 8, 2012, 2:41:50 PM4/8/12
to
On second though, changing the seed of 0000 in
set "uniq=0000%random%"
to
set "uniq=1111%random%"
seems to have solved the problem.

Am testing more... thanks anyway.

billious

unread,
Apr 8, 2012, 7:59:37 PM4/8/12
to
Might seem that way - but it hasn't. You only have a 1/32767 chance of
generating 0 as the random number on the first try, so you'd have to do a
great deal of testing to have a realistic chance.

It's rather frustrating that your approach appears to be to make a random
change rather than understanding the fundamentals of how the routine works.
This seems unfortunately to be the modern way - make a change, cursory test,
mark the problem solved.

Assuming that you reset the code to the original sequence of zeroes since
you find (as you probably eventually will) that the sequence of 1s did not
fix the problem, then to fix it reliably, use

:loop
set "uniq=0000%random%"
set "uniq=%uniq:~-5%"
IF %N%%UNIQ%==100000 GOTO LOOP
find "%uniq" uniqlist.txt>nul

(the capitalised line being the change; batch is largely immune to case, so
it may be changed to lower-case of you wish) The magic involved is to detect
the state that N is "1" and the unwanted value "00000" simultaneously. If
that is so, choose another.

Your original code theoretically produces COUNT different numbers in a
linear distribution of values 0..32767. The modified code's distribution is
very slightly different...


justaguy

unread,
Apr 8, 2012, 10:22:55 PM4/8/12
to
But now, with 66 counts, it contains numerous 00???, 0???? etc. But I
don't want any leading 0s.

foxidrive

unread,
Apr 9, 2012, 12:10:02 AM4/9/12
to
On 9/04/2012 12:22, justaguy wrote:
> On Apr 8, 7:59 pm, "billious" <billious_1...@hotmail.com> wrote:
>> justaguy wrote:
>>> On Apr 8, 12:59 pm, justaguy <do...@yahoo.com> wrote:
>>>> Hi guys,
>>
>>>> One of you kindly helped me with a script of generating a bunch of 5
>>>> random numbers.
>>>> Here's the code:

> But now, with 66 counts, it contains numerous 00???, 0???? etc. But I
> don't want any leading 0s.

Try this"

@echo off>uniqlist.txt
setlocal
set count=66
set n=1
:loop
set "uniq=%random%"
if %uniq% EQU 0 goto :loop
find "%uniq" uniqlist.txt>nul
if %errorlevel% equ 0 goto :loop
echo %uniq%
echo %uniq%~>>uniqlist.txt
set /a n+=1
if not %n% gtr %count% goto :loop
notepad uniqlist.txt



--
Mic

billious

unread,
Apr 9, 2012, 2:35:17 AM4/9/12
to
\/\/\/\/\/\/\/\/\/\/\/\/\/\

It seems you simply want to suppress the leading zeroes in each line.

Try:

@echo off
setlocal
set count=66
set n=1
:loop
set "uniq= %random%"
set "uniq=%uniq:~-5%"
find "%uniq%" uniqlist.txt>nul
if %errorlevel% equ 0 goto :loop
echo %uniq%
>>uniqlist.txt echo %uniq%
set /a n+=1
if not %n% gtr %count% goto :loop
notepad uniqlist.txt


Note that there are 4 spaces between "=" and "%random%"
Note also that on the FIND line, your code had the second "%" omitted.

The line

>>uniqlist.txt echo %uniq%

is perfectly valid and used to get over the anomaly that a digit occurring
directly before a redirector changes the redirection. Unfortunately, a
single space occurring here instead will be reproduced, as will the tilde
you used.

Naturally, if you actually WANT to have the tilde, just use your original
line.

(This latest code will allow "0" to be the first number selected...)


billious

unread,
Apr 9, 2012, 2:57:29 AM4/9/12
to
Nearly - but not quite.

consider the case where '123' is chosen after '1234'...

Now

findstr /b /e "%uniq%" uniqlist.txt >nul

would work.

... If the closing "%" is applied to uniq....

... And this produces left-justified results

... And you've interpreted 'first number' the same way as I did...

Yet another example of attempting to fix the METHOD without a firm idea of
the OBJECT (and actually, we're still guessing.)

Now a single introductory sentence like "I'm trying to produce a file of
unique random numbers, range 0-32767, right-justified in five columns" would
have been of immense help...


justaguy

unread,
Apr 10, 2012, 12:08:04 AM4/10/12
to
Mic and billious, thank you both for your assistance.

Yes, billious, I hear you on the objective of the code... which I
wrote for the related previous posting but hey how could I expect
people to remember it...

Ok, the objective is to generate a set of (count) of random numbers
made up of 5 digits (occasional 4 acceptable), each set may not start
with a 0.

Mic's code generated quite a few sets of 3 digits (sorry my fault).
billious code generated one 3 digits (sorry again my fault).


foxidrive

unread,
Apr 10, 2012, 4:21:15 AM4/10/12
to
On 10/04/2012 14:08, justaguy wrote:

> Ok, the objective is to generate a set of (count) of random numbers
> made up of 5 digits (occasional 4 acceptable), each set may not start
> with a 0.

There ya go. It helps to repeat the objective. :)


@echo off>uniqlist.txt
setlocal
set count=66
set n=1
:loop
set "uniq=%random%"
if %uniq% LSS 10000 goto :loop
findstr "^%uniq%$" uniqlist.txt>nul && goto :loop
echo %uniq%
echo>>uniqlist.txt %uniq%
set /a n+=1
if not %n% gtr %count% goto :loop
notepad uniqlist.txt



--
Mic

justaguy

unread,
Apr 10, 2012, 6:04:18 PM4/10/12
to
Beautiful, Mic. Thank you.

Dr J R Stockton

unread,
Apr 10, 2012, 4:11:59 PM4/10/12
to
In alt.msdos.batch.nt message <fc685e5b-d8ca-4f23-b5b1-41fd58e9652b@z17g
2000yqf.googlegroups.com>, Sun, 8 Apr 2012 09:59:48, justaguy
<do...@yahoo.com> posted:

>
>Now, I need to ensure that the very first number is not 0,
>so, before the line: echo %uniq% ~>>uniqlist.txt
>we need to check if the first value of the uniq var is 0, how do we do
>that?

Just generate a sufficiently long list of "random" values, and then
remove all the leading zeroes.

IIRC, the number of possible results of %random% is about 32768. That
could be generated with a 15-bit method, or by chopping down results
from a more-bit method, probably with a longer cycle length. Has the
cycle length been tested?

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Todd Vargo

unread,
Apr 10, 2012, 11:39:54 PM4/10/12
to
On 4/10/2012 6:04 PM, justaguy wrote:
> On Apr 10, 4:21 am, foxidrive<foxidr...@gotcha.woohoo.invalid> wrote:
>> On 10/04/2012 14:08, justaguy wrote:
>>
>>> Ok, the objective is to generate a set of (count) of random numbers
>>> made up of 5 digits (occasional 4 acceptable), each set may not start
>>> with a 0.
>>
>> There ya go. It helps to repeat the objective. :)
>>
>> @echo off>uniqlist.txt
>> setlocal
>> set count=66
>> set n=1
>> :loop
>> set "uniq=%random%"
>> if %uniq% LSS 10000 goto :loop
>> findstr "^%uniq%$" uniqlist.txt>nul&& goto :loop
>> echo %uniq%
>> echo>>uniqlist.txt %uniq%
>> set /a n+=1
>> if not %n% gtr %count% goto :loop
>> notepad uniqlist.txt
>>
>> --
>> Mic
>
> Beautiful, Mic. Thank you.

It has been a bit over a year since I posted the original code.

http://preview.tinyurl.com/8yaoz7m

As Mic's modification demonstrates, the new requirement to generate
numbers without leading zeros was not a difficult one to accomplish.
However, if you find that you would like to expand the 10000 to 32767
range up to 99999, you may find the following modification also useful.

@echo off>uniqlist.txt
setlocal
set count=66
set n=1
:loop
set "uniq=%random%"
if %uniq% equ 0 goto :loop
set "uniq=%uniq%%random%%random%%random%%random%"
set "uniq=%uniq:~0,5%"
find "%uniq%" uniqlist.txt>nul && goto :loop
echo %uniq%
echo>>uniqlist.txt %uniq%
set /a n+=1
if not %n% gtr %count% goto :loop
notepad uniqlist.txt

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Timo Salmi

unread,
Apr 11, 2012, 9:53:24 AM4/11/12
to
On 08.04.2012 19:59 justaguy wrote:
> One of you kindly helped me with a script of generating a bunch of 5
> random numbers.

One option

@echo off & setlocal enableextensions
::
:: Build a Visual Basic Script
set skip=
set temp_=%temp%
if defined mytemp if exist "%mytemp%\" set temp_=%mytemp%
set vbs_=%temp_%\tmp$$$.vbs
>"%vbs_%" findstr "'%skip%VBS" "%~f0"
::
:: Run the script with Microsoft Windows Script Host Version 5.6
:: to generate a five digit random number between 10000 and 99999
for /f "tokens=*" %%a in ('
cscript //nologo "%vbs_%" 10000 99999') do (
set random_=%%a)
echo random_=%random_%
::
:: Clean up
for %%f in ("%vbs_%") do if exist %%f del %%f
endlocal & goto :EOF
'
'The Visual Basic Script
Randomize 'VBS
Set arg = WScript.Arguments 'VBS
Wscript.Echo RandomFn(arg(0), arg(1)) 'VBS
'
Function RandomFn (low, high) 'VBS
RandomFn = 1 + Int (low - 1 + (high - low + 1) * Rnd) 'VBS
End Function 'VBS

All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
http://www.netikka.net/tsneti/homepage.php
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

Timo Salmi

unread,
Apr 11, 2012, 10:02:58 AM4/11/12
to
On 11.04.2012 16:53 Timo Salmi wrote:
> Function RandomFn (low, high) 'VBS
> RandomFn = 1 + Int (low - 1 + (high - low + 1) * Rnd) 'VBS
RandomFn = Int (low + (high - low + 1) * Rnd) 'VBS

Dr J R Stockton

unread,
Apr 12, 2012, 2:08:50 PM4/12/12
to
In alt.msdos.batch.nt message <4F858F12...@uwasa.fi>, Wed, 11 Apr
2012 17:02:58, Timo Salmi <t...@uwasa.fi> posted:

>On 11.04.2012 16:53 Timo Salmi wrote:
>> Function RandomFn (low, high) 'VBS
>> RandomFn = 1 + Int (low - 1 + (high - low + 1) * Rnd) 'VBS
> RandomFn = Int (low + (high - low + 1) * Rnd) 'VBS
>> End Function 'VBS


Where it matters, one should consider and test the cycle length of the
underlying generator and the uniformity of the sequence generated.

An underlying generator with fewer than 17 bits cannot possibly, for
example, generate a random number in the range 00000-99999; with a
16-bit generator, for example, over a third of the hoped-for values will
be missing.

<http://www.merlyn.demon.co.uk/pas-rand.htm>
<http://www.merlyn.demon.co.uk/js-randm.htm>
<http://www.merlyn.demon.co.uk/js-shufl.htm>
<http://www.merlyn.demon.co.uk/vb-maths.htm#Rand>
may help by analogy.

I've published nothing (and know very little) about batch %random% - but
if it was written by colleagues of those responsible for JScript
Math.random, the omens are not good.

Moreover, it seems possible that the code behind %random% may not always
be the same.

VBScript had when I tested it, ... and still does have, a 24-bit
generator, which is mediocre for this millennium.

Page js-randm links to excellent Random functions by Johannes Baagoe;
they run in a Web page, so they will run in WSH and can thus be used in
Batch.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

frank.w...@gmail.com

unread,
Apr 13, 2012, 7:40:17 AM4/13/12
to
From Dr J R Stockton :
>Where it matters, one should consider and test the cycle
>length of the
>underlying generator and the uniformity of the sequence
>generated.

It might be sufficient to truncate the value received by
adding or OR-ing a constant (to avoid 0) to the
millisecond portion of %time% and multiplying that with
%random%.

Frank
--
Please pardon me if my comments are late: I read and
write mail at home and transfer it away from home.

Timo Salmi

unread,
Apr 13, 2012, 3:21:00 PM4/13/12
to
On 12.04.2012 21:08 Dr J R Stockton wrote:
> In alt.msdos.batch.nt message <4F858F12...@uwasa.fi>, Wed, 11 Apr
> 2012 17:02:58, Timo Salmi <t...@uwasa.fi> posted:
>> On 11.04.2012 16:53 Timo Salmi wrote:
>>> Function RandomFn (low, high) 'VBS
>>> RandomFn = Int (low + (high - low + 1) * Rnd) 'VBS
>>> End Function 'VBS

> An underlying generator with fewer than 17 bits cannot possibly, for
> example, generate a random number in the range 00000-99999; with a
> 16-bit generator, for example, over a third of the hoped-for values will
> be missing.

True. But if one is concerned, it is fairly trivial to combine two
sufficiently short random sequences side by side. Furthermore, the
quality of the random numbers hardly is essential anyway when one
resorts to crude methods such as batch programming.

billious

unread,
Apr 13, 2012, 11:32:46 PM4/13/12
to

"Timo Salmi" <t...@uwasa.fi> wrote in message
news:4F887C9C...@uwasa.fi...
> On 12.04.2012 21:08 Dr J R Stockton wrote:
>> In alt.msdos.batch.nt message <4F858F12...@uwasa.fi>, Wed, 11 Apr
>> 2012 17:02:58, Timo Salmi <t...@uwasa.fi> posted:
>>> On 11.04.2012 16:53 Timo Salmi wrote:
>>>> Function RandomFn (low, high) 'VBS
>>>> RandomFn = Int (low + (high - low + 1) * Rnd) 'VBS
>>>> End Function 'VBS
>
>> An underlying generator with fewer than 17 bits cannot possibly, for
>> example, generate a random number in the range 00000-99999; with a
>> 16-bit generator, for example, over a third of the hoped-for values will
>> be missing.
>
> True. But if one is concerned, it is fairly trivial to combine two
> sufficiently short random sequences side by side. Furthermore, the
> quality of the random numbers hardly is essential anyway when one
> resorts to crude methods such as batch programming.
>
> All the best, Timo

Agreed - but even a structure such as was suggested by Todd, which I suppose
is the "side by side" method you refer to,

set "uniq=%uniq%%random%%random%%random%%random%"
set "uniq=%uniq:~0,5%"

is fatally flawed - even assuming the random-number generator is returning a
perfectly-random number 0..32767. 61%+ of the numbers generated would start
"1" or "2" simply because of the right-most %random%

set digits=5
(set result=)
:loop
set selection=%random%
if %selection% geq 32760 goto loop
set result=%result%%selection:~-1%
set /a digits -=1
if %digits% gtr 0 goto loop
echo %result%

would seem to be better - OP's requirements then applied as a shell.

And frankly, "occasional 4 acceptable" and OP's lack of comment on the odd
range (10000 - 32767) generated by foxidrive's have me confused. AAMOI, I'd
have tried

set/a uniq=10000+%random%

to yield 10000-42767; but we really haven't got a handle on OP's real
requirements IMO.



foxidrive

unread,
Apr 14, 2012, 4:08:48 AM4/14/12
to
On 14/04/2012 13:32, billious wrote:
> And frankly, "occasional 4 acceptable" and OP's lack of comment on the odd
> range (10000 - 32767) generated by foxidrive's have me confused. AAMOI, I'd
> have tried

TBH the range of random numbers didn't occur to me. :)


> set/a uniq=10000+%random%
>
> to yield 10000-42767; but we really haven't got a handle on OP's real
> requirements IMO.


Hopefully they will weigh back in and comment on this.

--
Mic

Todd Vargo

unread,
Apr 14, 2012, 4:35:44 PM4/14/12
to
OP's requirements were clear enough for me. OP asked to modify my code,
which was posted over a year ago, to create a set of unique 5 digit
random numbers, only now without the zero padding.

Numbers from 00000 thru 32727 did not seem to be an issue back then.
However, if there is concern over the first digit being 1 or 2 heavy,
simply replace the first instance of %random% in my code with
%random:~-1% and then you are golden.

Note, the %random%%random%%random%%random% was included only as
prevention of the off chance that multiple single digit numbers came up
(a long shot but possible).

@echo off>uniqlist.txt
setlocal
set count=66
set n=1
:loop
set "uniq=%random:~-1%"
if %uniq% equ 0 goto :loop
set "uniq=%uniq%%random%%random%%random%%random%"
set "uniq=%uniq:~0,5%"
find "%uniq%" uniqlist.txt>nul && goto :loop
echo %uniq%
echo>>uniqlist.txt %uniq%
set /a n+=1
if not %n% gtr %count% goto :loop
notepad uniqlist.txt

Dr J R Stockton

unread,
Apr 14, 2012, 4:56:57 PM4/14/12
to
In alt.msdos.batch.nt message <136ab8dea59$frank.w...@gmail.com>,
Fri, 13 Apr 2012 11:40:17, frank.w...@gmail.com posted:

>From Dr J R Stockton :
>>Where it matters, one should consider and test the cycle
>>length of the
>>underlying generator and the uniformity of the sequence
>>generated.
>
>It might be sufficient to truncate the value received by adding or OR-
>ing a constant (to avoid 0) to the millisecond portion of %time% and
>multiplying that with %random%.

For me, using WinXP sp3, the fractional portion is two digits, and Batch
can read it about once per millisecond.

Your suggestion would help; but it would not be easy to prove its
properties. One must for example remember that multiplying two "random"
integers together gives a number which has a 75% chance of being even.

To get reproducible "random" sequences for testing, one would need to
use a pseudo-clock.

Note though, that I am diverging from what is necessary for the OP, with
the caveat "Where it matters".

--
(c) John Stockton, near London. *@merlyn.demon.co.uk/?.?.Stockton@physics.org
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, and links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (RFC5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFC5536/7)

Dr J R Stockton

unread,
Apr 15, 2012, 6:28:37 AM4/15/12
to
In alt.msdos.batch.nt message <4F887C9C...@uwasa.fi>, Fri, 13
Apr
2012 22:21:00, Timo Salmi <t...@uwasa.fi> posted:

>On 12.04.2012 21:08 Dr J R Stockton wrote:
>> In alt.msdos.batch.nt message <4F858F12...@uwasa.fi>, Wed, 11 Apr
>> 2012 17:02:58, Timo Salmi <t...@uwasa.fi> posted:
>>> On 11.04.2012 16:53 Timo Salmi wrote:
>>>> Function RandomFn (low, high) 'VBS
>>>> RandomFn = Int (low + (high - low + 1) * Rnd) 'VBS
>>>> End Function 'VBS
>
>> An underlying generator with fewer than 17 bits cannot possibly, for
>> example, generate a random number in the range 00000-99999; with a
>> 16-bit generator, for example, over a third of the hoped-for values will
>> be missing.
>
>True. But if one is concerned, it is fairly trivial to combine two
>sufficiently short random sequences side by side. Furthermore, the
>quality of the random numbers hardly is essential anyway when one
>resorts to crude methods such as batch programming.

One never knows who else may read advice given publicly and use it
where
more advice is needed; and I did somewhere say "Where it matters".

Sequences being combined must be of independent origin to get any
real
gain; however many calls of %random% are stirred together to generate
a
long random string, the cycle length cannot exceed that of the
internal
state of the %random% engine used. The VBS engine is known, and I
have
emulated it in vb-maths.htm; it is a 24-bit Lehmer engine -
<http://en.wikipedia.org/wiki/Linear_congruential_generator>
<http://en.wikipedia.org/wiki/Lehmer_RNG>?

Batch returns 15 bits; but it is easy to show that the generator has
more than that - there was no pattern repeat in more than 2^20
consecutive values. It might well use the same routine as VBScript,
in which case one wouls need to check somewhat over 2^24
consecutive values.

--
(c) John Stockton, near London, UK. Using Google, no spell-check.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Pascal, JavaScript, ....|

justaguy

unread,
Apr 16, 2012, 10:10:40 AM4/16/12
to
On Apr 15, 6:28 am, Dr J R Stockton <J.R.Stock...@physics.org> wrote:
> In alt.msdos.batch.nt message <4F887C9C.9000...@uwasa.fi>, Fri, 13
> Apr
> 2012 22:21:00, Timo Salmi <t...@uwasa.fi> posted:
>
>
>
>
>
>
>
>
>
> >On 12.04.2012 21:08 Dr J R Stockton wrote:
> >> In alt.msdos.batch.nt message <4F858F12.9000...@uwasa.fi>, Wed, 11 Apr
@ Dr. J R Stockton,

I thought abou this approach then abandoned it. The thought is,
generating
desired random number at the first cut is more desirable option.

@ Todd Vargo
Sorry, I forgot to quote your name for the original code to give
sufficient credit.
And thanks for the new code.

@ Timo Salmi

Why would we need VBS for this? Simply finding another way to do the
same?

@ Frank Westl
Interesting thought, thanks.

In sum, thank you all.


Timo Salmi

unread,
Apr 16, 2012, 3:02:27 PM4/16/12
to
On 16.04.2012 17:10 justaguy wrote:
> @ Timo Salmi
> Why would we need VBS for this? Simply finding another way to do the
> same?

Quite. For the philosophy see e.g. the following Google Groups Advanced
Search http://bit.ly/HSw26W

frank.w...@gmail.com

unread,
Apr 16, 2012, 10:28:08 AM4/16/12
to
From Dr J R Stockton:
>Note though, that I am diverging from what is necessary
>for the OP, with
>the caveat "Where it matters".

You had already diverged and it was that divergence of
which I commented.

Caveat:
The contents of this message apply only where
applicable; in all other circumstances they should by
applied in a manner in which they are correct. It is the
readers responsibility to ensure correct application of
this message.

Todd Vargo

unread,
Apr 17, 2012, 4:28:48 PM4/17/12
to
On 4/16/2012 10:28 AM, frank.w...@gmail.com wrote:
> From Dr J R Stockton:
>> Note though, that I am diverging from what is necessary
>> for the OP, with
>> the caveat "Where it matters".
>
> You had already diverged and it was that divergence of which I commented.
>
> Caveat:
> The contents of this message apply only where applicable; in all other
> circumstances they should by applied in a manner in which they are
> correct. It is the readers responsibility to ensure correct application
> of this message.

Point well stated. :)

Klaatu

unread,
Apr 19, 2012, 12:22:40 PM4/19/12
to
On Wed, 11 Apr 2012 13:53:24 GMT, Timo Salmi posted to
alt.msdos.batch.nt:

> @echo off & setlocal enableextensions
> ::
> :: Build a Visual Basic Script
> set skip=
> set temp_=%temp%
> if defined mytemp if exist "%mytemp%\" set temp_=%mytemp%
> set vbs_=%temp_%\tmp$$$.vbs
> >"%vbs_%" findstr "'%skip%VBS" "%~f0"
> ::
> :: Run the script with Microsoft Windows Script Host Version 5.6
> :: to generate a five digit random number between 10000 and 99999
> for /f "tokens=*" %%a in ('
> cscript //nologo "%vbs_%" 10000 99999') do (
> set random_=%%a)
> echo random_=%random_%
> ::
> :: Clean up
> for %%f in ("%vbs_%") do if exist %%f del %%f
> endlocal & goto :EOF
> '
> 'The Visual Basic Script
> Randomize 'VBS
> Set arg = WScript.Arguments 'VBS
> Wscript.Echo RandomFn(arg(0), arg(1)) 'VBS
> '
> Function RandomFn (low, high) 'VBS
> RandomFn = 1 + Int (low - 1 + (high - low + 1) * Rnd) 'VBS
> End Function 'VBS

Could you please clarify for me the reason for the %skip% variable in the
code above? TIA

--
... Frisbeetarianism: The belief that when you die, your soul goes up on
the roof and gets stuck.

foxidrive

unread,
Apr 19, 2012, 1:45:00 PM4/19/12
to
Quickie post. The text %skip% stops that line from being included in the VBS script.
Mic

Timo Salmi

unread,
Apr 19, 2012, 3:32:44 PM4/19/12
to
On 19.04.2012 19:22 Klaatu wrote:
> On Wed, 11 Apr 2012 13:53:24 GMT, Timo Salmi posted to
> alt.msdos.batch.nt:
>> set skip=
>> >"%vbs_%" findstr "'%skip%VBS" "%~f0"
:

> Could you please clarify for me the reason for the %skip% variable in the
> code above? TIA

If the skip variable had been omitted, a line
"%vbs_%" findstr "'VBS" "%~f0"
would also find itself, hence disrupting the VBS code with a line that
actually doesn't belong to the code's VBS section.

foxidrive

unread,
Apr 19, 2012, 4:05:17 PM4/19/12
to
On 20/04/2012 03:45, foxidrive wrote:
> On 20/04/2012 02:22, Klaatu wrote:
>> On Wed, 11 Apr 2012 13:53:24 GMT, Timo Salmi posted to
>> alt.msdos.batch.nt:
>>
>>> @echo off & setlocal enableextensions
>>> ::
>>> :: Build a Visual Basic Script
>>> set skip=
>>> set temp_=%temp%
>>> if defined mytemp if exist "%mytemp%\" set temp_=%mytemp%
>>> set vbs_=%temp_%\tmp$$$.vbs
>>> >"%vbs_%" findstr "'%skip%VBS" "%~f0"

> Quickie post. The text %skip% stops that line from being included in the VBS script.


This seems like it would work too. Not better, just different.


set script=VBS
set temp_=%temp%
if defined mytemp if exist "%mytemp%\" set temp_=%mytemp%
set vbs_=%temp_%\tmp$$$.vbs
>"%vbs_%" findstr "'%script%" "%~f0"




--
Mic

foxidrive

unread,
Apr 27, 2012, 9:12:31 AM4/27/12
to
On 20/04/2012 05:32, Timo Salmi wrote:
> On 19.04.2012 19:22 Klaatu wrote:
>> On Wed, 11 Apr 2012 13:53:24 GMT, Timo Salmi posted to
>> alt.msdos.batch.nt:
>>> set skip=
>>> >"%vbs_%" findstr "'%skip%VBS" "%~f0"
> :
>
>> Could you please clarify for me the reason for the %skip% variable in the
>> code above? TIA
>
> If the skip variable had been omitted, a line
> "%vbs_%" findstr "'VBS" "%~f0"
> would also find itself, hence disrupting the VBS code with a line that
> actually doesn't belong to the code's VBS section.
>
> All the best, Timo

This is an interesting way of filtering out the findstr line from a script file, Timo.


type "%~f0" | find " " | find /v "Not Me!" > %TEMP%\script.txt


In the original batch file, each line of the script was indented with 4 spaces. The second FIND filters itself out of the script. Clever.


The original script using this technique is here:

http://www.ericphelps.com/batch/samples/hta_password.bat.txt


--
Mic

Harry Vaderchi

unread,
Apr 27, 2012, 9:52:55 AM4/27/12
to
So back in the day (and the other NG) we could have had:

@echo off
If exist %0.bat %0.bat
type %0 | find " "|find "Not me!" /v > %TEMP%\tmp.dbg
debug<%TEMP%\tmp.dbg
del %TEMP%\tmp.dbg
echo test.com
goto :EOF
:: asm indented 4 spaces
A100
mov dx,110
mov ah,9
int 21
ret

E110 "Hello, World!$"

Ntest.com
Rcx
20
W
Q


--
[dash dash space newline 4line sig]

Albi CNU

billious

unread,
Apr 27, 2012, 10:42:12 AM4/27/12
to

"Harry Vaderchi" <ad...@127.0.0.1> wrote in message
news:op.wdffiiuk1r0rdn@dell3100...
Hmm - small danger here of including other lines which CONTAINED 4
consecutive spaces - especially indentation on multi-line statements.

But - if there are no leading 4-space indents in non-DEBUG code then
shouldn't

type %0 | findstr /b /c:" " > %TEMP%\tmp.dbg

do the job?

And in Timo's case,

>"%vbs_%" findstr /e "'VBS" "%~f0"

?




foxidrive

unread,
Apr 27, 2012, 10:45:44 AM4/27/12
to
Almost. Back in the other newsgroup there was no 'goto :EOF' statement.




--
Mic


foxidrive

unread,
Apr 27, 2012, 10:47:09 AM4/27/12
to
The line above would have been included too. This is what we are getting around...


> And in Timo's case,
>
>> "%vbs_%" findstr /e "'VBS" "%~f0"
>
> ?

Ditto.



--
Mic


billious

unread,
Apr 27, 2012, 11:47:58 AM4/27/12
to

"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:Zjymr.1610$tH5....@newsfe11.iad...
>>
>> Hmm - small danger here of including other lines which CONTAINED 4
>> consecutive spaces - especially indentation on multi-line statements.
>>
>> But - if there are no leading 4-space indents in non-DEBUG code then
>> shouldn't
>>
>> type %0 | findstr /b /c:" " > %TEMP%\tmp.dbg
>>
>> do the job?
>
> The line above would have been included too. This is what we are getting
> around...
>
>
>> And in Timo's case,
>>
>>> "%vbs_%" findstr /e "'VBS" "%~f0"
>>
>> ?
>
> Ditto.
>

Oh - a little rash, I believe.

Works for me in both instances.

;-)


foxidrive

unread,
Apr 27, 2012, 1:32:16 PM4/27/12
to
I didn't notice those sneaky little switches. :)



--
Mic


Harry Vaderchi

unread,
Apr 28, 2012, 7:49:58 AM4/28/12
to
On Fri, 27 Apr 2012 15:42:12 +0100, billious <billio...@hotmail.com>
wrote:

>
> "Harry Vaderchi" <ad...@127.0.0.1> wrote in message
> news:op.wdffiiuk1r0rdn@dell3100...

[trimmed]
>> So back in the day (and the other NG) we could have had:
>>
>> @echo off
>> If exist %0.bat %0.bat
>> type %0 | find " "|find "Not me!" /v > %TEMP%\tmp.dbg
>> debug<%TEMP%\tmp.dbg
>> del %TEMP%\tmp.dbg
>> echo test.com
>> goto :EOF
>> :: asm indented 4 spaces

>>
>> --

Sorry, the end space gets dropped after leaving me.


> Hmm - small danger here of including other lines which CONTAINED 4
> consecutive spaces - especially indentation on multi-line statements.
>
> But - if there are no leading 4-space indents in non-DEBUG code then
> shouldn't
>
> type %0 | findstr /b /c:" " > %TEMP%\tmp.dbg
>
> do the job?
>
> And in Timo's case,
>
>> "%vbs_%" findstr /e "'VBS" "%~f0"
>

I don't think we had findstr back then. Excellent suggestion for today,
though!

billious

unread,
Apr 28, 2012, 8:50:21 AM4/28/12
to

"Harry Vaderchi" <ad...@127.0.0.1> wrote in message
news:op.wdg4hkpl1r0rdn@dell3100...
Did %0 work in the olden days?


Harry Vaderchi

unread,
Apr 28, 2012, 10:22:11 AM4/28/12
to
On Sat, 28 Apr 2012 13:50:21 +0100, billious <billio...@hotmail.com>
Yes.
and e.g. type %0\..\fileincurdir

Todd Vargo

unread,
Apr 28, 2012, 3:33:34 PM4/28/12
to
That assumes the batch was either in the current directory, or that %0
contained a fully qualified filespec. Hence, "%~f0".

Harry Vaderchi

unread,
Apr 29, 2012, 3:30:41 PM4/29/12
to
On Sat, 28 Apr 2012 20:33:34 +0100, Todd Vargo <tlv...@sbcglobal.netz>
wrote:

[trimmed]
>>> Did %0 work in the olden days?
>>>
>> Yes.
>> and e.g. type %0\..\fileincurdir
>>
>
> That assumes the batch was either in the current directory, or that %0
> contained a fully qualified filespec. Hence, "%~f0".
>
Sorry, yes, %0\..\file is a file in the same directory as the executed
batch file, not the current directory.

Todd Vargo

unread,
Apr 29, 2012, 4:52:06 PM4/29/12
to
On 4/29/2012 3:30 PM, Harry Vaderchi wrote:
> On Sat, 28 Apr 2012 20:33:34 +0100, Todd Vargo <tlv...@sbcglobal.netz>
> wrote:
>
> [trimmed]
>>>> Did %0 work in the olden days?
>>>>
>>> Yes.
>>> and e.g. type %0\..\fileincurdir
>>>
>>
>> That assumes the batch was either in the current directory, or that %0
>> contained a fully qualified filespec. Hence, "%~f0".
>>
> Sorry, yes, %0\..\file is a file in the same directory as the executed
> batch file, not the current directory.

The usual caveat remains, if batch is invoked using the basename only
and the batch is located somewhere along path, then 'type %0...' fails.

foxidrive

unread,
Apr 29, 2012, 6:51:07 PM4/29/12
to
On 30/04/2012 06:52, Todd Vargo wrote:
> On 4/29/2012 3:30 PM, Harry Vaderchi wrote:
>> On Sat, 28 Apr 2012 20:33:34 +0100, Todd Vargo <tlv...@sbcglobal.netz>
>> wrote:
>>
>> [trimmed]
>>>>> Did %0 work in the olden days?
>>>>>
>>>> Yes.
>>>> and e.g. type %0\..\fileincurdir
>>>>
>>>
>>> That assumes the batch was either in the current directory, or that %0
>>> contained a fully qualified filespec. Hence, "%~f0".
>>>
>> Sorry, yes, %0\..\file is a file in the same directory as the executed
>> batch file, not the current directory.
>
> The usual caveat remains, if batch is invoked using the basename only
> and the batch is located somewhere along path, then 'type %0...' fails.

That was catered for by the second line (echo off is line 1).



--
Mic


foxidrive

unread,
Apr 29, 2012, 6:55:53 PM4/29/12
to
I take that back. If it's executed on the path and not in the current folder then type %0 will fail, as you said.



--
Mic


0 new messages