![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-06 04:05 |
48,039 topics / 350,124 posts / today 2 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » Using a for loop for text replacement, but some special characters cannot be replaced. Please help! |
| Printable Version 4,018 / 11 |
| Floor1 diane | Posted 2006-07-13 20:50 |
| 新手上路 Posts 1 Credits 6 | |
|
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... |
|
| Floor2 buddiyar | Posted 2006-07-15 00:31 |
| 初级用户 Posts 75 Credits 160 | |
|
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 ^_^ |
|
| Floor3 chenall | Posted 2006-07-16 20:36 |
| 银牌会员 Posts 469 Credits 1,276 From 福建泉州 | |
|
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 |
|
| Floor4 willsort | Posted 2006-07-20 01:28 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
致黛安:
set的字符串替换支持句点和空格,请测试以下示例,正常情况下应该显示出我的英文name。你的问题应该与a.txt中的其他内容有关。 另外,echo!n!到以下几句,可使用一句echo.!n! >> temp.txt等同替换。 |
|
| Floor5 buddiyar | Posted 2006-07-21 20:18 |
| 初级用户 Posts 75 Credits 160 | |
|
Hehe, thanks for chenall's explanation. But it seems that brother willsort's is more clear and understandable. Always admired you a lot
|
|
| Floor6 oilio | Posted 2007-01-16 03:00 |
| 高级用户 Posts 303 Credits 641 | |
|
Oh, this is really confusing. What on earth is!n!?
|
|
| Floor7 minmin888 | Posted 2007-04-21 03:45 |
| 初级用户 Posts 62 Credits 127 | |
|
Learned something, thanks willsort
|
|
| Floor8 xielei198210 | Posted 2007-04-24 04:26 |
| 新手上路 Posts 2 Credits 4 | |
|
Why can the sentence `echo.!n! >> temp.txt` be used to determine and delete echo?
|
|
| Floor9 xielei198210 | Posted 2007-04-24 04:27 |
| 新手上路 Posts 2 Credits 4 | |
|
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 ] |
|
| Floor10 vkill | Posted 2007-04-25 02:17 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
Dealing with special characters in p is still quite troublesome.
|
|
| Floor11 qqqrun3 | Posted 2010-12-01 23:15 |
| 初级用户 Posts 28 Credits 59 | |
|
『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! ) |
|
| Floor12 qqqrun3 | Posted 2010-12-01 23:21 |
| 初级用户 Posts 28 Credits 59 | |
|
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?
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |