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-06-20 17:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Using a for loop for text replacement, but some special characters cannot be replaced. Please help! View 3,788 Replies 11
Original Poster Posted 2006-07-13 20:50 ·  中国 上海 联通
新手上路
Credits 6
Posts 1
Joined 2006-07-13 20:21
19-year member
UID 58532
Gender Female
Status Offline
In batch files, replacing certain text in file a.txt with other text

for /f "tokens=* delims= " %%i in (a.txt) do (
set n=%%i
set n=!n:aaa=bbb!
echo !n! | find /I "ECHO"
IF ERRORLEVEL 1 (
echo !n! >> temp.txt
) else (
echo. >>temp.txt
)
)
copy temp.txt a.txt

If the string aaa contains spaces or dots ., it cannot be replaced correctly. Is there any expert who has a better solution? Thanks thanks thanks...
Floor 2 Posted 2006-07-15 00:31 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 160
Posts 75
Joined 2006-06-28 01:07
19-year member
UID 57661
Gender Male
Status Offline
set n=!n:aaa=bbb!

You are also a newbie, it seems you are more capable than me
Hehe, if you don't mind, please explain this sentence first

Thanks ^_^
Floor 3 Posted 2006-07-16 20:36 ·  中国 福建 泉州 石狮市
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
According to the building owner's requirements, basically it cannot be achieved.
I have to add a few more replacement commands. Add all possible combinations in.
I don't know what this is for, what is the final purpose? If it can be more detailed (preferably paste your original file), maybe there is a way.

TO the 2nd floor:
set n=!n:aaa=bbb!
Replace aaa with bbb in variable n
QQ:366840202
http://chenall.net
Floor 4 Posted 2006-07-20 01:28 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
致黛安:

set的字符串替换支持句点和空格,请测试以下示例,正常情况下应该显示出我的英文name。你的问题应该与a.txt中的其他内容有关。

另外,echo!n!到以下几句,可使用一句echo.!n! >> temp.txt等同替换。


@echo off & setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%i in ("Hello world.") do (
set n=%%i
set n=!n:ld.=t!
set n=!n:o w= S!
set n=!n:He=Wi!
echo !n!
)
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2006-07-21 20:18 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 160
Posts 75
Joined 2006-06-28 01:07
19-year member
UID 57661
Gender Male
Status Offline
Hehe, thanks for chenall's explanation. But it seems that brother willsort's is more clear and understandable. Always admired you a lot
Floor 6 Posted 2007-01-16 03:00 ·  中国 江苏 连云港 电信
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Oh, this is really confusing. What on earth is!n!?
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 7 Posted 2007-04-21 03:45 ·  中国 广东 中山 电信
初级用户
★★
Credits 127
Posts 62
Joined 2007-04-19 12:43
19-year member
UID 85744
Gender Male
Status Offline
Learned something, thanks willsort
Floor 8 Posted 2007-04-24 04:26 ·  中国 香港
新手上路
Credits 4
Posts 2
Joined 2007-04-20 04:04
19-year member
UID 85797
Gender Male
Status Offline
Why can the sentence `echo.!n! >> temp.txt` be used to determine and delete echo?
Floor 9 Posted 2007-04-24 04:27 ·  中国 香港
新手上路
Credits 4
Posts 2
Joined 2007-04-20 04:04
19-year member
UID 85797
Gender Male
Status Offline
Why can echo.!n! >> temp.txt determine that echo is an illegal character and delete it?

And the original spaces in the result are gone now, how to keep the spaces?

And the "!" cannot be retained, what to do?

[ Last edited by xielei198210 on 2007-4-23 at 03:34 PM ]
Floor 10 Posted 2007-04-25 02:17 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Dealing with special characters in p is still quite troublesome.
Floor 11 Posted 2010-12-01 23:15 ·  中国 广东 佛山 电信
初级用户
Credits 59
Posts 28
Joined 2010-11-05 01:45
15-year member
UID 177110
Gender Male
Status Offline
『Thread Starter』: Using a for loop for text replacement, some special characters cannot be replaced. Need help!

In a batch file, replace some text in file a.txt with other text
for /f "tokens=* delims= " %%i in (a.txt) do (
set n=%%i
set n=!n:aaa=bbb!
echo !n! | find /I "ECHO"
IF ERRORLEVEL 1 (
echo !n! >> temp.txt
) else (
echo. >>temp.txt
)
)
copy temp.txt a.txt

If the string aaa contains spaces or dots ., it cannot be replaced correctly. Is there any great god with a better solution? Thanks thanks thanks
TO 2nd floor:
set n=!n:aaa=bbb!
Replace aaa with bbb in variable n


@echo off & setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%i in ("Hello world.") do (
set n=%%i
set n=!n:ld.=t!
set n=!n:o w= S!
set n=!n:He=Wi!
echo !n!
)
Floor 12 Posted 2010-12-01 23:21 ·  中国 广东 佛山 电信
初级用户
Credits 59
Posts 28
Joined 2010-11-05 01:45
15-year member
UID 177110
Gender Male
Status Offline
Ask about the previous back: For such code that requires DOS command extensions support with setlocal EnableDelayedExpansion, can it run in a non-Windows environment like DOS 7.1?
Forum Jump: