China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-02 01:36
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Problems with using the SET command to take strings one by one. (Solved) View 1,881 Replies 3
Original Poster Posted 2006-08-23 19:36 ·  中国 广东 东莞 电信
中级用户
★★
Credits 387
Posts 104
Joined 2004-11-19 00:00
21-year member
UID 33813
Gender Male
Status Offline
The original Chinese text contains some content about batch code issues. The relevant translation for the part about the problematic instruction is:

The problem with the instruction `set "char0=!choice:~%n%,1!"` is that the variable name here is wrong. It should be `char` instead of `choice`. Because earlier you used `set /p char=`, so the variable to be processed should be `char` not `choice`.
Floor 2 Posted 2006-08-23 23:51 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Suppose you have the statements correct elsewhere, but you set n=0 earlier and then don't change the value of n later, so in the statement "set "char0=!choice:~%n%,1!"", %n% is always 0, and you can only get the first character.

In fact, there is a problem with the statement "set "char0=!choice:~%n%,1!"" (is choice a typo for char?) in your code. I don't see the function of using !!; the whole code will have an error.

[ Last edited by namejm on 2006-8-23 at 23:55 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-08-24 08:11 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Originally posted by zts59 at 2006-8-23 19:36:
set char=
set /p char= enter:
:test
set n=0
set "char0=!char:~%n%,1!"
if %char0%="" goto end
set /A n="n+1"
echo %char0%
goto test
:end
pause


Want to achieve...

1. Logical error:
The line
set n=0
is after the subsequent goto test, which will reset the value of n to 0 again. Even if all other code is correct, it will fall into an infinite loop, continuously displaying the first character.
Correct solution:
Move it in front of the test label.
2. Syntax error:
if %char0%="" goto end

Correct solution:
if "char0%" == "" goto end

3. Command error:
The command to enable delayed environment variable expansion, setlocal ENABLEDELAYEDEXPANSION, is not used, resulting in the variables contained in the subsequent!! not being recognized and being treated as strings.
Correct solution:
Add setlocal ENABLEDELAYEDEXPANSION at the front.
Modify your code slightly as follows:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set n=0
set char=
set /p char= enter:

:test
set "char0=!char:~%n%,1!"
if "%char0%" == "" goto end
set /A n="n+1"
echo %char0%
goto test

:end
pause


Used to handle strings without sensitive characters, which is basically sufficient.
Floor 4 Posted 2006-08-24 08:59 ·  中国 广东 东莞 电信
中级用户
★★
Credits 387
Posts 104
Joined 2004-11-19 00:00
21-year member
UID 33813
Gender Male
Status Offline
Thanks, you guys. There is indeed a typo,
and there is also a logical problem. Thanks for correcting.

Oh, and there's also an if "char0%" == "" goto end

When should we use quotes and when not?

Also, using the command to enable delayed environment variables, thank you again.
Forum Jump: