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

How do I get the position of a substring in a string?

5,308 views
Skip to first unread message

Timo Salmi

unread,
Jan 3, 2004, 3:11:31 PM1/3/04
to
DRAFT:

50) How do I get the position of a substring in a string?

@echo off & setlocal enableextensions
set a_=Hello world
set b_=or
call :InstrFN "%a_%" "%b_%" position_
echo The last position of "%b_%" in "%a_%" is %position_%
endlocal & goto :EOF

:: ===============================================================
:InstrFN
setlocal enableextensions enabledelayedexpansion
echo %1|findstr %2>nul
if %errorlevel% EQU 1 (endlocal & set %3=0& goto :EOF)
set rest_=%1
set /a instr_=0
:_loop
set rest_=%rest_:~1%
echo !rest_!|findstr %2>nul
if %errorlevel% EQU 1 (endlocal & set %3=%instr_%& goto :EOF)
set /a instr_ +=1
goto _loop

The output will be
D:\TEST>cmdfaq
The last position of "or" in "Hello world" is 8

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Phil Robyn

unread,
Jan 3, 2004, 5:30:31 PM1/3/04
to
Timo Salmi wrote:

Usually, INSTR or INDEX functions return the *first* or leftmost
position of the target string in the search string rather than
the *last* or rightmost position. So perhaps you should change
your topic. :-)

Here's another version that returns the *first* or leftmost position
of the target string in the search string, or '-1' if not found.

- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>ruler 70&echo Now is the time for all good men to come to the aid of their country.
....+....1....+....2....+....3....+....4....+....5....+....6....+....7
Now is the time for all good men to come to the aid of their country.

<Win2000> c:\cmd>demo\Instr "Now is the time for all good men to come to the aid of their country." "."

<Win2000> c:\cmd>set instr
Instr=69

<Win2000> c:\cmd>demo\Instr "Now is the time for all good men to come to the aid of their country." "or al"

<Win2000> c:\cmd>set instr
Instr=18

<Win2000> c:\cmd>demo\Instr "Now is the time for all good men to come to the aid of their country." "o"

<Win2000> c:\cmd>set instr
Instr=2

<Win2000> c:\cmd>rlist demo\Instr.cmd
=====begin c:\cmd\demo\Instr.cmd ====================
01. @echo off
02. setlocal
03. set S1=%~1
04. set S2=%~2
05. echo>t3mp.$$$ %S1%
06. for %%a in (t3mp.$$$) do set S1.len=%%~za
07. echo>t3mp.$$$ %S2%
08. for %%a in (t3mp.$$$) do set S2.len=%%~za
09. set /a S1.len-=2, S2.len-=2
10. del t3mp.$$$
11. set /a pos = -1
12. :next_char
13. set /a pos += 1
14. if %pos% gtr %S1.len% set /a pos = -1 & goto :DONE
15. call set S3=%%S1:~%pos%,%S2.len%%%
16. if "%S3%" EQU "%S2%" set /a pos+=1 & goto :DONE
17. goto :next_char
18. :DONE
19. endlocal&set /a %~n0=%pos%&goto :EOF
=====end c:\cmd\demo\Instr.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

Timo Salmi

unread,
Jan 4, 2004, 3:08:09 AM1/4/04
to
Phil Robyn <zipp...@uclink.berkeley.edu> wrote:
> Timo Salmi wrote:
> > DRAFT:

> > :InstrFN

> Usually, INSTR or INDEX functions return the *first* or leftmost
> position of the target string in the search string rather than
> the *last* or rightmost position. So perhaps you should change

I was aware of the problem, but could not come upon a reasonably
simple and logical way to overcome it. (Wait a minute ... if I
somehow reverse the string. Well, I'll have to think about this a
bit more.)

> Here's another version that returns the *first* or leftmost position
> of the target string in the search string, or '-1' if not found.

Once again, Phil, you quickly were able to come up with a more
befitting solution than I to a FAQ-like task. My best compliments.
At the very least I'll make a Google link to your solution posting
in the soon upcoming update of the tscmd.zip FAQ. The version under
construction suitably has now an even 50 items.

Al Dunbar

unread,
Jan 4, 2004, 12:23:35 PM1/4/04
to

"Timo Salmi" <t...@UWasa.Fi> wrote in message
news:bt8hl9$1...@poiju.uwasa.fi...

> Phil Robyn <zipp...@uclink.berkeley.edu> wrote:
> > Timo Salmi wrote:
> > > DRAFT:
>
> > > :InstrFN
>
> > Usually, INSTR or INDEX functions return the *first* or leftmost
> > position of the target string in the search string rather than
> > the *last* or rightmost position. So perhaps you should change
>
> I was aware of the problem, but could not come upon a reasonably
> simple and logical way to overcome it. (Wait a minute ... if I
> somehow reverse the string. Well, I'll have to think about this a
> bit more.)

Easy! But don't forget to reverse BOTH strings...

/Al


Timo Salmi

unread,
Jan 4, 2004, 12:15:52 PM1/4/04
to
@echo off & setlocal enableextensions
set a_=Hello world, Hello.
set b_=ll

call :InstrFN "%a_%" "%b_%" position_
echo The first position of "%b_%" in "%a_%" is %position_%
endlocal & goto :EOF

::===============================================================
:: A subroutine to find the first position of a searchstring in a
:: mainstring. If not found returns -1. If found, the indexing of
:: the position starts from 0 in accordance with SET conventions.
::
:InstrFN
setlocal enableextensions enabledelayedexpansion
::
:: Get the mainstring and the searchstring, get rid of possible quotes
set MainStr=%1
set MainStr=%MainStr:"=%
set SearchStr=%2
set SearchStr=%SearchStr:"=%
::
:: Get the length of the searchstring
set rest_=%SearchStr%
set /a SearchLength_=0
:_length_loop
set /a SearchLength_+=1
set rest_=%rest_:~1%
if defined rest_ goto :_length_loop
::
:: Check for a non-match
echo %MainStr%|find "%SearchStr%">nul
if %errorlevel% EQU 1 (endlocal & set /a %3=-1& goto :EOF)
::
:: Match the searchstring with mainstring substrings
set /a i_=0
:_position_loop
if "!MainStr:~%i_%,%SearchLength_%!"=="%SearchStr%" (
endlocal & set /a %3=%i_%& goto :EOF)
set /a i_+=1
goto _position_loop
endlocal & goto :EOF

Dr John Stockton

unread,
Jan 5, 2004, 7:39:39 AM1/5/04
to
JRS: In article <bt77lj$o...@poiju.uwasa.fi>, seen in
news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Sat, 3 Jan
2004 22:11:31 :-

>
>50) How do I get the position of a substring in a string?
>

ISTM that the above may be a member of a large class of questions which
can be answered using only a pure batch language but which can be much
more readily be answered in a higher level language.

That one, for example, I believe you could mentally sketch out in Pascal
in about a second, and implement in under a minute.

Of course, Batch users do not necessarily have Pascal; but ISTM that
where batches can be imported and run in NT+, XP, etc., it is at least
rather likely that Windows Scripting Host can be run.

WSH runs (at least) javascript & VBscript; finding the position should
be trivial in either language, one can write to standard output, and
there should be a way of giving command-line parameters to a JS or VB
script.

Using JS has the advantage, for some, that the language can also be used
in Web pages - one lot of learning serves two purposes. (I doubt
whether VB on a Web page is reliable on non-MS OSs.)

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Timo Salmi

unread,
Jan 5, 2004, 4:46:04 PM1/5/04
to
Dr John Stockton <sp...@merlyn.demon.co.uk> wrote:
> news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Sat, 3 Jan
> >50) How do I get the position of a substring in a string?

> ISTM that the above may be a member of a large class of questions which
> can be answered using only a pure batch language but which can be much
> more readily be answered in a higher level language.

This is very true. In fact, the task is already solved using gawk in
a compatible way in

243153 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi

However, in

56675 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

"the usage of any extra tools is avoided as much as reasonable". I
have tried to avoid replicating overly much of the former codes in
the latter, since the value added would not be high.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Al Dunbar

unread,
Jan 5, 2004, 10:40:54 PM1/5/04
to

"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
news:ddWr7HELsV+$Ew...@merlyn.demon.co.uk...

> JRS: In article <bt77lj$o...@poiju.uwasa.fi>, seen in
> news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Sat, 3 Jan
> 2004 22:11:31 :-
> >
> >50) How do I get the position of a substring in a string?
> >
>
> ISTM that the above may be a member of a large class of questions which
> can be answered using only a pure batch language but which can be much
> more readily be answered in a higher level language.

I agree. And further, any batch program that needs to make use of functions
of this sort is likely to be itself more ambitious than easily
accomplishable in batch.

> That one, for example, I believe you could mentally sketch out in Pascal
> in about a second, and implement in under a minute.
>
> Of course, Batch users do not necessarily have Pascal; but ISTM that
> where batches can be imported and run in NT+, XP, etc., it is at least
> rather likely that Windows Scripting Host can be run.
>
> WSH runs (at least) javascript & VBscript; finding the position should
> be trivial in either language, one can write to standard output, and
> there should be a way of giving command-line parameters to a JS or VB
> script.

In fact, in writing a function in WSH/vbscript to be called from batch, one
would probably spend 90% of the time and the code just dealing with the
interface between the two languages. IMHO, if one is resorting to WSH for a
couple of minor functions that are difficult to implement in batch, one
might have less frustration coding entirely in WSH. Either that, or at least
converting larger and larger chunks of code from batch to WSH.

Consider the effect of calling a WSH function from within a batch FOR loop.
If you are going to load cscript a thousand times, why not do it just once?

> Using JS has the advantage, for some, that the language can also be used
> in Web pages - one lot of learning serves two purposes. (I doubt
> whether VB on a Web page is reliable on non-MS OSs.)

That is most certainly true. However, the gist of most batch files is to
modify the local environment (creating/deleting files and folders, tweaking
the registry, etc). To do some from a web page typically requires a lowering
of security levels.

/Al


Al Dunbar

unread,
Jan 5, 2004, 10:45:23 PM1/5/04
to

"Timo Salmi" <t...@UWasa.Fi> wrote in message
news:btclus$p...@poiju.uwasa.fi...

> Dr John Stockton <sp...@merlyn.demon.co.uk> wrote:
> > news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Sat, 3 Jan
> > >50) How do I get the position of a substring in a string?
>
> > ISTM that the above may be a member of a large class of questions which
> > can be answered using only a pure batch language but which can be much
> > more readily be answered in a higher level language.
>
> This is very true. In fact, the task is already solved using gawk in
> a compatible way in
>
> 243153 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
> tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi
>
> However, in
>
> 56675 Dec 27 2003 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
> tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
>
> "the usage of any extra tools is avoided as much as reasonable". I
> have tried to avoid replicating overly much of the former codes in
> the latter, since the value added would not be high.

I agree, if the problem itself is one that requires batch for some reason. I
am (historically, anyway) as nutty about batch as anyone else here. But my
work environment is more concerned with solving problems as effectively as
possible than with doing things with batch that would make a newbie's head
spin. I do run some more involved batch programs, but I am finding that the
simple re-usability of WSH code (using the .WSF format) is making that
platform more ideal when looking at redevelopment.

/Al


Charles Dye

unread,
Jan 5, 2004, 10:57:55 PM1/5/04
to
On Mon, 5 Jan 2004 12:39:39 +0000, Dr John Stockton
<sp...@merlyn.demon.co.uk> wrote:

>JRS: In article <bt77lj$o...@poiju.uwasa.fi>, seen in
>news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Sat, 3 Jan
>2004 22:11:31 :-
>>
>>50) How do I get the position of a substring in a string?
>
>ISTM that the above may be a member of a large class of questions which
>can be answered using only a pure batch language but which can be much
>more readily be answered in a higher level language.

A shell script really is not the appropriate tool for manipulating
arbitrary strings. Complexity aside, it can be dangerous if you
don't know what the strings might be. Consider, for example, the
innocent-looking command

echo %myvar%

It looks harmless enough, but what if MYVAR contains syntactically
significant characters? Suppose it were created via

ekko 'y 124 'format * 'z: | stow myvar

All of a sudden our old familiar ECHO has the potential to wipe out
entire volumes! It seems to me that safely handling unknown strings
*requires* external tools created using some programming language
which makes the distinction between code and data.

--
Charles Dye ras...@highfiber.com

Dr John Stockton

unread,
Jan 6, 2004, 8:04:59 AM1/6/04
to
JRS: In article <nbqKb.962458$pl3.196109@pd7tw3no>, seen in
news:alt.msdos.batch.nt, Al Dunbar <alan-no-...@hotmail.com>
posted at Tue, 6 Jan 2004 03:45:23 :-

>
>"Timo Salmi" <t...@UWasa.Fi> wrote in message
>news:btclus$p...@poiju.uwasa.fi...
>> Dr John Stockton <sp...@merlyn.demon.co.uk> wrote:
>> > news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Sat, 3 Jan
>> > >50) How do I get the position of a substring in a string?
>>
>> > ISTM that the above may be a member of a large class of questions which
>> > can be answered using only a pure batch language but which can be much
>> > more readily be answered in a higher level language.

>> "the usage of any extra tools is avoided as much as reasonable". I


>> have tried to avoid replicating overly much of the former codes in
>> the latter, since the value added would not be high.

Agreed; but ISTM that, in Win32+, WSH is likely to be normally present,
so it is not as "extra" a tool as GAWK or COLS.

>I agree, if the problem itself is one that requires batch for some reason. I
>am (historically, anyway) as nutty about batch as anyone else here. But my
>work environment is more concerned with solving problems as effectively as
>possible than with doing things with batch that would make a newbie's head
>spin. I do run some more involved batch programs, but I am finding that the
>simple re-usability of WSH code (using the .WSF format) is making that
>platform more ideal when looking at redevelopment.


ISTM that there should be a FAQ or FAQ section, in News and Web, on
generalities of scripting in Win32; it is rather too easy to work only
(and with undue effort) using the tools that one first learnt, without
realising that a different approach may be much better.

ISTM also that you may be well able to draft it.

My own version (which for obvious reasons omits DOS/NT Batch) is at
<URL:http://www.merlyn.demon.co.uk/batfiles.htm#Other> but it is based
on knowledge which is as yet rather limited; for example, I know nothing
of interacting with the Environment in WSH scripts.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Timo Salmi

unread,
Jan 6, 2004, 4:43:16 PM1/6/04
to
Dr John Stockton <sp...@merlyn.demon.co.uk> wrote:
> >"Timo Salmi" <t...@UWasa.Fi> wrote in message
> >> "the usage of any extra tools is avoided as much as reasonable". I
> >> have tried to avoid replicating overly much of the former codes in
> >> the latter, since the value added would not be high.

> Agreed; but ISTM that, in Win32+, WSH is likely to be normally present,
> so it is not as "extra" a tool as GAWK or COLS.

Likewise agreed. But there is a very mundane reason for the total
exlucusion of WSH from my FAQ. I do know enough about it. Not at
least yet. And, despite the very welcome ideas and contributions by
the gentle readers, mine will stay a one-author FAQ. Thus, such
additions to my own FAQ's current concept have to be taken up
totally detached from it by someone else, if such information is
wanted at the moment.

> ISTM that there should be a FAQ or FAQ section, in News and Web, on
> generalities of scripting in Win32; it is rather too easy to work only

Again yes. And again as per my comment above.

> ISTM also that you may be well able to draft it.

It has been an interesting experience to draw up and learn the for
first 50 items in tscmd.zip. However, additions to my cmd.exe
scripting FAQ will now go much on hold. I have to be allocating my
time to quite another project of my own research interests, that, in
fact, has nothing to do with programming nor even much with
computers at that.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Al Dunbar

unread,
Jan 6, 2004, 11:37:04 PM1/6/04
to

"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
news:ijqE0sF7Jr+$Ew...@merlyn.demon.co.uk...

Personally, I doubt the value of a FAQ about everything scripting, or
rather, the ability of any of us to create a FAQ that would, once and for
all, give everyone the tools to understand the best scripting approach to
each problem.

But for those interested in, specifically, admin scripting for the win32
platform, consider microsoft.public.windows.server.scripting. The concept of
that newsgroup is that, if one's problem domain is admin scripting, one
shouldn't have to access all of the individual scripting newsgroups to find
an answer to a problem (and likel y blasted for multi-/cross- posting at
the same time).

> ISTM also that you may be well able to draft it.

If you are meaning me, then I am flattered. But, as I hint above, my heart
would not be up to such a task.

/Al


Dr John Stockton

unread,
Jan 7, 2004, 10:04:29 AM1/7/04
to
JRS: In article <btfa5k$j...@poiju.uwasa.fi>, seen in
news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi> posted at Tue, 6 Jan
2004 23:43:16 :-

>Dr John Stockton <sp...@merlyn.demon.co.uk> wrote:
>> >"Timo Salmi" <t...@UWasa.Fi> wrote in message
>> >> "the usage of any extra tools is avoided as much as reasonable". I
>> >> have tried to avoid replicating overly much of the former codes in
>> >> the latter, since the value added would not be high.
>
>> Agreed; but ISTM that, in Win32+, WSH is likely to be normally present,
>> so it is not as "extra" a tool as GAWK or COLS.
>
>Likewise agreed. But there is a very mundane reason for the total
>exlucusion of WSH from my FAQ. I do know enough about it. Not at
>least yet. And, despite the very welcome ideas and contributions by
>the gentle readers, mine will stay a one-author FAQ. Thus, such
>additions to my own FAQ's current concept have to be taken up
>totally detached from it by someone else, if such information is
>wanted at the moment.
>
>> ISTM that there should be a FAQ or FAQ section, in News and Web, on
>> generalities of scripting in Win32; it is rather too easy to work only
>
>Again yes. And again as per my comment above.
>
>> ISTM also that you may be well able to draft it.
>
>It has been an interesting experience to draw up and learn the for
>first 50 items in tscmd.zip. However, additions to my cmd.exe
>scripting FAQ will now go much on hold. I have to be allocating my
>time to quite another project of my own research interests, that, in
>fact, has nothing to do with programming nor even much with
>computers at that.

ISTM that you can do anything, but not everything at once - and, partly
for that reason, it was in fact Al D at whom that suggestion was
actually aimed.

For those who have been doing batch in MSDOS..Win98/ME for a number of
years, and who then start using a NT+/XP system, the move to NT+/XP
batch is a long but comparatively gentle slope; most of the old stuff
still works or can easily be adapted, and new features can be introduced
as they are learned.

The move to WSH is, in distinction, rather a jump upwards; one needs to
know javascript and/or VB, and one needs to discover how the scripting
connects with the rest of the system. But once the jump is made, one
has access to something more like a traditional HLL, and further
progress looks comparatively easy.

So ISTM that there is a need for a good signpost-plus at the foot of the
ascents, so that an informed choice can be made.


Slight progress; I can now read the Environment in WSH javascript.

Al Dunbar

unread,
Jan 7, 2004, 1:40:08 PM1/7/04
to

"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
news:+pqj2EH9$B$$Ew...@merlyn.demon.co.uk...

<blush/>

> For those who have been doing batch in MSDOS..Win98/ME for a number of
> years, and who then start using a NT+/XP system, the move to NT+/XP
> batch is a long but comparatively gentle slope; most of the old stuff
> still works or can easily be adapted, and new features can be introduced
> as they are learned.

That has certainly been my experience. It probably also explains why us
oldies are not the ones complaining most bitterly about the syntactical
oddities of NT batch, as we know that it brings more functionality than we
had previously.

> The move to WSH is, in distinction, rather a jump upwards;

I would have said sideways, diagonally forward, turning partways upside
down, moving to the next timezone, and switching partially into a parallel
universe, but YMMV on that one. ;-)

> one needs to
> know javascript and/or VB, and one needs to discover how the scripting
> connects with the rest of the system. But once the jump is made, one
> has access to something more like a traditional HLL, and further
> progress looks comparatively easy.

How much time one needs to spend walking up that hill before getting to the
easier downhill part is something that, imho, differs significantly from one
person to the next.

> So ISTM that there is a need for a good signpost-plus at the foot of the
> ascents, so that an informed choice can be made.

While that would seem to be a good idea, it is not clear exactly *where*
that signpost should be erected. I am also not convinced that it is
reasonable to put all of the required information on one sign board.

Perhaps each path leading to the mountain should contain signposts to the
others, with each path giving the nitty gritty about its own methodology.
(Note: path == scripting platform specific newsgroup). That would also help
out in the vbscript groups where I have seen people struggling mightily to
do something in vbscript that would be a snap in batch.

It is hard enough to create a FAQ about a platform one is expert in that
will be meaningful for newbies. The task of combining multiple technologies
in one FAQ would seem to me to be an exponentially more difficult thing. The
only way it would work, imho, would be for it to be kept to the very basic
level. VERY basic.

> Slight progress; I can now read the Environment in WSH javascript.

Bravo!

/Al


Todd Vargo

unread,
Jan 7, 2004, 7:18:49 PM1/7/04
to

"Al Dunbar" <alan-no-...@hotmail.com> wrote in message
news:coYKb.17293$ts4.5717@pd7tw3no...

>
> "Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
> news:+pqj2EH9$B$$Ew...@merlyn.demon.co.uk...
> > JRS: In article <btfa5k$j...@poiju.uwasa.fi>, seen in

> > For those who have been doing batch in MSDOS..Win98/ME for a number of


> > years, and who then start using a NT+/XP system, the move to NT+/XP
> > batch is a long but comparatively gentle slope; most of the old stuff
> > still works or can easily be adapted, and new features can be
> > introduced as they are learned.
>
> That has certainly been my experience. It probably also explains why us
> oldies are not the ones complaining most bitterly about the syntactical
> oddities of NT batch, as we know that it brings more functionality than
> we had previously.

Right on. I have been reading the NT related posts in a.m.b for years and
when I started using XP, it did not seem so foriegn. Well, I also use
Win95cmd in Win98 too, so that helps a bit (too bad Win95cmd was never fixed
to be 100% functional).

>
> > The move to WSH is, in distinction, rather a jump upwards;
>
> I would have said sideways, diagonally forward, turning partways upside
> down, moving to the next timezone, and switching partially into a
> parallel universe, but YMMV on that one. ;-)

I would reserve this discription for embedding WSH code into batch code. For
strait WSH coding, WSH 5.1 has a fine FREE referance available with
examples, and there are a great many books available to actually learn how
to use it. For BATCH we have newsgroups.

>
> > one needs to
> > know javascript and/or VB, and one needs to discover how the scripting
> > connects with the rest of the system. But once the jump is made, one
> > has access to something more like a traditional HLL, and further
> > progress looks comparatively easy.
>
> How much time one needs to spend walking up that hill before getting to
> the easier downhill part is something that, imho, differs significantly
> from one person to the next.
>
> > So ISTM that there is a need for a good signpost-plus at the foot of
> > the ascents, so that an informed choice can be made.
>
> While that would seem to be a good idea, it is not clear exactly *where*
> that signpost should be erected. I am also not convinced that it is
> reasonable to put all of the required information on one sign board.
>
> Perhaps each path leading to the mountain should contain signposts to the
> others, with each path giving the nitty gritty about its own methodology.
> (Note: path == scripting platform specific newsgroup). That would also
> help out in the vbscript groups where I have seen people struggling
> mightily to do something in vbscript that would be a snap in batch.
>
> It is hard enough to create a FAQ about a platform one is expert in that
> will be meaningful for newbies. The task of combining multiple
> technologies in one FAQ would seem to me to be an exponentially more
> difficult thing. The only way it would work, imho, would be for it to be
> kept to the very basic level. VERY basic.

Right, a signpost should point travelers to resources not provide them.
Answers to a FAQ is a resource. IMO, usage of another programming language
(i.e. WSH) within batch is considered advanced programming. Likewise,
shelling batch commands from another language is considered advanced
programming. Embedding can be a FAQ if it actually asked/answered
ferquently.

>
> > Slight progress; I can now read the Environment in WSH javascript.
>
> Bravo!

Wait till he finds out he can pass arguments to a script. ;-)

--
Todd Vargo (body of message must contain my name to reply by email)

Al Dunbar

unread,
Jan 7, 2004, 7:42:50 PM1/7/04
to

"Todd Vargo" <todd...@nccw.net> wrote in message
news:bti89m$7kkur$5...@ID-25025.news.uni-berlin.de...

>
> "Al Dunbar" <alan-no-...@hotmail.com> wrote in message
> news:coYKb.17293$ts4.5717@pd7tw3no...
> >
> > "Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
> > news:+pqj2EH9$B$$Ew...@merlyn.demon.co.uk...
> > > JRS: In article <btfa5k$j...@poiju.uwasa.fi>, seen in

<snip>

> > > The move to WSH is, in distinction, rather a jump upwards;
> >
> > I would have said sideways, diagonally forward, turning partways upside
> > down, moving to the next timezone, and switching partially into a
> > parallel universe, but YMMV on that one. ;-)
>
> I would reserve this discription for embedding WSH code into batch code.
For
> strait WSH coding, WSH 5.1 has a fine FREE referance available with
> examples, and there are a great many books available to actually learn how
> to use it. For BATCH we have newsgroups.

WSH also has newsgroups, including microsoft.public.scripting.wsh.

> > It is hard enough to create a FAQ about a platform one is expert in that
> > will be meaningful for newbies. The task of combining multiple
> > technologies in one FAQ would seem to me to be an exponentially more
> > difficult thing. The only way it would work, imho, would be for it to be
> > kept to the very basic level. VERY basic.
>
> Right, a signpost should point travelers to resources not provide them.
> Answers to a FAQ is a resource. IMO, usage of another programming language
> (i.e. WSH) within batch is considered advanced programming.

Certainly in the context of a group dedicated to only *one* of the platforms
involved.

> > > Slight progress; I can now read the Environment in WSH javascript.
> >
> > Bravo!
>
> Wait till he finds out he can pass arguments to a script. ;-)

Arg!

But I was thinking: wait till he finds out what he can do with WMI on a
network!

/Al


Dr John Stockton

unread,
Jan 8, 2004, 7:55:46 AM1/8/04
to
JRS: In article <eI1Lb.18575$X%5.18534@pd7tw2no>, seen in

news:alt.msdos.batch.nt, Al Dunbar <alan-no-...@hotmail.com> posted
at Thu, 8 Jan 2004 00:42:50 :-

>
>> > > Slight progress; I can now read the Environment in WSH javascript.
>> >
>> > Bravo!

And in WSH VB. I cannot yet write to it from those.


>> Wait till he finds out he can pass arguments to a script. ;-)

No need to wait; as those who read the cited <URL:http://www.merlyn.demon
.co.uk/batfiles.htm> will know, that is already done, for VB & JS.


>Arg!
>
>But I was thinking: wait till he finds out what he can do with WMI on a
>network!

Less likely; I have no network here.

Adapting your metaphor - we should not want a number of signposts, but a
single signpost with arms pointing in the various directions; or one of
those panoramic displays as set up at viewpoints - then we need signposts
pointing to that display.

Al Dunbar

unread,
Jan 9, 2004, 2:08:23 AM1/9/04
to

"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
news:3OxRPqISNV$$Ew...@merlyn.demon.co.uk...

> JRS: In article <eI1Lb.18575$X%5.18534@pd7tw2no>, seen in
> news:alt.msdos.batch.nt, Al Dunbar <alan-no-...@hotmail.com> posted
> at Thu, 8 Jan 2004 00:42:50 :-
> >
> >> > > Slight progress; I can now read the Environment in WSH javascript.
> >> >
> >> > Bravo!
>
> And in WSH VB. I cannot yet write to it from those.

Not sure why you would want to. The environment is the nt batch' version of
variables. vbscript has real variables, and persistence is more generally
kept in the registry than in the environment. Note that a batch that might
want to read that variable updated by the WSH script would fail to do so if
it was started before the change was made, as the local environment is
inhereited at the time the instance of cmd.exe is spawned.

> >> Wait till he finds out he can pass arguments to a script. ;-)
>
> No need to wait; as those who read the cited <URL:http://www.merlyn.demon
> .co.uk/batfiles.htm> will know, that is already done, for VB & JS.
>
>
> >Arg!
> >
> >But I was thinking: wait till he finds out what he can do with WMI on a
> >network!
>
> Less likely; I have no network here.

Oh, the horror! You poor man. ;-)

> Adapting your metaphor - we should not want a number of signposts, but a
> single signpost with arms pointing in the various directions; or one of
> those panoramic displays as set up at viewpoints - then we need signposts
> pointing to that display.

Alternately, switching metaphors on you, perhaps we should all get together
in one room somewhere to hammer this all out once and for all. My experience
with gathering intelligence on the web and in newsgroups is that none of it
is guaranteed correct, and it is not all coordinated.

/Al


Dr John Stockton

unread,
Jan 9, 2004, 7:36:53 AM1/9/04
to
JRS: In article <HrsLb.32191$JQ1.11662@pd7tw1no>, seen in

news:alt.msdos.batch.nt, Al Dunbar <alan-no-...@hotmail.com>
posted at Fri, 9 Jan 2004 07:08:23 :-

>
>"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
>news:3OxRPqISNV$$Ew...@merlyn.demon.co.uk...
>> JRS: In article <eI1Lb.18575$X%5.18534@pd7tw2no>, seen in
>> news:alt.msdos.batch.nt, Al Dunbar <alan-no-...@hotmail.com> posted
>> at Thu, 8 Jan 2004 00:42:50 :-
>> >
>> >> > > Slight progress; I can now read the Environment in WSH javascript.
>> >> >
>> >> > Bravo!
>>
>> And in WSH VB. I cannot yet write to it from those.
>
>Not sure why you would want to. The environment is the nt batch' version of
>variables. vbscript has real variables, and persistence is more generally
>kept in the registry than in the environment. Note that a batch that might
>want to read that variable updated by the WSH script would fail to do so if
>it was started before the change was made, as the local environment is
>inhereited at the time the instance of cmd.exe is spawned.

While this is a.m.b.*.nt, WSH is also usable in a.m.b OSs. Since in
those batch is more limited, it is more likely to be able to use a
little assistance from VB or JS.

For example, Win98 Batch has no knowledge of Summer Time (I don't know
whether NT+ batch has), but Win98 WSH JS can find out all about it, for
the current locality. While this can be passed back by

cscript //nologo F.js >> $$$.BAT
$$$

with $$$.BAT containing a series of SET commands, it would be nicer to
be able to be able to write to the parent environment directly, in one
or both types of OS.

Moreover, ISTM that it may well be that an experienced NT+ batch
programmer, needing special help from JS or VB to complete a job, might
well still want to do as much as possible in batch, treating the JS/VB
more or less as an imported utility.

Then JS has abilities lacking in VB, and vice versa; unless WSH VB can
call JS, it might useful to run JS & VB in turn from a batch,
communicating via the parent environment; similarly if an imported
utility needed assistance from JS or VB.

--

Todd Vargo

unread,
Jan 9, 2004, 6:26:25 PM1/9/04
to

"Dr John Stockton" wrote:

> While this is a.m.b.*.nt, WSH is also usable in a.m.b OSs. Since in
> those batch is more limited, it is more likely to be able to use a
> little assistance from VB or JS.
>
> For example, Win98 Batch has no knowledge of Summer Time (I don't know
> whether NT+ batch has), but Win98 WSH JS can find out all about it, for
> the current locality. While this can be passed back by
>
> cscript //nologo F.js >> $$$.BAT
> $$$
>
> with $$$.BAT containing a series of SET commands, it would be nicer to
> be able to be able to write to the parent environment directly, in one
> or both types of OS.

But that is what batch wrappers are for. The method in your example has been
used for many years (i.e. with BASIC interpreters).


>
> Moreover, ISTM that it may well be that an experienced NT+ batch
> programmer, needing special help from JS or VB to complete a job, might
> well still want to do as much as possible in batch, treating the JS/VB
> more or less as an imported utility.
>
> Then JS has abilities lacking in VB, and vice versa; unless WSH VB can
> call JS, it might useful to run JS & VB in turn from a batch,
> communicating via the parent environment; similarly if an imported
> utility needed assistance from JS or VB.

As you said, this is a.m.b.*.nt. ;-)

Dr John Stockton

unread,
Jan 10, 2004, 9:06:26 AM1/10/04
to
JRS: In article <btneme$91ke3$1...@ID-25025.news.uni-berlin.de>, seen in
news:alt.msdos.batch.nt, Todd Vargo <todd...@nccw.net> posted at Fri,
9 Jan 2004 18:26:25 :-

>
>"Dr John Stockton" wrote:
>
>> While this is a.m.b.*.nt, WSH is also usable in a.m.b OSs. Since in
>> those batch is more limited, it is more likely to be able to use a
>> little assistance from VB or JS.
>>
>> For example, Win98 Batch has no knowledge of Summer Time (I don't know
>> whether NT+ batch has), but Win98 WSH JS can find out all about it, for
>> the current locality. While this can be passed back by
>>
>> cscript //nologo F.js >> $$$.BAT
>> $$$
>>
>> with $$$.BAT containing a series of SET commands, it would be nicer to
>> be able to be able to write to the parent environment directly, in one
>> or both types of OS.
>
>But that is what batch wrappers are for. The method in your example has been
>used for many years (i.e. with BASIC interpreters).

Don't be silly, of course it is. The point is that a wrapper, to be
useful, needs something appropriate to wrap, and that WSH can provide
that something. Redirecting to $$$.bat is, however, inelegant in
comparison with directly doing what is wanted from within the VB or JS
itself. It would be helpful to all concerned if you were to make a
reasonable attempt to understand that which you otherwise feel a need to
comment on.


>> Moreover, ISTM that it may well be that an experienced NT+ batch
>> programmer, needing special help from JS or VB to complete a job, might
>> well still want to do as much as possible in batch, treating the JS/VB
>> more or less as an imported utility.
>>
>> Then JS has abilities lacking in VB, and vice versa; unless WSH VB can
>> call JS, it might useful to run JS & VB in turn from a batch,
>> communicating via the parent environment; similarly if an imported
>> utility needed assistance from JS or VB.
>
>As you said, this is a.m.b.*.nt. ;-)

Indeed; but that is no reason for struggling with a complex and
confusing batch-only solution in a case where WSH + VB/JS is a better
tool.

Most of those who ask real questions in News want solutions, preferably
understandable ones, using any readily-available tool. Those who only
like to demonstrate what they can achieve using only pure batch are mere
show-offs.

--
© John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

Al Dunbar

unread,
Jan 10, 2004, 8:06:48 PM1/10/04
to

"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
news:rayW6CMi...@merlyn.demon.co.uk...

> JRS: In article <btneme$91ke3$1...@ID-25025.news.uni-berlin.de>, seen in
> news:alt.msdos.batch.nt, Todd Vargo <todd...@nccw.net> posted at Fri,
> 9 Jan 2004 18:26:25 :-
> >
> >"Dr John Stockton" wrote:
> >
> >> While this is a.m.b.*.nt, WSH is also usable in a.m.b OSs. Since in
> >> those batch is more limited, it is more likely to be able to use a
> >> little assistance from VB or JS.
> >>
> >> For example, Win98 Batch has no knowledge of Summer Time (I don't know
> >> whether NT+ batch has), but Win98 WSH JS can find out all about it, for
> >> the current locality. While this can be passed back by
> >>
> >> cscript //nologo F.js >> $$$.BAT
> >> $$$
> >>
> >> with $$$.BAT containing a series of SET commands, it would be nicer to
> >> be able to be able to write to the parent environment directly, in one
> >> or both types of OS.
> >
> >But that is what batch wrappers are for. The method in your example has
been
> >used for many years (i.e. with BASIC interpreters).
>
> Don't be silly, of course it is. The point is that a wrapper, to be
> useful, needs something appropriate to wrap, and that WSH can provide
> that something. Redirecting to $$$.bat is, however, inelegant in
> comparison with directly doing what is wanted from within the VB or JS
> itself. [ It would be helpful to all concerned if you were to make a
> reasonable attempt to understand that which you otherwise feel a need to
> comment on. ]

You are certainly welcome to your opinions. But I am not sure how
constructive that last sentence is in terms of moving the discussion along.

> >> Moreover, ISTM that it may well be that an experienced NT+ batch
> >> programmer, needing special help from JS or VB to complete a job, might
> >> well still want to do as much as possible in batch, treating the JS/VB
> >> more or less as an imported utility.
> >>
> >> Then JS has abilities lacking in VB, and vice versa; unless WSH VB can
> >> call JS, it might useful to run JS & VB in turn from a batch,
> >> communicating via the parent environment; similarly if an imported
> >> utility needed assistance from JS or VB.
> >
> >As you said, this is a.m.b.*.nt. ;-)
>
> Indeed; but that is no reason for struggling with a complex and
> confusing batch-only solution in a case where WSH + VB/JS is a better
> tool.
>
> Most of those who ask real questions in News want solutions, preferably
> understandable ones, using any readily-available tool. [ Those who only
> like to demonstrate what they can achieve using only pure batch are mere

> show-offs. ]

Who cares if some of us are show-offs? Although I agree with you on certain
practical levels, one's own abilities in batch would remain rather stagnant
if it were not for those who come up with extreme ways to get batch to do
things most think are impossible.

/Al


Todd Vargo

unread,
Jan 10, 2004, 10:46:34 PM1/10/04
to

"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
news:rayW6CMi...@merlyn.demon.co.uk...

> JRS: In article <btneme$91ke3$1...@ID-25025.news.uni-berlin.de>, seen in
> news:alt.msdos.batch.nt, Todd Vargo <todd...@nccw.net> posted at Fri,
> 9 Jan 2004 18:26:25 :-
> >
> >"Dr John Stockton" wrote:
> >
> >> While this is a.m.b.*.nt, WSH is also usable in a.m.b OSs. Since in
> >> those batch is more limited, it is more likely to be able to use a
> >> little assistance from VB or JS.
> >>
> >> For example, Win98 Batch has no knowledge of Summer Time (I don't know
> >> whether NT+ batch has), but Win98 WSH JS can find out all about it, for
> >> the current locality. While this can be passed back by
> >>
> >> cscript //nologo F.js >> $$$.BAT
> >> $$$
> >>
> >> with $$$.BAT containing a series of SET commands, it would be nicer to
> >> be able to be able to write to the parent environment directly, in one
> >> or both types of OS.
> >
> >But that is what batch wrappers are for. The method in your example has
been
> >used for many years (i.e. with BASIC interpreters).
>
> Don't be silly, of course it is. The point is that a wrapper, to be
> useful, needs something appropriate to wrap, and that WSH can provide
> that something. Redirecting to $$$.bat is, however, inelegant in
> comparison with directly doing what is wanted from within the VB or JS
> itself.

Don't be silly, of course what is?

Yes, it would be nicer, your point was understood. But my point was, that
the method you posted HAS been used for years with other languages and
programs which do not support modification of the parent environment. So
IMO, it is no big deal to create a transient batch before returning to the
CALLing batch.


> It would be helpful to all concerned if you were to make a
> reasonable attempt to understand that which you otherwise feel a need to
> comment on.

It would be more helpful if you had noticed, I was not disputing your
comments, rather, just offering my own to an open discussion (not attacking
you personally as you have done to me).

>
>
> >> Moreover, ISTM that it may well be that an experienced NT+ batch
> >> programmer, needing special help from JS or VB to complete a job, might
> >> well still want to do as much as possible in batch, treating the JS/VB
> >> more or less as an imported utility.
> >>
> >> Then JS has abilities lacking in VB, and vice versa; unless WSH VB can
> >> call JS, it might useful to run JS & VB in turn from a batch,
> >> communicating via the parent environment; similarly if an imported
> >> utility needed assistance from JS or VB.
> >
> >As you said, this is a.m.b.*.nt. ;-)
>
> Indeed; but that is no reason for struggling with a complex and
> confusing batch-only solution in a case where WSH + VB/JS is a better
> tool.
>
> Most of those who ask real questions in News want solutions, preferably
> understandable ones, using any readily-available tool. Those who only
> like to demonstrate what they can achieve using only pure batch are mere
> show-offs.

Again, I was not disputing your comments, rather, agreeing with your earlier
comment. ISTM, discussion about passing data between VB and JS has nothing
to with pure batch or showing off to warrant your attack. :-(

0 new messages