Batch Programming Woes. OS Checker?

T

Tim Snider

I have created a batch file to set a variable for different OS's. (Other
than the Standard Ones)
REM Begin~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo off
cls
echo Stand by inspecting your system.
echo.
echo OS check
:check1
for /f "usebackq tokens=1,2,3" %%i in (`ver`) do set OSVer=%%i %%j %%k

set
Pause
echo This is your Operating System: %OSVer%
pause

echo.
IF % %OSVer%%==Microsoft Windows XP goto Win2k
goto WrongOS

:Win2k
echo Your system is %%OSVer%%
pause


:WrongOS
echo OS not supported.
pause

exit
REM End~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now the "For" line creates a string like Microsoft Windows 2000 and sets a
variable called OSVer.
When I run %%OSVer%% from within the batch file it returns "Microsoft
Windows 2000" string ok.

Problem lies in the IF statement
Running the batch file it ends the batch file and I can't see what the error
is.

Then I tried to trouble shoot this from the command prompt,

I type the for statement to set the variable.
for /f "usebackq tokens=1,2,3" %i in (`ver`) do set OSVer=%i %j %k

I type the "Set" command and view the variables and OSVer is set correctly.

I then type the IF statement as shown below just to test it out

IF %OSVer%==Microsoft Windows 2000 echo OK

I then get an error saying that "Windows was unexpected at this time"

Can anyone tell me what is wrong with this statement? And why I am getting
this error?
 
P

Phil Robyn

Tim said:
I have created a batch file to set a variable for different OS's. (Other
than the Standard Ones)
REM Begin~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo off
cls
echo Stand by inspecting your system.
echo.
echo OS check
:check1
for /f "usebackq tokens=1,2,3" %%i in (`ver`) do set OSVer=%%i %%j %%k

set
Pause
echo This is your Operating System: %OSVer%
pause

echo.
IF % %OSVer%%==Microsoft Windows XP goto Win2k
goto WrongOS

:Win2k
echo Your system is %%OSVer%%
pause


:WrongOS
echo OS not supported.
pause

exit
REM End~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now the "For" line creates a string like Microsoft Windows 2000 and sets a
variable called OSVer.
When I run %%OSVer%% from within the batch file it returns "Microsoft
Windows 2000" string ok.

Problem lies in the IF statement
Running the batch file it ends the batch file and I can't see what the error
is.

Then I tried to trouble shoot this from the command prompt,

I type the for statement to set the variable.
for /f "usebackq tokens=1,2,3" %i in (`ver`) do set OSVer=%i %j %k

I type the "Set" command and view the variables and OSVer is set correctly.

I then type the IF statement as shown below just to test it out

IF %OSVer%==Microsoft Windows 2000 echo OK

IF "%OSVer%"=="Microsoft Windows 2000" . . . .
 
A

Al Dunbar

You know, I was almost going to get this FREE download thing just to see if
it might be anything near what your claims say it is. Thing is, as I clicked
around the site to find this free subset of the full package, I never did
find a link to actually download it. I just kept getting shown the full
package with its "almost free" pricetag. What is this, bait and switch?

/Al
 
B

Bill Stewart

Al Dunbar said:
You know, I was almost going to get this FREE download thing just to see
if it might be anything near what your claims say it is. Thing is, as I
clicked around the site to find this free subset of the full package, I
never did find a link to actually download it. I just kept getting shown
the full package with its "almost free" pricetag. What is this, bait and
switch?

Hi Al,

I added this spammer's address to my "blocked senders" list a long time
ago. I sort of got tired of seeing this "I'm the best script author ever"
attitude and a lack of an actual name. Plus, I think it's funny that he's
actually trying to sell batch files, none of which provide capabilities
that one can get elsewhere for free. It seems to me that when people post
questions here, they get very helpful answers from people like Phil Robyn,
Marty List, Jerry Schulman, and others.

FWIW,

Bill
 
A

Al Dunbar [MS-MVP]

Bill Stewart said:
Hi Al,

I added this spammer's address to my "blocked senders" list a long time
ago. I sort of got tired of seeing this "I'm the best script author ever"
attitude and a lack of an actual name. Plus, I think it's funny that he's
actually trying to sell batch files, none of which provide capabilities
that one can get elsewhere for free.

To be brutally honest, the only reason I have any interest in seeing his
stuff is to see if reading his code would give any insight as to what he
actually knows about batch and coding in general. You certainly don't get
the impression from his posts that he is capable of discussing *BATCH*
itself, as his only posts are ads (and annoying ones at that) for his
products. I don't remember him ever getting involved in any real
bi-directional discussion, and he certainly has not provided any help of
the: "this is how it works" variety.
It seems to me that when people post
questions here, they get very helpful answers from people like Phil Robyn,
Marty List, Jerry Schulman, and others.

Actually almost insulting to mention those venerables, even in comparison,
to Mr. booming and fuming.

/Al
 
K

Kevin Gould

Here's a tip to clean up your code a bit

Frequently you'll be trying to grab multiple fields with a for loop. Don't
treat them as multiple fields. Indicate a delimiter of a character you'll
never see.

C:\temp> for /f "delims=!" %%a in ('ver') do set OSVER=%%a
 
A

Al Dunbar

guard said:
Instructions for obtaining any of our FREE products are at

(http://TheSystemGuard.com/Contact.htm)

*******

As a clarification:

The Advanced Library (ntlib.cmd) is Copyrighted Freeware for commercial or
personal use. It contains no nagware, spyware or any such thing. We find
that it is a very useful introduction to the power of "Mounted Scripting" as
opposed to the way it is normally done with hundreds (thousands?) of special
cases to remember. These are like APIs for shell scripting with all
extended commands being contained within a single file and NO BINARY CODE
being added to a standard NT, 2K, XP or K3 installation.

OK, so I bit the bullet and got a copy (please don't shoot me, guys). I
figure: "hey, I have often thought it would be nice to dump all of the
useful little batch tools I have ever created into a single batch file to
reduce the constant wheel-reinventing, maybe these guys have successfully
done it".

Well, perhaps they have. The code is pretty dense, almost to the point where
it might as well be encrypted;-) Unfortunately (or perhaps fortunately) I do
not have the time to fully evaluate it, mainly because it seems such an
all-or-nothing kind of proposition to use.

They have followed a number of coding conventions designed to avoid conflict
with the user's coding practices. Since I routinely use "_" as a prefix to
variable names, however, I would need to change my ways in order to best use
the product. Sorry, but it won't happen (and that is not the only reason).

I will say this: a lot of work has gone into the development of this thing.
The fact that it works at all likely means they have used some sort of
regression testing to verify it in many combinations. By the same token, I
suppose one could start using it to develop a bit of code to run on the
various platforms and just wait to see if it abends at some point. But that
is a risk whose potential benefits (at least in my case) far underweigh that
risk.

But what about the newbie who is flummoxed by the arcane nature of batch
programming, would he find it beneficial? Perhaps. But it seems to me the
cost is the learning yet another layer of protocol, and proprietary protocol
at that. And since it sits on top of batch, the coder still needs to be able
to understand the code he is writing.

"But does it work?" I hear some of you asking (the rest are likely saying:
"AAAAAAAAAArgh"). I tried a couple of functions and did not find any that
failed to work in the simple case. I suppose we need to take it on faith
that, since the producer likens this to the famous sliced bread, that it is,
indeed bullet-proof. Perhaps this explains why it generates errors when
displaying its own help text:

C:\Documents and Settings\Al\Desktop\ntlib>cmd /c >argc ntlib /C
FINDSTR: Cannot open C:\Documents
FINDSTR: Cannot open and
FINDSTR: Cannot open Settings\Al\Desktop\ntlib\ntlib.cmd
FINDSTR: Cannot open C:\Documents
FINDSTR: Cannot open and
FINDSTR: Cannot open Settings\Al\Desktop\ntlib\ntlib.cmd

<snipped five more sets of error messages>

I dunno. I guess I could look at the code to see where it has forgotten that
its own location might be at a path containing blanks (a common enough error
for a newbie). But somehow I don't think I will. But I do wonder if similar
issues are present elsewhere.

Perhaps the real question, already answered by ng members who now routinely
ignore postings from "the guard", is: does this have a place in this
newsgroup?

I think not. Anyone can post a solution here that refers to some third-party
product available on the web, including ntlib. But such posts should *NOT*
(imho) be simply ads for their product. Their web site declares that they do
not participate in spamming, but that is what most of their posts here seem
to be.

The originators of this "product" obviously have some knowledge of batch
programming plus a lot of stick-to-it-iveness. If they were to engage in
discussions here with the purpose of sharing this knowledge (as many others
freely do), I for one would welcome their contributions. Assuming, of
course, that they would shelve their obvious superiority complex when doing
so.

I suspect that, to many posters here, my observations come as no surprise.
Some might ask why I even bothered, as the postings of "the guard" have been
considered by so many as OT. Well, I apologize in advance if this post has
any unfortunate effect on the signal to noise ratio here. The fact is,
however, that you cannot always just assume that a product is not useful
simply because you find the commercials annoying.

/Al
 
P

Phil Robyn

Jerry said:
Phil,




Wouldn't it be better to use some other character than the quotation marks?
Like {%OSVer%}=={Microsoft Windows 2000}? I've often run into problems with
using the quotation marks as they are reserved characters.

In this particular case, I guess it doesn't matter. But how about the following?

<Win2000> c:\cmd>c:\temp\testinput "now is the time"
it matches.

<Win2000> c:\cmd>c:\temp\testinput now is the time
is was unexpected at this time.

<Win2000> c:\cmd>rlist c:\temp\testinput.cmd
=====begin c:\temp\testinput.cmd ====================
1. @echo off
2. if [%*] equ ["now is the time"] echo it matches.
=====end c:\temp\testinput.cmd ====================
 
P

Phil Robyn

Jerry said:
Phil,

This is precisely my point. *8^)

In this particular case, I guess it doesn't matter. But how about the
following?

<Win2000> c:\cmd>c:\temp\testinput "now is the time"
it matches.

<Win2000> c:\cmd>c:\temp\testinput now is the time
is was unexpected at this time.

<Win2000> c:\cmd>rlist c:\temp\testinput.cmd
=====begin c:\temp\testinput.cmd ====================
1. @echo off
2. if [%*] equ ["now is the time"] echo it matches.
=====end c:\temp\testinput.cmd ====================


If you replace the enclosing [] above with "" you get the same error: 'is
was unexpected at this time.'

if "%*" equ ""now is the time"" echo it matches. <--- this will generate an
error.

The point that I was trying to make is that it is best to avoid quotation
marks as enclosures in If statements. *8^)

Cordially yours,
Jerry G. Young II

But what about the following? No matches. :-(

<Win2000> c:\cmd>c:\temp\testinput abcd

<Win2000> c:\cmd>c:\temp\testinput abcd efgh

<Win2000> c:\cmd>c:\temp\testinput "abcd efgh"

<Win2000> c:\cmd>type c:\temp\testinput.cmd
@echo off
if !%1! equ !abcd efgh! echo OK.
 
A

Al Dunbar [MS-MVP]

Jerry G. Young II said:
Phil,



Wouldn't it be better to use some other character than the quotation marks?
Like {%OSVer%}=={Microsoft Windows 2000}? I've often run into problems with
using the quotation marks as they are reserved characters.

But the quotation marks are reserved for just such purposes as to allow
commands to deal with strings of characters containing whitespace as if they
were a single entity. Whether this is a filename or an arbitrary string
being tested in an IF statement.

By the same token, if variables may take almost any value, then any
arbitrary non-special character you might use for a delimiter in an IF
statement (i.e. "{" and "}") could, conceivably, appear. Since the double
quote *is* a special character, I figure it is *less* likely to appear in a
variable, simply because of the problems associated with doing so.

That said, one still needs to take into account the possibility that a batch
parameter was passed in double-quoted form.

/Al
 
G

guard

OK, so I bit the bullet and got a copy (please don't shoot me, guys). I
figure: "hey, I have often thought it would be nice to dump all of the
useful little batch tools I have ever created into a single batch file to
reduce the constant wheel-reinventing, maybe these guys have successfully
done it".

This is precisely the reason that we developed our Command Libraries and
Mount/\Command Set. Initially, for our own use in a large corporate
setting, and then for our clients in businesses of all sizes.
(see http://TheSystemGuard.com/SAProfiles.htm)
Well, perhaps they have. The code is pretty dense, almost to the point where
it might as well be encrypted;-)

The density of the code comes from our !Compress.cmd Script. This script
essentially takes a fully commented source code file (.cmdsrc) and removes
(almost) everything that is not essential for the computer to run the
script. We will be releasing a FREE Advanced Version(TSG_Compress.cmd) and
an "Almost Free" Expert Version (!Compress.cmd) of this script in the near
future.

The Expert Version will make use of our "Extreme Mount/\Command Set". These
are commands designed solely to reduce the size and to increase the speed of
the script. They are present in all of our "Almost Free" products and some
of our more experienced clients use them for their own scripts.

Here is the sample output from a recent build using !Compress.cmd:

------------------------------------------------------------------------
!Compress Release 2003.xx.xx {beta}
------------------------------------------------------------------------
Source File: NTCmdLib.cmdsrc
Destination File: NTCmdLib.cmd
Total Lines In Source: 4734
Blank Lines: 1106
Comment Lines: 1127
Lines In Destination: 2502 {before multiline compression}
: 1685 {after multiline compression}
Total Lines Removed: 3049
Source File Size: 233595
Destination File Size: 133221
Compression (Lines): 64%
Compression (Bytes): 42%
Embedded (Lines): 5521
Embedded (Bytes): 224008
Final Size On Disk: 357229
________________________________________________________________________
[Thu 08/30/2003 04:21:08] !Compress - Task Complete.!

Unfortunately (or perhaps fortunately) I do
not have the time to fully evaluate it, mainly because it seems such an
all-or-nothing kind of proposition to use.

Actually, since there is no "installation" and the product is entirely
contained within a SINGLE FILE, it's quite easy to use it or not, as the
situation may require.

Many of our clients initialize the library from a READ-ONLY network share,
requiring no access to the local disks. The Mount/\Commands are loaded into
the current environment, so unless you use one that requires temporary
files, you never touch the local hard disk.
They have followed a number of coding conventions designed to avoid conflict
with the user's coding practices. Since I routinely use "_" as a prefix to
variable names, however, I would need to change my ways in order to best use
the product. Sorry, but it won't happen (and that is not the only reason).

As you noted, we have taken VERY CAREFUL CONSIDERATION to avoid creating
conflicts between your scripts and the library script code. The variables
that are prefixed with "_" are only used as the output of $FUNCTIONs and
:procedures.
(see http://TheSystemGuard.com/NTCmdLib.asp#LibraryArchitecture)

Unless you modify a specific "_" prefixed variable used by a FUNCTION or
Procedure you are calling, the library will NOT interfere with your code.
The "_" prefixed variables are not cleared until you unload the library with
"NTCmdLib /u". Since most "Mounted Scripts" follow the format

CALL NTCmdLib /init
 
A

Al Dunbar

guard said:
This is precisely the reason that we developed our Command Libraries and
Mount/\Command Set. Initially, for our own use in a large corporate
setting, and then for our clients in businesses of all sizes.
(see http://TheSystemGuard.com/SAProfiles.htm)

Just lose the attitude in your posts and I will stop complaining. Maybe you
will be removed from untold kill-files.

/Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top