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-07-14 23:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Help, how to delete the specified characters in the variable View 3,088 Replies 12
Original Poster Posted 2006-11-08 03:13 ·  中国 上海 联通
新手上路
Credits 8
Posts 2
Joined 2006-05-29 20:52
20-year member
UID 56217
Status Offline
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.
Floor 2 Posted 2006-11-08 04:06 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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 ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-11-08 04:51 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 4 Posted 2006-11-08 05:00 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline


set a=%%j
echo %a:-=%
Floor 5 Posted 2006-11-08 05:22 ·  中国 广东 河源 电信
中级用户
★★
Credits 266
Posts 98
Joined 2006-04-21 20:29
20-year member
UID 54223
Gender Male
Status Offline
The person upstairs, please explain why %a:-=% can remove all "-"? Can you talk about the usage?
纵是千年万年 亦难以忘记
Floor 6 Posted 2006-11-08 05:51 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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 "-".
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +2 2006-11-08 06:15
Floor 7 Posted 2006-11-08 07:10 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
Add a hyphen in delims= in for /f
Then %%j becomes %%j-%%k-%%l-%%m
00e04c8888ee is %%j%%k%%l%%m
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
electronixtar +2 2006-11-23 07:03
Floor 8 Posted 2006-11-08 07:23 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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: (
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 9 Posted 2006-11-16 20:32 ·  美国 科罗拉多州 阿拉珀霍 利特尔顿 TW_Telecom控股股份有限公司
初级用户
Credits 35
Posts 18
Joined 2006-04-26 13:31
20-year member
UID 54481
Status Offline
High, really high!
Floor 10 Posted 2006-11-17 01:40 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I like to use sed for replacements in files
Floor 11 Posted 2006-11-23 02:54
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
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?
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 12 Posted 2006-11-23 07:03 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline

Too bad I've already used up all my points today: (

I'll help you add some

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 13 Posted 2006-11-23 07:08 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Haha... Why are the only plus points options 1, 3, 5, 7, 9... Where did the original +2 points option go?
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Forum Jump: