|
024024
初级用户
 
积分 60
发帖 14
注册 2006-3-23
状态 离线
|
『楼 主』:
用什么办法可以使BAT结束,而变量值还在,还可以用呢?
使用 LLM 解释/回答一下
我在BAT文件中用SET设置了一些变量,可是BAT一结束,这些变量值也跟着消失了.
用什么办法可以使BAT结束,而变量值还在,还可以用呢?
I set some variables with SET in the BAT file, but as soon as the BAT ends, those variable values disappear. What method can be used to make the variable values remain and be usable even after the BAT ends?
|
|
2006-3-23 17:35 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
在批处理中使用Jscript或者VBscript来设置变量。如:
//test.js
var shell
shell = WScript.CreateObject("WScript.Shell")
shell.Environment("System").Item("DEVMGR_SHOW_NONPRESENT_DEVICES")='1'
Using JScript or VBScript in batch processing to set variables. For example:
//test.js
var shell
shell = WScript.CreateObject("WScript.Shell")
shell.Environment("System").Item("DEVMGR_SHOW_NONPRESENT_DEVICES")='1'
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2006-3-23 18:17 |
|
|
DOSforever
金牌会员
     
积分 4639
发帖 2239
注册 2005-1-30
状态 离线
|
|
2006-3-23 18:34 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
楼上说得是不对的,在批处理中设置的环境变量只在一个cmd进程中有效,如果cmd进程关掉了,变量也就消失了。楼主肯定是直接双击执行批处理,执行完成后,相应的cmd进程就直接关掉了。就算不关掉,批处理设置的环境变量也只是在它运行的那个cmd环境中有效,而不是全局生效。不信试一下!
先打开一个cmd窗口,设置一个环境变量,再打开另一个cmd窗口,检查是否有相应的环境变量。
What you said upstairs is incorrect. In batch processing, the environment variables set are only effective in one cmd process. If the cmd process is closed, the variables will disappear. The owner must have directly double - clicked to execute the batch processing, and after execution, the corresponding cmd process is directly closed. Even if it is not closed, the environment variables set by the batch processing are only effective in the cmd environment in which it runs, not globally effective. Just try it! First, open a cmd window, set an environment variable, and then open another cmd window to check if there is the corresponding environment variable.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2006-3-23 18:40 |
|
|
DOSforever
金牌会员
     
积分 4639
发帖 2239
注册 2005-1-30
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
CMD?!
对不起,我只考虑纯 DOS 环境。
顺便和各位说一下我一直以来的看法:现在我们这个 DOS 论坛越来越不纯了,许多问题其实都是 Windows 下的,却发到什么“DOS问题解答室”来了,这样的例子可以举出许多,后面随便翻几页都有。如果我是版主的话,早就转了。当然,还有些问题属于比较低层的,和操作系统的关系不大,但论坛又没有专门开设一个这样的版面,可能是考虑到“疑难解答室”的人比较多,所以就发到这里来了吧。呵呵
另外,小嗑好长时间没见到你了,来,让我亲一个  呵呵呵呵……
CMD?!
I'm sorry, I only consider the pure DOS environment.
By the way, I want to tell you my view all the time: Now our DOS forum is getting less pure. Many problems are actually under Windows, but they are posted to the "DOS Problem Solving Room". There are many such examples, and you can find some by flipping a few pages later. If I were a moderator, I would have moved them long ago. Of course, there are some problems that belong to a relatively low level and have little to do with the operating system, but the forum doesn't have a special section for this. Maybe it's because there are more people in the "Difficult Problems Solving Room", so they are posted here. Hehe
In addition, I haven't seen Xiao Ke for a long time. Come on, let me give you a kiss  Hehehe...
|

DOS倒下了,但永远不死
DOS NEVER DIES !
投票调查:
http://www.cn-dos.net/forum/viewthread.php?tid=46187
本人尚未解决的疑难问题:
http://www.cn-dos.net/forum/viewthread.php?tid=15135
http://www.cn-dos.net/forum/viewthread.php?tid=47663
http://www.cn-dos.net/forum/viewthread.php?tid=48747 |
|
2006-3-23 18:57 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
也不能这么绝对,虽然cmd不同于dos,便我们的目的是利用计算机更好的为我们服务,何必过于执着呢?毕竟现在没有几个人拿纯dos工作,cmd环境下的应用要远远多于纯dos下的应用,何况,在批处理级别,二者也没有本质上的差别。
It can't be so absolute. Although cmd is different from DOS, but our purpose is to use the computer to serve us better. Why be too persistent? After all, there are not many people using pure DOS for work now. The applications in the cmd environment are far more than those in pure DOS. Besides, at the batch processing level, there is no essential difference between the two.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2006-3-23 20:24 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Re 024024l:
环境变量的修改在程序结束后失效的情况主要有以下几类:
1、在DOS/9x/NTs的COMMAND下,在批处理中使用%comspec%调用了另外的程序和子程序,并在其中修改变量,在结束%comspec%调用后这个修改将失效;
解决:在结束%comspec%调用前将其中的环境变量保存到临时批处理文件中,在结束后再运行这个批处理重新设定变量。
实例:
%comspec% /c %0 : subroutin
%temp%\_setlocal.bat
goto end
:subroutin
set local=var1
echo set local=%local% > %temp%\_setlocal.bat
goto end
:end
2、在NTs的CMD下,在批处理中使用了setlocal这个环境变量本地化命令,自此以后,直到遇到endlocal或者批处理结束,其间所有环境变量的修改将于其后失效;
解决:在批处理结束前,使用enlocal命令,并与环境变量设置语句合并运行;
实例:
setlocal
set local=var1
enlocal & set local=%local%
3、在Win下的COMMND/CMD中,环境变量的修改通常只在当前的命令行环境中有效,它不会影响系统全局,或者以后所有的命令行环境;
解决:使用Windows98安装盘上的增强工具 setx 设备变量。
实例:
setx local=var1
Re Climbing:
MSDOS6.22 / MSDOS7.10/ COMAND@Win9x / COMMAND@WinXP / CMD@WinXP 这些环境的区别与联系异常复杂,他们对批处理的影响也难以估量。它直接导致我在编写完批处理后,必须反复验证它在各种环境下的兼容性,未经测试的环境决不会轻易署在它的可运行环境中,即使同为COMMAND或者CMD。
Re 024024l:
The situations where modifications to environment variables become invalid after the program ends are mainly of the following types:
1. Under the COMMAND of DOS/9x/NTs, using %comspec% in a batch script to call another program and subroutine, and modifying variables in it. After the %comspec% call ends, this modification will be invalid;
Solution: Save the environment variables in it to a temporary batch file before the %comspec% call ends, and then run this batch file after the end to reset the variables.
Example:
%comspec% /c %0 : subroutin
%temp%\_setlocal.bat with the _setlocal.bat
goto end
:subroutin
set local=var1
echo set local=%local% > %temp%\_setlocal.bat
goto end
:end
2. Under the CMD of NTs, using the setlocal environment variable localization command in a batch script. From then on, until endlocal is encountered or the batch script ends, all modifications to environment variables during this period will be invalid afterwards;
Solution: Before the batch script ends, use the enlocal command and combine with the environment variable setting statement to run;
Example:
setlocal
set local=var1
enlocal & set local=%local%
3. In the COMMND/CMD under Win, modifications to environment variables usually only take effect in the current command line environment. It will not affect the system globally or all subsequent command line environments;
Solution: Use the enhanced tool setx device variable on the Windows98 installation disk.
Example:
setx local=var1
Re Climbing:
The differences and connections among these environments such as MSDOS6.22/7.10/COMAND@Win9x/COMMAND@WinXP/CMD@WinXP are extremely complex. Their impact on batch scripts is also immeasurable. It directly leads to after writing batch scripts, and I must repeatedly verify its compatibility in various environments. Untested environments will never easily be placed in its runnable environments, even if they are the same COMMAND or CMD.
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-3-23 21:53 |
|
|
linson
中级用户
  
积分 283
发帖 31
注册 2004-3-6
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by Climbing at 2006-3-23 08:24 PM:
也不能这么绝对,虽然cmd不同于dos,便我们的目的是利用计算机更好的为我们服务,何必过于执着呢?毕竟现在没有几个人拿纯dos工作,cmd环境下的庠...
同意这种说法!
Originally posted by Climbing at 2006-3-23 08:24 PM:
It can't be so absolute. Although cmd is different from DOS, our purpose is to use the computer to serve us better. Why be too persistent? After all, there are not many people working with pure DOS now, and the programs in the cmd environment...
I agree with this statement!
|
|
2006-3-23 22:06 |
|
|
DOSforever
金牌会员
     
积分 4639
发帖 2239
注册 2005-1-30
状态 离线
|
|
2006-3-23 22:16 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
最简单的方法:
在批处理的最后把你要保存的变量的赋值语句写到一个批处理文件,然后修改你的注册表使每次启动CMD的时候先执行那个批处理。如在你的批处理最后加上一句:
set myvar=%var% >> c:\mybat.bat
然后修改注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor下的AutoRun值为c:\mybat.bat
这样你运行批处理或CMD的时候就会自动执行mybat.bat。
如果你不想修改注册表的话也可以在桌面点右键创建快捷方式,命令行中就输入:
cmd /k c:\mybat.bat
以后只要通过这个快捷方式来执行运行批处理就可以保证变量的值了
The simplest method:
At the end of the batch processing, write the assignment statement of the variable you want to save into a batch processing file, and then modify your registry to make the batch processing file execute first every time CMD is started. For example, add a line at the end of your batch processing:
set myvar=%var% >> c:\mybat.bat
Then modify the AutoRun value under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor to c:\mybat.bat
In this way, when you run the batch processing or CMD, mybat.bat will be executed automatically.
If you don't want to modify the registry, you can also create a shortcut on the desktop. In the command line, enter:
cmd /k c:\mybat.bat
In the future, as long as you execute the batch processing through this shortcut, the value of the variable can be guaranteed.
|
|
2006-3-23 22:45 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
站内的DOS是有些不纯了,不过什么事情都是在变化,不能总停留在一个地方,我们希望的只是发挥我们最大的能力去支持和使用这个永远不过的系统---DOS
The DOS in the site is a bit impure, but everything is changing, and we can't always stay in one place. What we hope is just to exert our maximum ability to support and use this evergreen system --- DOS
|
|
2006-3-24 10:20 |
|