![]() |
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-29 08:07 |
47,816 topics / 349,916 posts / today 0 new / 48,265 members |
| DOS批处理 & 脚本技术(批处理室) » Help, how to delete the specified characters in the variable |
| Printable Version 3,181 / 12 |
| Floor1 tangz | Posted 2006-11-08 03:13 |
| 新手上路 Posts 2 Credits 8 | |
|
To remove the hyphens from `%%j` which has the value `00-e0-4c-88-88-ee` and change it to `00e04c8888ee` in a batch script using `for /f`, you can use the following approach. Here's a sample script snippet:
```batch @echo off setlocal enabledelayedexpansion set "input=00-e0-4c-88-88-ee" set "output=" for /f "tokens=1-6 delims=-" %%a in ("%input%") do ( set "output=%%a%%b%%c%%d%%e%%f" ) echo %output% endlocal ``` In the above script: 1. First, we enable delayed expansion with `enabledelayedexpansion`. 2. We define the input string with the hyphens. 3. Then, we use `for /f` with `delims=-` to split the input string by hyphens into tokens `%%a` to `%%f`. 4. We concatenate these tokens together to form the string without hyphens, storing the result in `output`. 5. Finally, we echo the resulting string which should be `00e04c8888ee`. If you are already reading from an external file using `for /f`, you can adjust the script accordingly to process each line from the file similarly. For example, if you have a file named `data.txt` with each line being the string with hyphens, the script would be: ```batch @echo off setlocal enabledelayedexpansion for /f "usebackq delims=" %%i in ("data.txt") do ( set "line=%%i" set "result=" for /f "tokens=1-6 delims=-" %%a in ("!line!") do ( set "result=%%a%%b%%c%%d%%e%%f" ) echo !result! ) endlocal ``` This script reads each line from `data.txt`, processes it to remove hyphens, and then echoes the result. |
|
| Floor2 redtek | Posted 2006-11-08 04:06 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Deleting specified characters from a variable is equivalent to replacing the specified content with an "empty" character, which is "~:)"
How to replace characters in text http://www.cn-dos.net/forum/viewthread.php?tid=22245&fpage=1&highlight=%E2%80%9C For details, see the fifth floor of the post (you can copy it directly and change the string to what you need). A more general method ~:) If you want to familiarize yourself with various string and variable application methods, see the index in the following post: http://www.cn-dos.net/forum/viewthread.php?tid=24549&fpage=1 [ Last edited by redtek on 2006-11-8 at 04:08 AM ] |
|
| Floor3 不得不爱 | Posted 2006-11-08 04:51 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
| Floor4 lxmxn | Posted 2006-11-08 05:00 |
| 版主 Posts 4,938 Credits 11,386 | |
|
|
|
| Floor5 zerostudy | Posted 2006-11-08 05:22 |
| 中级用户 Posts 98 Credits 266 | |
|
The person upstairs, please explain why %a:-=% can remove all "-"? Can you talk about the usage?
|
|
| Floor6 lxmxn | Posted 2006-11-08 05:51 |
| 版主 Posts 4,938 Credits 11,386 | |
|
The function of %a:x=y% is: replace all "x" contained in variable "a" with "y". In the above example, there are no characters after the equal sign (=), which is replaced with an empty value (not a space), which is equivalent to deleting the "-".
|
|
| Floor7 zerocq | Posted 2006-11-08 07:10 |
| 中级用户 Posts 196 Credits 458 | |
|
Add a hyphen in delims= in for /f
Then %%j becomes %%j-%%k-%%l-%%m 00e04c8888ee is %%j%%k%%l%%m |
|
| Floor8 redtek | Posted 2006-11-08 07:23 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
It's really that many hands make light work: ) Brother zerocq has another method, haha...
It's a pity that all my points have been used up today: ( |
|
| Floor9 honhaizh | Posted 2006-11-16 20:32 |
| 初级用户 Posts 18 Credits 35 | |
|
High, really high!
|
|
| Floor10 vkill | Posted 2006-11-17 01:40 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
I like to use sed for replacements in files
|
|
| Floor11 hxuan999 | Posted 2006-11-23 02:54 |
| 中级用户 Posts 161 Credits 337 | |
|
I think there are only these two methods for CMD. Who among the experts can come up with a third one, without using third-party tools?
|
|
| Floor12 electronixtar | Posted 2006-11-23 07:03 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
I'll help you add some |
|
| Floor13 redtek | Posted 2006-11-23 07:08 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Haha... Why are the only plus points options 1, 3, 5, 7, 9... Where did the original +2 points option go?
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |