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-24 05:11
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to remove the characters at the beginning and end of a TXT text View 3,242 Replies 13
Original Poster Posted 2006-12-14 10:04 ·  中国 广东 珠海 电信
初级用户
Credits 70
Posts 24
Joined 2005-09-20 00:43
20-year member
UID 42654
Status Offline
set IBV storage path=D\disk\ibv
set machine name prefix=XP-
set number of clients=200

if %number of clients% leq 9 goto :9
if %number of clients% leq 99 goto :99
if %number of clients% leq 999 goto :999
goto :end

:9
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%machine name prefix%00%%i" file="My Computer\%IBV storage path%\%machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt


:99
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%machine name prefix%00%%i" file="My Computer\%IBV storage path%\%machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (10,1,%number of clients%) do echo "<device name="IBV%%i" target="%machine name prefix%0%%i" file="My Computer\%IBV storage path%\%machine name prefix%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt


:999
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%machine name prefix%00%%i" file="My Computer\%IBV storage path%\%machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (10,1,99) do echo "<device name="IBV%%i" target="%machine name prefix%0%%i" file="My Computer\%IBV storage path%\%machine name prefix%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (100,1,%number of clients%) do echo "<device name="IBV%%i" target="%machine name prefix%%%i" file="My Computer\%IBV storage path%\%machine name prefix%%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt

:end

After generation, it is like this
"<device name="IBV1" target="XP-001" file="My Computer\D\disk\ibv\XP-001.ibv" mode="3" clustered="no" asyncmode="yes"/>"

Just want to remove the preceding "and the following"

<device name="IBV1" target="XP-001" file="My Computer\D\disk\ibv\XP-001.ibv" mode="3" clustered="no" asyncmode="yes"/>


There is also one that takes the last 3 digits of the machine name and defines it as a variable. Is there a more intuitive way

for /f %%i in ("%computername%") do (
set "name=%%i"
call set "name=%%name:~3,4%%")
Floor 2 Posted 2006-12-14 14:52 ·  中国 广东 珠海 电信
初级用户
Credits 70
Posts 24
Joined 2005-09-20 00:43
20-year member
UID 42654
Status Offline
Can anyone give some guidance?
Floor 3 Posted 2006-12-14 15:30 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
###
  Suppose the content of your IBVcfg.txt is as follows:

"<device name="IBV%%i" target="%Machine name prefix%00%%i" file="My Computer\%IBV storage path%\%Machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>"
"<device name="IBV%%i" target="%Machine name prefix%0%%i" file="My Computer\%IBV storage path%\%Machine name prefix%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>"
"<device name="IBV1" target="XP-001" file="My Computer\D\disk\ibv\XP-001.ibv" mode="3" clustered="no" asyncmode="yes"/>"

  Try this script:

rem code by lxmxn @ cn-dos.net
@echo off
for /f "delims=" %%a in (IBVcfg.txt) do (
setlocal enabledelayedexpansion
set str=%%a
set str=!str:~1,-1!
echo !str!
::echo If you want to input to a file, add >>filename.txt after echo !str!
endlocal
)
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2006-12-15 01:30
Floor 4 Posted 2006-12-14 16:31 ·  中国 广东 珠海 电信
初级用户
Credits 70
Posts 24
Joined 2005-09-20 00:43
20-year member
UID 42654
Status Offline
The dots at the back are not removed, only the ones at the front are removed.
Floor 5 Posted 2006-12-15 01:06 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  I can get the correct result. The content of my IBVcfg.txt is:

"<device name="IBV%%i" target="%Machine name prefix%00%%i" file="My Computer\%IBV storage path%\%Machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>"
"<device name="IBV%%i" target="%Machine name prefix%0%%i" file="My Computer\%IBV storage path%\%Machine name prefix%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>"
"<device name="IBV1" target="XP-001" file="My Computer\D\disk\ibv\XP-001.ibv" mode="3" clustered="no" asyncmode="yes"/>"

  The obtained result is:

<device name="IBV%%i" target="%Machine name prefix%00%%i" file="My Computer\%IBV storage path%\%Machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>
<device name="IBV%%i" target="%Machine name prefix%0%%i" file="My Computer\%IBV storage path%\%Machine name prefix%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>
<device name="IBV1" target="XP-001" file="My Computer\D\disk\ibv\XP-001.ibv" mode="3" clustered="no" asyncmode="yes"/>

  Please check it carefully again.
Floor 6 Posted 2006-12-15 01:30 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Indeed. Brother lxmxn's code is correct. Add points!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 7 Posted 2006-12-15 02:58 ·  中国 湖北 武汉 电信
初级用户
Credits 24
Posts 11
Joined 2006-09-30 09:07
19-year member
UID 64133
Status Offline
Learned, feel that I am too bad
Thank you
Floor 8 Posted 2006-12-15 03:20 ·  中国 广东 珠海 电信
初级用户
Credits 70
Posts 24
Joined 2005-09-20 00:43
20-year member
UID 42654
Status Offline
Hehe, it's still like this. Please give me some more pointers on where the errors are.

set IBV存放路径=D\disk\ibv
set 机器名前缀=XP-
set 客户机数量=200

if %客户机数量% leq 9 goto :9
if %客户机数量% leq 99 goto :99
if %客户机数量% leq 999 goto :999
goto :end

:9
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%机器名前缀%00%%i" file="My Computer\%IBV存放路径%\%机器名前缀%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt


:99
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%机器名前缀%00%%i" file="My Computer\%IBV存放路径%\%机器名前缀%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (10,1,%客户机数量%) do echo "<device name="IBV%%i" target="%机器名前缀%0%%i" file="My Computer\%IBV存放路径%\%机器名前缀%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt


:999
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%机器名前缀%00%%i" file="My Computer\%IBV存放路径%\%机器名前缀%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (10,1,99) do echo "<device name="IBV%%i" target="%机器名前缀%0%%i" file="My Computer\%IBV存放路径%\%机器名前缀%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (100,1,%客户机数量%) do echo "<device name="IBV%%i" target="%机器名前缀%%%i" file="My Computer\%IBV存放路径%\%机器名前缀%%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt

:end

rem code by lxmxn @ cn-dos.net
@echo off
for /f "delims=" %%a in (IBVcfg.txt) do (
setlocal enabledelayedexpansion
set str=%%a
set str=!str:~1,-1!
echo !str! >>filename.txt
::echo 如果要输入到文件中,在echo !str!加上>>filename.txt
endlocal
)
pause
Hehe, it's still like this. Please give some more pointers on where the errors are.

set IBV storage path=D\disk\ibv
set machine name prefix=XP-
set number of clients=200

if %number of clients% leq 9 goto :9
if %number of clients% leq 99 goto :99
if %number of clients% leq 999 goto :999
goto :end

:9
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%machine name prefix%00%%i" file="My Computer\%IBV storage path%\%machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt


:99
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%machine name prefix%00%%i" file="My Computer\%IBV storage path%\%machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (10,1,%number of clients%) do echo "<device name="IBV%%i" target="%machine name prefix%0%%i" file="My Computer\%IBV storage path%\%machine name prefix%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt


:999
for /l %%i in (1,1,9) do echo "<device name="IBV%%i" target="%machine name prefix%00%%i" file="My Computer\%IBV storage path%\%machine name prefix%00%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (10,1,99) do echo "<device name="IBV%%i" target="%machine name prefix%0%%i" file="My Computer\%IBV storage path%\%machine name prefix%0%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt
for /l %%i in (100,1,%number of clients%) do echo "<device name="IBV%%i" target="%machine name prefix%%%i" file="My Computer\%IBV storage path%\%machine name prefix%%%i.ibv" mode="3" clustered="no" asyncmode="yes"/>" >>IBVcfg.txt

:end

rem code by lxmxn @ cn-dos.net
@echo off
for /f "delims=" %%a in (IBVcfg.txt) do (
setlocal enabledelayedexpansion
set str=%%a
set str=!str:~1,-1!
echo !str! >>filename.txt
::echo If you want to input to the file, add >>filename.txt to echo !str!
endlocal
)
pause
Floor 9 Posted 2006-12-15 03:21 ·  中国 广东 珠海 电信
初级用户
Credits 70
Posts 24
Joined 2005-09-20 00:43
20-year member
UID 42654
Status Offline
<device name="IBV1" target="XP-001" file="My Computer\D\disk\ibv\XP-001.ibv" mode="3" clustered="no" asyncmode="yes"/>"

This is what it looks like after survival. There is still a dot at the end.
Floor 10 Posted 2006-12-15 03:46 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Re novice :

  The code you and I are using is not different. It's because there is a space after each line in your IBVcfg.txt file (you can find it by carefully observing). This causes the batch script not to meet the requirements you want.

  The solution is to remove the space in front of the input redirection symbol ">>" in your original code. In this way, there will be no space after each sentence in the generated IBVcfg.txt file, and then the above code can meet your requirements. In fact, the essence of the problem is still that my code is not robust.  
Floor 11 Posted 2006-12-15 03:53 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Using the %~i expansion in a for statement to remove the quotes at the beginning and end of a string is a bit more concise:


@echo off
for /f "delims=" %%i in (IBVcfg.txt) do echo %%~i
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lxmxn +3 2006-12-15 04:15
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-12-15 04:04 ·  中国 广东 珠海 电信
初级用户
Credits 70
Posts 24
Joined 2005-09-20 00:43
20-year member
UID 42654
Status Offline
Thanks to the moderator and the great person for the help, extremely grateful!!!
Floor 13 Posted 2006-12-15 04:15 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  The moderator's code is very concise, give a thumbs up.

  But if there is a space after the text, it can't solve the problem of quotes well.
Floor 14 Posted 2006-12-16 10:00 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Forum Jump: