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

Looking for documentation on new CMD (Win2K) batch language features

29 views
Skip to first unread message

Kenny McCormack

unread,
Jul 31, 2004, 2:34:15 PM7/31/04
to
I'm looking for a good docu on the new features. So far, I've gleaned some
from "if /?" and "for /?".

I know about: %var:~<start>,<length>%, which returns the substring of the
value of var. I also know about extensions in the FOR command.

I find it strange that all the goodies in FOR are only available in a FOR
statement - that is, you have to code a "fake" FOR statement in order to
use these features. It seems like it ought to be possible to use these
features in the normal language, but apparently, the only extension that
works there is the substring notation (described above).

Matthias Tacke

unread,
Jul 31, 2004, 4:48:53 PM7/31/04
to
Kenny McCormack wrote:
Hi Kenny

>I'm looking for a good docu on the new features. So far, I've gleaned
>some from "if /?" and "for /?".
>
Have a look at: XP> hh.exe ntcmds.chm::/ntcmds_new_tools.htm
. W2k and XP> hh.exe ntcmds.chm::/dos_diffs.htm

>I know about: %var:~<start>,<length>%, which returns the substring of
>the value of var. I also know about extensions in the FOR command.
>
>I find it strange that all the goodies in FOR are only available in a
>FOR statement - that is, you have to code a "fake" FOR statement in
>order to use these features. It seems like it ought to be possible to
>use these features in the normal language, but apparently, the only
>extension that works there is the substring notation (described above).

IMO you are wasting time if you complain about things you can't change.

To what are you referring? The ~ commands can be used with command line
arguments also. And the parsing features are most useful in a command
able to iterate through a set.

And cmd.exe is just a free example of a shell, there are alternatives
like 4nt and cygwin with bash.

Besides my signature some more links:

NT/W2K/XP------------------------------
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx#tophttp://www.labmice.net/articles/batchcmds.htm <http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx
http://www.ss64.com/nt/index.html
http://www.commandline.co.uk/lib/treeview/index.php
http://www.uwasa.fi/~ts/http/http2.html#cmdscript
http://home.comcast.net/~stewartb/cmdprompt.html
http://www.seanet.com/~shardy/ntscript.html
http://www.robvanderwoude.com/
http://www.jsiinc.com/reghack.htm
W2K>HH windows.chm::ntcmds.htm
XP>HH ntcmds.chm

Or search these groups. Replace "your+keywords" with your keywords

http://www.google.com/groups?group=alt.msdos.batch&q=your+keywords
http://www.google.com/groups?group=alt.msdos.batch.nt&q=your+keywords
http://www.google.com/groups?group=alt.msdos.batch.*&q=your+keywords
http://www.google.com/groups?group=microsoft.public.win2000.cmdprompt.admin&q=your+keywords

or directly open the advanced Group Search page with preentered group:
http://www.google.com/advanced_group_search?group=alt.msdos.batch
http://www.google.com/advanced_group_search?group=alt.msdos.batch.nt
http://www.google.com/advanced_group_search?group=alt.msdos.batch.*
http://www.google.com/advanced_group_search?group=microsoft.public.win2000.cmdprompt.admin

If you are using internet explorer as your browser take a look here:
http://www.commandline.co.uk/searchurl/index.html
where Ritchie Lawrence lined out how to ease searching a lot.

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm

Kenny McCormack

unread,
Jul 31, 2004, 8:49:18 PM7/31/04
to
In article <ceh0jl$v8s$06$1...@news.t-online.com>,

Matthias Tacke <Matthia...@web.de> wrote:
>Kenny McCormack wrote:
>Hi Kenny
>>I'm looking for a good docu on the new features. So far, I've gleaned
>>some from "if /?" and "for /?".

>Have a look at: W2k and XP> hh.exe ntcmds.chm::/dos_diffs.htm

Interesting. But this doesn't tell me about the new ~ options in variable
substitutions - which is what I am specifically interested in.

>>I know about: %var:~<start>,<length>%, which returns the substring of
>>the value of var. I also know about extensions in the FOR command.
>>
>>I find it strange that all the goodies in FOR are only available in a
>>FOR statement - that is, you have to code a "fake" FOR statement in
>>order to use these features. It seems like it ought to be possible to
>>use these features in the normal language, but apparently, the only
>>extension that works there is the substring notation (described above).
>
>IMO you are wasting time if you complain about things you can't change.

I was using the Usenet technique of asserting something in the knowledge
that if the assertion isn't correct, someone will surely explain why it is
wrong. Works well.

>To what are you referring? The ~ commands can be used with command line
>arguments also.

Really? Please explain.

>And the parsing features are most useful in a command able to iterate
>through a set.

So, you are confirming my belief that they only work when iterating, right?

>And cmd.exe is just a free example of a shell, there are alternatives
>like 4nt and cygwin with bash.

Understood - and I use CMD.EXE rarely, which explains why I am asking these
questions. But, as I'm sure everyone in this group is aware, the reason we
have to know about and work with CMD.EXE is because that's what the general
public uses.

Clay Calvert

unread,
Jul 31, 2004, 10:13:44 PM7/31/04
to
On Sun, 01 Aug 2004 00:49:18 GMT, gaz...@yin.interaccess.com (Kenny
McCormack) wrote:

>Interesting. But this doesn't tell me about the new ~ options in variable
>substitutions - which is what I am specifically interested in.

At the end of this post will be a crib sheet I put together a while
back to help me keep all the parsing straight.

>>To what are you referring? The ~ commands can be used with command line
>>arguments also.
>
>Really? Please explain.

Arguments passed can be referenced as %1 through %9, and behave
basically the same was as %x and %%x in FOR loops. Additional
arguments can be accessed individually using the SHIFT command. "%0"
refers to the name of the batch file itself... more specifically it is
the string that launched the batch file. So "%0" could be from
"BatchFile" to "d:\directoryname\batchfile.bat". Note that SHIFT will
change the value of "%0" to that of which was previously "%1".

I use the "~" commands very often with "%0". I usually have output
files match the name of the batch, except with a .TXT extension.

"%~dpn0.txt"

The above gives; "Drive\Path\BatchFile.txt"

The following does use a FOR loop, but the "~" functionality is
expanded before FOR processes it.

for /f "skip=2 tokens=2" %%a in (%~fs0) do net user %%a %%b
goto:eof
User1 PW1
User2 PW2
etc etc

The above uses the full path to the batch file in MS-DOS compatible
format (so there are no spaces requiring quotes) as input to the
batch. Some may not prefer this structure, but it does make the
scripts more portable.

One poster wanted to know how to tell if a batch was called from a
drive letter or a UNC path.

if "%~d0"=="\\" echo UNC Path.

So, there are quite a number of creative ways to use "~" outside of a
FOR loop.

---------- Parsing.txt -----------

The following are examples of variable substitution and parsing in
Windows NT, 2000 and XP.
A "#" character will indicate functionality only available in Windows
2000, and XP.

More information can be found from running "Set /?" and "For /?".

A "one-sided" variable is a single character variable that has a
single percent sign preceding the character.

Examples: %1 and %a


"Two-sided" variables can have more than one character and have a
percentage sign on both sides.
Examples: %temp% and %path%

The following string will be used for these examples:

set test=123456789abcdef0

Substitution [: and =] Two sided only

%PATH:str1=str2%
%PATH:str1=%
%PATH:*str1=str2%

%Test:ab=xy% 123456789xycdef0
%Test:ab=% 123456789cdef0
%Test:*ab=% cdef0
%Test:*ab=XY% XYcdef0


Parsing [%:~X% and %:~X,Y%] Two sided variables only

Extract only the first 5 characters
%test:~0,5%
12345

Skip 7 characters and then extract the next 5
%test:~7,5%
89abc

Skip 7 characters and then extract everything else
%test:~7%
89abcdef0

Extract only the last 7 characters #
%test:~-7%
abcdef0

Extract everything BUT the last 7 characters #
%test:~0,-7%
123456789

Extract between 7 from the front and 5 from the back #
%test:~7,-5%
89ab

Go back 7 from the end then extract 5 towards the end #
%test:~-7,5%
abcde

Extract between 7 from the end and 5 from the end #
%test:~-7,-5%
ab


[%~letter] One sided variables only

%~I removes any surrounding quotes (") #
%~fI Fully qualified path name
%~dI Drive letter only
%~pI Path only
%~nI file Name only
%~xI eXtension only
%~sI Short names only
%~aI Attributes #
%~tI Time and date of file #
%~zI siZe of file #

[%~$string]
%~$PATH:I searches the PATH and expands to the full name of the
first found

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:i - searches the PATH and expands to the drive letter and
path of the first found
%~ftzaI - expands %I to a DIR like output line #

In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

--------------------------------------------

HTH


Clay Calvert
CCal...@Wanguru.com
Replace "W" with "L"

Kenny McCormack

unread,
Aug 1, 2004, 8:29:13 AM8/1/04
to
In article <uaiog0tc9rgjfus35...@4ax.com>,
Clay Calvert <ccal...@Wanguru.com> wrote:
...

>At the end of this post will be a crib sheet I put together a while
>back to help me keep all the parsing straight.

Great. Looks good. The Usenet working like it used to.

>>>To what are you referring? The ~ commands can be used with command line
>>>arguments also.
>>
>>Really? Please explain.
>
>Arguments passed can be referenced as %1 through %9, and behave
>basically the same was as %x and %%x in FOR loops. Additional
>arguments can be accessed individually using the SHIFT command. "%0"
>refers to the name of the batch file itself... more specifically it is
>the string that launched the batch file. So "%0" could be from
>"BatchFile" to "d:\directoryname\batchfile.bat". Note that SHIFT will
>change the value of "%0" to that of which was previously "%1".

OK, so you are saying that the "FOR goodies" work in exactly two places:
in FOR statements and with the "positional parameters" (as they are
generally known). But nowhere else, right?

Todd Vargo

unread,
Aug 1, 2004, 11:42:37 AM8/1/04
to

"Kenny McCormack" <gaz...@yin.interaccess.com> wrote:
> OK, so you are saying that the "FOR goodies" work in exactly two places:
> in FOR statements and with the "positional parameters" (as they are
> generally known). But nowhere else, right?

No that is not was said, but your take on it is correct.

--
Todd Vargo (remove hyphen to reply by email)


Matthias Tacke

unread,
Aug 1, 2004, 4:48:09 PM8/1/04
to
Kenny McCormack wrote:
>Matthias Tacke <Matthia...@web.de> wrote:
>>Kenny McCormack wrote:
>>Hi Kenny
>>>I'm looking for a good docu on the new features. So far, I've
>>>gleaned some from "if /?" and "for /?".
>
>>Have a look at: W2k and XP> hh.exe ntcmds.chm::/dos_diffs.htm
>
>Interesting. But this doesn't tell me about the new ~ options in
>variable substitutions - which is what I am specifically interested in.
>
There are still some enhancements compared with dos in
cmd /?
set /?
call /?
setlocal /?

As the nice compilation from Clay shows, the file relevant variable
substitutions work with one sided variables, while the substitution
with [: and =] work only with two sided variables but then anywhere.
And the latter isn't a for goodie.

>>>I know about: %var:~<start>,<length>%, which returns the substring of
>>>the value of var. I also know about extensions in the FOR command.
>>>
>>>I find it strange that all the goodies in FOR are only available in a
>>>FOR statement - that is, you have to code a "fake" FOR statement in
>>>order to use these features. It seems like it ought to be possible
>>>to use these features in the normal language, but apparently, the
>>>only extension that works there is the substring notation (described
>>>above).
>>
>>IMO you are wasting time if you complain about things you can't
>>change.
>
>I was using the Usenet technique of asserting something in the
>knowledge that if the assertion isn't correct, someone will surely
>explain why it is wrong. Works well.
>

I never hesitate to do so, but you're constantly staying unclear.

>>To what are you referring? The ~ commands can be used with command
>>line arguments also.
>
>Really? Please explain.
>

Clay did that in detail.

>>And the parsing features are most useful in a command able to iterate
>>through a set.
>
>So, you are confirming my belief that they only work when iterating,
>right?

Since I don't know what you really mean with *the* goodies and with what
you do compare it's difficult.

>>And cmd.exe is just a free example of a shell, there are alternatives
>>like 4nt and cygwin with bash.
>
>Understood - and I use CMD.EXE rarely, which explains why I am asking
>these questions. But, as I'm sure everyone in this group is aware,
>the reason we have to know about and work with CMD.EXE is because
>that's what the general public uses.

Don't get me wrong. 4nt and cygwin/bash are good solutions also
affording time to get used to. I prefer to realize tasks with pure batch,
if necessary in combination with external commands (if possible free) or
wsh/vbs/js if present in the given environment.

In the group links of my first answer, nearly every problem was solved,
reading for some time or sieving through with google will reveal that.

If you want a positive answer, that the special enhancements to for only
work with for, I can agree even if this is a bit redundant.

guard

unread,
Aug 8, 2004, 5:54:40 PM8/8/04
to
"Kenny McCormack" wrote

See "TheGuardBook" at (http://TheSystemGuard.com/TheGuardBook/CCS-Int) for a
complete list of all internal commands included in the default command
interpreter "Cmd.Exe". Each "Mounted Help Page" is a color-keyed, merged
help page showing the differences between NT4, 2000, XP and Server 2003.

For a much more extensive list of what can be done using ONLY the native
commands and utilities, try the FREE Advanced Command Library at
(http://ntlib.com) which includes over 200 additional resources.

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)


0 new messages