如何在DOS下增加变量? [英] How to increment variable under DOS?

查看:101
本文介绍了如何在DOS下增加变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去3个小时我一直在努力解决问题,但找不到解决方案.这是我的批处理脚本:

I've spent the past 3hrs trying to work this out but just couldn't find a solution. Here's my batch script:

if NOT Exist Counter.txt GOTO START
Type c:\counter.txt
if %COUNTER% EQU %Cycles% goto Pass
if NOT %COUNTER% EQU %Cycles% goto Cycle

:START
Set COUNTER=0
set CYCLES=250

:Cycle
set /A COUNTER=%COUNTER%+1     <----------- PROBLEM
echo Iteration %COUNTER% of %CYCLES%
echo Delay 3 seconds to reboot
choice /cy /n /t:y,3
warmboot.com

:Pass
pause

它的作用是运行文件"warmboot.com"(重新启动PC)并运行250个周期.一旦周期运行了250次(即计数器等于周期),它就会停止.

What it's doing is it runs the file "warmboot.com" (which reboots my PC) and runs for 250 cycles. Once the cycles have run 250 times (i.e when counter equals cycles) then it stops.

在Windows下,此方法有效.但是,这在DOS环境下不起作用.我已经尝试了从v4一直到v6.22甚至是v7的版本,当它们到达问题"行时它们都失败了.

Under Windows, this works. However, this doesn't work under DOS environment. I've tried versions from v4 all the way up to v6.22 and even v7 and they all fail when it reaches the "PROBLEM" line.

如果我这样做:

set /A COUNTER=%COUNTER%+1
echo %Counter%

或者这个:

set /A COUNTER+=1
echo %Counter%

都将返回一个空行,即它不显示任何内容.

both will return a blank line, i.e it shows nothing as the output.

如果我输入:

set /?

然后显示以下内容:

显示,设置或删除cmd.exe环境变量.

Displays, sets, or removes cmd.exe environment variables.

SET [variable = [string]]

SET [variable=[string]]

variable指定环境变量名称.
字符串指定要分配给变量的一系列字符.

variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.

,但是在Windows中的CMD下键入相同的命令会显示更多内容.我认为DOS下的SET函数不支持算术函数,但是出于工作目的,我只能在DOS中运行脚本.

but typing the same command under CMD in Windows shows a lot more stuff. I'm thinking that the SET function under DOS doesn't support arithmetic functions, but for work purposes I have to run my scripts in DOS only.

有什么想法吗?

推荐答案

我知道您找到了另一个答案-但事实是您的原始代码几乎是是正确的,但是存在语法错误.

I realize you've found another answer - but the fact is that your original code was nearly correct but for a syntax error.

您的代码包含该行

set /A COUNTER=%COUNTER%+1

并且可行的语法很简单...

and the syntax that would work is simply...

set /A COUNTER=COUNTER+1

有关SET命令的所有详细信息,请参见 http://ss64.com/nt/set.html .我只是想为所有无法使用FreeDOS的人添加此说明.

See http://ss64.com/nt/set.html for all the details on the SET command. I just thought I'd add this clarification for anyone else who doesn't have the option of using FreeDOS.

这篇关于如何在DOS下增加变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆