problem when using set /p var=

J

James Pang

Hi,

I want to use below command to set the output string of find into
variable 'v'
ver | find "Windows" | set /p v=

but it doesn't work!!! anybody has any idea?
 
T

Timo Salmi

James said:
I want to use below command to set the output string of find into
variable 'v' ver | find "Windows" | set /p v=
but it doesn't work!!! anybody has any idea?

That is a problem that is a superficially easy

@echo off & setlocal enableextensions
if [%OS%]==[Windows_NT] echo NT/2000/XP...
for /f "tokens=2 delims=[]" %%v in ('ver') do set ver_=%%v
echo %ver_%
endlocal & goto :EOF

Which would give e.g.
C:\_D\BAS>cmdfaq
NT/2000/XP...
Version 5.1.2600

However, that is somewhat of a circular deduction. We already have
advance knowledge what the OS is when using the above! Note that
'ver' output fields can differ between Windows version.

The more fundamental question is how does one generally find out in a
script which OS? That problem has been tackled by Ritchie Lawrence in

http://www.commandline.co.uk/lib/Batch Function Library/System Functions/GetOS.html

All the best, Timo
 
T

Timo Salmi

Timo said:
The more fundamental question is how does one generally find out in a
script which OS? That problem has been tackled by Ritchie Lawrence

Here is my rendition, partly incomplete and (naturally) not tested
for all the OSes.

@echo off
set my_os=
::
echo Test|find "Fail">nul
if errorlevel 0 if not errorlevel 1 set my_os=Pre MS-DOS 6.0
if not "%my_os%"=="" goto _show
::
ver|find "6.22">nul
if errorlevel 0 if not errorlevel 1 set my_os=MS-DOS 6.22
::
ver|find "Windows 95.">nul
if errorlevel 0 if not errorlevel 1 set my_os=Windows95
::
if not "%OS%"=="Windows_NT" goto _show
::
net config workstation|find /i "Software version"|find "NT40">nul
if errorlevel 0 if not errorlevel 1 set my_os=Windows NT 4.0
::
net config workstation|find /i "Software version"|find "2000">nul
if errorlevel 0 if not errorlevel 1 set my_os=Windows 2000
::
net config workstation|find /i "Software version"|find "2002">nul
if errorlevel 0 if not errorlevel 1 set my_os=Windows XP
::
net config workstation|find /i "Software version"|find "2003">nul
if errorlevel 0 if not errorlevel 1 set my_os=Windows 2003
::
:_show
if "%my_os%"=="" set my_os=Unknown
echo %my_os%
for %%v in (my_os) do set %%v=

All the best, Timo
 
M

Mike Jones

Timo said:
Here is my rendition, partly incomplete and (naturally) not tested
for all the OSes.

@echo off
set my_os=
::
echo Test|find "Fail">nul
if errorlevel 0 if not errorlevel 1 set my_os=Pre MS-DOS 6.0
if not "%my_os%"=="" goto _show

Not sure about that. anyway, the thing to test (for "proper DOS" and maybe
it works for later versions?) is the response from DOS function 33h int 21h
(ref DOSVER by William Allen in news:alt.msdos.batch passim)
::
ver|find "6.22">nul
if errorlevel 0 if not errorlevel 1 set my_os=MS-DOS 6.22
::
ver|find "Windows 95.">nul
if errorlevel 0 if not errorlevel 1 set my_os=Windows95
::

erm I think you missed Windows 98 (and 98SE) here!
if not "%OS%"=="Windows_NT" goto _show
::
well I'd test if %OS% was non-null first to discriminate the NT line from
the DOS versions (ie upto WME)
 
H

Herbert Kleebauer

Mike said:
Not sure about that. anyway, the thing to test (for "proper DOS" and maybe
it works for later versions?) is the response from DOS function 33h int 21h
(ref DOSVER by William Allen in news:alt.msdos.batch passim)

Also in Windows it the best to directly ask the operating system
and not to rely on the behavior of cmd.exe or other programs.
The following batch will work from DOS 5 (for older DOS versions
you have to replace the "call gver.bat") till the latest
Windows version:

@echo off
echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>gver.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>gver.com
echo T}{zs~@gwkCG@OEKcUt`s}@mqqCsy?seHCsaPhxr?@zAB`LrPEyoDt@Cj?>>gver.com
echo pky_jN@QEKpEt@ij?jySjN@REKpEt@jj?jyGjN@SEKkjtlGuNw?p@pjirz>>gver.com
echo VRvAZYX??YLtX@@?~XCo?V~rDZ@?bZ?@MV@?os1GG`Lb~eafgXzoG?Ca~g>>gver.com
echo O1wG?w?ygF?fyALHsq@w`G`LKLsdrTdu_Sl1qTni`To=q@ooo?ooo?ILo?>>gver.com
echo sdrTdu_Sl1qTnmhTo=q@ooo?ooo?ILo?sdrTdu_Sa1qTkhtTo=c@ooo?oo>>gver.com
echo o?ILo?sdrTdu_So1qTs`kTe=ePeeeTeeeTILe@sdrTdu_Sb1qT=crDxc__>>gver.com
echo @?AooG_B?@?~NcC?fOCGf?SCG_K_aFDsFBkzZBUKDOKS@KVCO?@OudxH_F>>gver.com
echo Zo@N??D@J?C?K?UHZoOXO?Y_X?OV?@VAX?EZC?FZC?CX_?BDZ?OCZ?YOV?>>gver.com
echo OVO?ODZ?[IZ?{VO?ORZCZ[V?smYOswdTAoXGXOX?CXC?_LZ?Z_VK1Z~rZO>>gver.com
echo k@O[DGOiVGOyVGOEVKcDZOHZO?XOW@GVO_HZO?VOy@ZOG?RTTSrQDDcmqO>>gver.com
echo VkkDZOk@dLFS`rrTAdfT@wnTDJ?SDMQTqrK@kcmS[Vk`iVO_yVO_EVOoFZ>>gver.com
echo O?hwDTqOsTdbnTXrrDsdFTcsRTm`GTdkcTqVYSdshTkhETFXdPUsdTrqdT>>gver.com
echo mnhT@wDT[g?C??QCST~B??OCPU}A~??`?Q_OC=?GcgXkFJXGCFBMX_gFmt>>gver.com
echo LtXQgJg}H}iXJQT~?K?OGOdr?Su_sP1qdTi`lT=qnDdr?Su_sP1qdTmhlT>>gver.com
echo =qnDdr?Su_sP1qdThtaT=ckDdr?Su_sP1qdT`koT=esDdr?Su_sP1qdTcr>>gver.com
echo bTU?=OXGxAPM}I@??pGC?ba?Azy?NwBuHCfA?MoA?MEUGQ}gan?PMQLg?K>>gver.com
echo B]XsLFZ?I?I_GEECFs?FXBgBBqj~QEaAp??@DB?B?QSOLt?C~ti=OKT?b?>>gver.com
echo ?`?QSO?i?CQWg@i??PEg@C??QCT~OL?OOO?H?oi[tPFg?C??QCQFg@i??P>>gver.com
echo T~?K?O?O?i?CGT~B??OCW=@A??QCtX@O?BWNhqvT_dsDqqdTZqnDZSHGaZ>>gver.com
echo ~r0x>>gver.com

gver.com>gver.exe
gver.exe>gver.bat
call gver.bat
for %%i in (com exe bat) do del gver.%%i

echo PlatformId=%ver_platf%
echo MajorVersion=%ver_major%
echo MinorVersion=%ver_minor%
echo BuildNumber=%ver_build%
echo CSDVersion=%ver_csd%


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
For DOS:

dwPlatformId=ffffffff


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FOR Windows:


dwMajorVersion
--------------
Major version number of the operating system. This member can be one of
the following values.

Operating System Meaning
Windows 95 4
Windows 98 4
Windows Me 4
Windows NT 3.51 3
Windows NT 4.0 4
Windows 2000 5
Windows XP 5
Windows Server 2003 family 5

dwMinorVersion
--------------
Minor version number of the operating system. This member can be one of
the following values.

Operating System Meaning
Windows 95 0
Windows 98 10
Windows Me 90
Windows NT 3.51 51
Windows NT 4.0 0
Windows 2000 0
Windows XP 1
Windows Server 2003 family 2


dwBuildNumber
-------------
Build number of the operating system.

Windows Me/98/95: The low-order word contains the build number of the
operating system. The high-order word contains the major
and minor version numbers.

dwPlatformId
------------
Operating system platform. This member can be one of the following values.

Value Meaning
0 VER_PLATFORM_WIN32s Win32s on Windows 3.1.
1 VER_PLATFORM_WIN32_WINDOWS Windows 95, Windows 98, or Windows Me.
2 VER_PLATFORM_WIN32_NT Windows NT, Windows 2000, Windows XP,
or Windows Server 2003 family.

szCSDVersion
------------
Pointer to a null-terminated string, such as "Service Pack 3", that
indicates the latest Service Pack installed on the system. If no Service
Pack has been installed, the string is empty.

Windows Me/98/95: Pointer to a null-terminated string that indicates
additional version information. For example, " C"
indicates Windows 95 OSR2 and " A" indicates Windows 98
Second Edition.


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


The source code:

dosmain:move.w s6,-(sp)
move.w (sp)+,s0
move.b #$30,m0
trap #$21
move.b m0,r1
move.l #_text+20,r6
bsr.w bin2hex
move.b r1,r0
bsr.w bin2hex
move.b m3,r0
bsr.w bin2hex

move.w #_text,r1
move.b #$09,m0
trap #$21
move.w #$4c01,r0
trap #$21

_text: dc.b "set ver_major=00000000",$0d,$0a
dc.b "set ver_minor=00000000",$0d,$0a
dc.b "set ver_build=00000000",$0d,$0a
dc.b "set ver_platf=ffffffff",$0d,$0a
dc.b "set ver_csd= ",'$'

bin2hex:move.w #2,r2
lsl.w #8,r0
_10: eor.b r0,r0
rol.w #4,r0
and.b #$0f,r0
add.b #$90,r0
adj_dec_add r0
addc.b #$40,r0
adj_dec_add r0
or.b #$20,r0
move.b r0,(r6.w)
inc.w r6
dbf.w r2,_10
addq.w #22,r6
rts.w



winmain::
move.l #VersionInfo,-(sp)
jsr.l (GetVersionExA)

move.l #text1,r5
move.l #VersionInfo+4,r6

move.l #4,r4
_10: bsr.l putstring
move.l (r6),r0
addq.l #4,r6
bsr.l hexout_r0
dec.l r4
bne.b _10
bsr.l putstring
move.l r6,r5
bsr.l putstring

moveq.l #0,-(sp)
jsr.l (ExitProcess) ; exit program

text1: dc.b "set ver_major=",0
text2: dc.b "set ver_minor=",0
text3: dc.b "set ver_build=",0
text4: dc.b "set ver_platf=",0
text5: dc.b "set ver_csd=",0

hexout_r0:
move.l r5,-(sp)
move.l #8,r2
move.l #_buf,r5
_20: rol.l #4,r0
move.b r0,r1
and.b #$0f,r1
cmp.b #9,r1
bls.b _10
add.b #'a'-10-'0',r1
_10: add.b #'0',r1
move.b r1,(r5)
inc.l r5
dbf.l r2,_20

move.l #_buf,r5
bsr.l putstring
move.l (sp)+,r5
rts.l
_buf: blk.b 8,0
dc.b $0d,$0a,0


putstring:
_20: move.b (r5),r0
inc.l r5
or.b r0,r0
beq.b _10
bsr.l putc
br.b _20
_10: rts.l

putc: move.b r0,_buf
eor.l r0,r0
add.l _handle,r0
bne.b _10
moveq.l #-11,-(sp)
jsr.l (GetStdHandle)
move.l r0,_handle
_10: moveq.l #0,-(sp)
move.l #_count,-(sp)
moveq.l #1,-(sp)
move.l #_buf,-(sp)
move.l r0,-(sp)
jsr.l (WriteFile)
or.l r0,r0
bne.b _20
_30: moveq.l #0,-(sp)
move.l #_text,-(sp)
move.l #_text,-(sp)
moveq.l #0,-(sp)
jsr.l (MessageBoxA)
moveq.l #0,-(sp)
jsr.l (ExitProcess)
_20: cmp.l #1,_count
bne.b _30
rts.l

_buf: dc.b 0
_text: dc.b 'write error',0
even4
_handle:dc.l 0
_count: dc.l 0

VersionInfo:
dc.l 148
blk.l 4
blk.b 128
 
T

Timo Salmi

Not sure about that.

It works at least under MS-DOS 5.0. That I tested.
anyway, the thing to test (for "proper DOS" and maybe it works for
later versions?) is the response from DOS function 33h int 21h

It has to be kept simple. When we do not know in advance which OS, we
do not know what batch tools and syntax we actually have available.
erm I think you missed Windows 98 (and 98SE) here!

True. "Here is my rendition, partly incomplete".

I am not sure what Windows 98 return for ver, even if I have a guess.

P.S. I had to drop alt.msdos.batch which you added (I agree) because
the newsserver I am currently on does not allow posting to newsgroups
that have here different servers.

All the best, Timo
 

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