![]() |
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-09-15 12:30 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » How to use batch processing to replace specified content in a text file. |
| Printable Version 14,438 / 23 |
| Floor1 pengfei | Posted 2006-07-22 17:54 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Just replace the characters in the first line of A.TXT with the ### characters in B.TXT.
Then replace the characters in the second line of A.TXT with the @@@ characters in B.TXT. There are several lines that need to be replaced. ........................ Can this work? Please ask experts for guidance. Thanks! [ Last edited by pengfei on 2006-7-22 at 23:19 ] |
|
| Floor2 willsort | Posted 2006-07-22 19:12 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
───────────────── Moderation Record ───────────────── Executor: Will Sort Operation: Move Topic: From DOS Troubleshooting & Problem Discussion (Help Desk) Description: Classified according to the topic content, more suitable for posting in this forum area Punishment: Since it is a new forum user, no points punishment, please visit {7326}Must-read for Forum Newcomers, Basic Code of Conduct for Everyone ───────────────── Moderation Record ───────────────── ───────────────── Moderator's Note ───────────────── To facilitate forum users' browsing and moderator management, please modify the title of this topic to briefly describe the content or intention of the topic If you confirm that the title does not need to be modified, please continue to reply to the topic for defense. If the defense reason is sufficient, this notice will be canceled If you confirm that the title needs to be modified, please click the "Edit" button below the first post of the topic, and modify the title bar in the editing page If you still do not appeal or modify within three days after this notice is issued, the moderator will modify it and impose corresponding punishment on the author The punishment is to deduct 8 points, including 6 points recovered from the rewards for publishing the topic and 2 points deducted punitively for the title violation After knowing that you have made a positive and appropriate response, the moderator will delete this notice within three working days to eliminate the adverse impact on you ───────────────── Moderator's Note ───────────────── |
|
| Floor3 namejm | Posted 2006-07-22 20:51 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Referencing the writing of doscc on floor 5, the previous code has been modified. Please note the usage conditions:
[ Last edited by namejm on 2006-7-23 at 16:29 ] |
|
| Floor4 pengfei | Posted 2006-07-22 23:13 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Hehe~ I'll give it a try, thanks for your hard work.
|
|
| Floor5 doscc | Posted 2006-07-23 04:04 |
| 中级用户 Posts 93 Credits 256 From 广东 | |
|
I'll also give it a try.
Take the three characters @ # $ as an example! If the file to be processed is more complex. It's best to use a third - party tool! [ Last edited by doscc on 2006 - 7 - 23 at 04:38 ] |
|
| Floor6 namejm | Posted 2006-07-23 08:54 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Re doscc:
Your code is really excellent, using the format of call :label parameter. I should learn from you in this regard. After looking at your code, there are some parts that I don't fully understand yet, and I'm gradually figuring them out. Hehe, it's just that my level is limited, so I'll keep figuring it out :) My code seems to be no longer afraid of any sensitive characters except for colons. However, if I need to replace N lines of content, I will inevitably have to repeat the code format for operating the first line of code. Using call to solve it should be a good idea. Someone else can expand on this. |
|
| Floor7 willsort | Posted 2006-07-23 13:12 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re namejm & doscc:
Brother doscc implemented line-by-line replacement, while Brother namejm implemented replacement of specified line numbers. Relatively speaking, the former has better flexibility, and the latter has higher efficiency. For Brother namejm's colon problem, changing tokens=1,2 to tokens=1,2* should solve it. However, this doesn't mean all special character problems can be solved. You'd better find a batch processing code with sufficient scale and complexity (it can also be your own batch processing) as the test text. In addition, it is suggested to also control the line number as a parameter of :_replace to have greater expandability. |
|
| Floor8 pengfei | Posted 2006-07-23 13:37 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Wow, it works after trying. Our China DOS Union is really full of talented people. But I'm just starting out and I'm completely confused. I hope namejm and doscc brothers can explain the specific meanings and usages of these commands. I'd like to thank you here in advance.
[ Last edited by pengfei on 2006-7-23 at 13:43 ] |
|
| Floor9 namejm | Posted 2006-07-23 14:11 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Re willsort:
The problem with colons is that using tokens=1,2* works well. It seems that the for statement still needs to be carefully studied; the suggestion to use the line number as a parameter for _replace is a good idea. It's just that I still don't fully understand the format of call :label parameters. I'm still learning from the doscc in building 5. If the moderator has a good method, it's better to post it for us to learn. What I mean by seemingly not being afraid of any sensitive characters means that it seems it can handle whether A.txt or B.txt contains any sensitive characters, not that replacing the content to be replaced like @@@, ### with sensitive characters. Maybe my description was not accurate enough to cause your misunderstanding, or maybe the text I tested was not complex enough to be representative. The text I tested is the following A.txt B.txt |
|
| Floor10 willsort | Posted 2006-07-23 14:48 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re namejm:
Setting parameters for a specified line number is not difficult. Change set /a num+=1 to set num=%2, and then follow the line number after call :_replace @@@. To write robust code, it is also necessary to perform null checks and type judgments on the introduced parameters. Of course, for module code that is only called internally, this requirement can be appropriately relaxed. For the issue of resisting special characters, it is suggested that you follow what I said above, save your batch script itself as A.TXt/B.TXT for testing, and I believe you will have many discoveries. |
|
| Floor11 namejm | Posted 2006-07-23 15:07 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
When writing this code, repeatedly testing made my head big; I saved the code I wrote as A.txt/B.txt for testing, oh my god, I didn't even know which was which. Hey, when encountering slightly complicated problems, my mind just short-circuits. It seems that the choice of not studying science and engineering originally wasn't without reason.
Any big shot hurry up and come to the rescue吧,I can't take it anymore. hoho. |
|
| Floor12 doscc | Posted 2006-07-23 16:13 |
| 中级用户 Posts 93 Credits 256 From 广东 | |
|
RE: namejm
The improved code by you is very good. Learning from you! Learning from the moderator! RE: pengfei The code is not difficult. The comments are tedious. Please refer to the top-sticked batch processing tutorial. You will understand it soon! |
|
| Floor13 pengfei | Posted 2006-07-23 18:40 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Two brothers' rewritten code completely meets my expected requirements, but I still can't understand it. May I ask, can I directly input characters in the batch processing and then replace the specified strings in the text in sequence? I'll ask for advice in a couple of days. Thanks, everyone.
[ Last edited by pengfei on 2006-7-23 at 18:41 ] |
|
| Floor14 pengfei | Posted 2006-07-26 11:34 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Excuse me, how to write a batch script that uses SET /P to directly specify a variable %all%, input characters in the batch, and then use this variable to replace the specified characters in a text document.
The batch scripts written by the two elder brothers above, the younger brother still doesn't quite understand, and is studying. |
|
| Floor15 namejm | Posted 2006-07-26 19:10 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
```
@echo off setlocal enabledelayedexpansion set file= set /p file= Please enter the name of the file to operate (including extension): set "file=%file:"=%" for %%i in ("%file%") do set file=%%~fi echo. set replaced= set /p replaced= Please enter the content to be replaced: echo. set all= set /p all= Please enter the replacement string: for /f "delims=" %%i in ('type "%file%"') do ( set str=%%i set "str=!str:%replaced%=%all%!" echo !str!>>"%file%"_tmp.txt ) copy "%file%" "%file%"_bak.txt >nul 2>nul move "%file%"_tmp.txt "%file%" start "" "%file%" ``` |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |