批处理编程 [英] batch programming
问题描述
我在一批新。尝试一些日子,以批量的东西,但有一个问题我解决不了。我读了很多的你的意见,但没有找到答案。也许你能帮助我吗?
问题的关键是:
- 我的输入字符串从键盘(例如10个字符)。它的名字是allinputstring
- 长度的计算就可以了(在txt文件重定向,扩大字节)。名称长度
- 解析字符串就可以了。
在10件(串)
因此,这里是一个问题,我想呼应这些作品,所以我用下一个code,我用一个计数器来找出是柜台给我算好作为输出变量,呼应它在屏幕上看到如果是好的。计数器似乎不错,片串结束反响不够好。但是,我希望把第5行变量计数
而不是%% M
,并不能找到一个语法路该怎么做。
SETLOCAL enabledelayedexpansion
在/ L %% M(1,1,!lenght!)做(
设置/计数= 0
设置/计数=计数+ %%米
回声计数!! allinputstring:〜!%%男,1!
)
ENDLOCAL
请帮我。
@ECHO关闭
SETLOCAL ENABLEDELAYEDEXPANSION
SET allinputstring = ABCDEFGHIJK
SET lenght = 10
在/ L %% M(1,1,!lenght!)做(
设置/计数= 0
设置/计数=计数+ %%米
FOR %% Z IN(!算!)不要回应!算! !allinputstring:%%〜Z,1!
)
GOTO:EOF
这是不是你需要什么?
所以...使COUNT节目(我是分配给KOWNT,但语法
ENDLOCAL和放大器;设置计数=%计%
将其分配给 COUNT
代替)我已经改变了初始值的 FOR / L
,因为字符计数从字符#0字符串中开始。
@ECHO关闭
SETLOCAL ENABLEDELAYEDEXPANSION
SET allinputstring = ABCDEFGHIJ
SET lenght = 9
在/ L %% M(0,1,!lenght!)做(
设置/计数= 0
设置/计数=计数+ %%米
FOR %% Z IN(!算!)不要回应!算! !allinputstring:%%〜Z,1!
)
ENDLOCAL&安培; SET KOWNT =%计%
ECHO现在KOWNT =%KOWNT%,但数=%计%,因为我们已经退出了SETLOCAL
GOTO:EOF
在该 ENDLOCAL
遇到,解析器替代行中的变量的当前值和然后执行就行了。
因此,该行被作为执行
ENDLOCAL和放大器;设置KOWNT = 9
因为值计数
的时间为9。
在该 SETLOCAL
被执行,因为匹配所有对环境的更改 SETLOCAL
被扔掉。环境变量恢复到其状态 SETLOCAL
被执行,计数
再次成为空的时候(因为它是前程序。)然后的 SET
指令被执行,其中规定 KOWNT
到9
I am new in batch. Trying for some days to make something in batch but have a problem I cannot solve. I read a lot of your comments but did not find answer. Maybe you can help me?
The point is:
- I input string from keyboard( e.g. 10 characters ). name of it is"allinputstring"
- Calculate of length is ok ( by redirect in txt file and expand its bytes ). name "length"
- Parse string in 10 pieces (strings) is ok.
So here is a problem, I want to echo these pieces, so I use next code, I use a counter to find out is the counter give me good count as output variable, and echo it to see on screen if it is good. Counter seems good, end echo of pieces strings is good enough. But I want to put in line 5. Variable count
instead of "%%m"
, and cannot find a syntax way how to do it.
setlocal enabledelayedexpansion
for /l %%m in (1,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
echo !count!!allinputstring:~%%m,1!
)
endlocal
please help me.
@ECHO off
setlocal ENABLEDELAYEDEXPANSION
SET allinputstring=abcdefghijk
SET lenght=10
for /l %%m in (1,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
FOR %%z IN (!count!) DO echo !count! !allinputstring:~%%z,1!
)
GOTO :eof
Does this do what you require?
So... to make COUNT show (I've assigned it to KOWNT, but the syntax
endlocal&set count=%count%
would assign it to COUNT
instead)
I've changed the starting value of the FOR/L
because character counting starts from character#0 in the string.
@ECHO off
setlocal ENABLEDELAYEDEXPANSION
SET allinputstring=abcdefghij
SET lenght=9
for /l %%m in (0,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
FOR %%z IN (!count!) DO echo !count! !allinputstring:~%%z,1!
)
endlocal&SET KOWNT=%count%
ECHO Now KOWNT=%KOWNT% but count=%count% because we have exited the SETLOCAL
GOTO :eof
When the ENDLOCAL
is encountered, the parser substitutes the CURRENT value of the variables in the line and THEN executes the line.
Hence, the line is executed as
endlocal&set KOWNT=9
since the value of count
at the time is 9.
When the SETLOCAL
is executed, all changes to the environment since the matching SETLOCAL
are thrown away. The environment variables are restored to their state when the SETLOCAL
was executed and count
becomes empty again (as it was before the routine.) THEN the SET
instruction is executed, which sets KOWNT
to 9.
这篇关于批处理编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!