Board logo

标题: (已结,改用set)Strings无法在NT环境下设置环境变量 [打印本页]

作者: myauto     时间: 2004-10-8 00:00    标题: (已结,改用set)Strings无法在NT环境下设置环境变量

如果输入strings left wang,2回车会得到结果wa
现在我想将wa赋到变量x上,使用如下语句
strings x=left wang,2
echo %x
但是奇怪的是得到的结果是
C:\>strings x=left wang,2
C:\>echo x
x
请问为什么这里显示的x不是wa?
多谢

---------- Edited by willsort  ----------
修改原标题:用过Strings的高手请进
---------- Edited by willsort ----------


[ Last edited by willsort on 2005-9-22 at 19:00 ]
作者: Wengier     时间: 2004-10-9 00:00
這不是Strings的問題,而是你對使用DOS環境變量並不熟悉。出現這個結果是正常的,"echo x"不顯示x才怪呢。建議你看看DOS入門方面的教材,了解一下DOS環境變量的使用方法。
作者: myauto     时间: 2004-10-9 00:00
可否再点拨一下,这里到底应该用什么语句才可以正确显示结果,还有Dos环境变量的使用文档那里有???非常感谢!!
作者: myauto     时间: 2004-10-9 00:00
另外,还找到一片关于strings的文章,似乎说strings不能用在win2000

---------- Edited by willsort  ---------
To  myauto:
  引用原文时,请最好使用论坛代码[quote]和[/quote];如果是网络资料,请最好给出原文链接。
  谢谢合作!

  Quote:
Making it work under Windows NT and Windows 2000STRINGS and ASET (and most other environment manipulation utilities) do not work under Windows NT and Windows 2000, even though they work as advertised in a DOS session or a DOS window under Microsoft Windows 9x. Why is this, and can it be corrected?Last question first: No, the problem cannot be corrected. Though Windows 2000 may complain "not enough environment space" when running STRINGS or ASET, the problem is not due to insufficient environment space. The problem is caused by the fact that STRINGS and ASET both use the DOS interrupt call INT 21 to alter environment variables. Windows NT and Windows 2000 are incompatible with this common technique. Here are some alternate solutions:Windows 2000: Get input from keyboardTo prompt the user for a value and then put their keyboard reply into an environment variable, run this command:
SET /P VARNAME=Please enter your name:

SET will display the prompt "Please enter your name:" to the user, who is expected to type a string of lettes and press ENTER. Whatever they type will be stored in the variable %VARNAME, where it can be retrieved for later operations.Windows NT: Get input from keyboardThis technique doesn't work with Windows NT. However, Simon Sheppard (www.ss64.com) wrote this script using Windows Scripting Host (present on NT and Win2K machines) to store user input into an environment variable:
:: Input1.cmd - A routine to prompt the user for an input string.
:: Requires WSH version 1.0 or later.
:: Input string is stored in the variable v_input
:: Use optional command line argument UCASE to convert input to all uppercase.
:: usage:
::   input1
::   input1 ucase
@ECHO off
SETLOCAL
SET v_vbs=%TEMP%\~tmp.VBS
SET v_cmd=%TEMP%\~tmp.cmd
ECHO Set oFS=CreateObject("Scripting.FileSystemObject")>%v_vbs%
ECHO oFS.OpenTextFile("CON",2).Write "Enter a string: ">>%v_vbs%
ECHO S=%1(Trim(oFS.OpenTextFile("CON",1).Readline))>>%v_vbs%
ECHO oFS.CreateTextFile("%v_cmd%",2).Write "set v_input=">>%v_vbs%
ECHO oFS.OpenTextFile("%v_cmd%",8).Write S>>%v_vbs%
cscript.exe //nologo %v_vbs%
CALL %v_cmd%
DEL %v_vbs%
DEL %v_cmd%
SET v_input
ENDLOCAL & SET v_input=%v_input%
One workaround is to place the command(s) to these utilities in the file called C:\WINNT\AUTOEXEC.NT or C:\%WINDIR%\AUTOEXEC.NT. The file AUTOEXEC.NT is a "batch" file and it runs like AUTOEXEC.BAT does in normal MS-DOS. If you are able, execute ASET or STRINGS from within AUTOEXEC.NT. Every time CMD.EXE or COMMAND.COM is executed, AUTOEXEC.NT will be executed, and any variables which are set there will persist for the life of the CMD or COMMAND window.There's probably another workaround, but I don't know what it is right now. If someone else does, please e-mail me. Thanks!

---------- Edited by willsort ----------


[ Last edited by willsort on 2005-9-16 at 10:20 ]
作者: Climbing     时间: 2004-10-10 00:00
C:\>strings x=left wang,2
C:\>echo %x%
作者: StarWorld     时间: 2005-9-15 14:39    标题: 我也遇到同样的问题了

strings x=left wang,2
echo %x%
根本就得不到wa
这是怎么回事?
作者: JonePeng     时间: 2005-9-15 22:04


  Quote:
Originally posted by StarWorld at 2005-9-15 14:39:
strings x=left wang,2
echo %x%
根本就得不到wa
这是怎么回事?

你一定是在NT系统下用这些命令。NT下的CMD不支持通过strings命令来创建并给一个环境变量。只有在纯DOS或Win9x的MSDOS方式才有效。
作者: StarWorld     时间: 2005-9-16 10:02    标题: 我是在windows 2000下使用的

不知道应该怎么解决这个问题??
作者: willsort     时间: 2005-9-16 10:24
Re StarWorld:

     在 NT 系列系统下解决你的问题,一般使用 set 命令的取子串功能;它属于命令扩展功能,故要求开启 CMD 的命令扩展,XP命令行中默认开启。如:
  set x=wang
  set x=%x:~0,2%
  echo %x%