中国DOS联盟论坛

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-08 22:24
47,811 topics / 349,895 posts / today 0 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » How to remove the characters at the beginning and end of a TXT text
Printable Version  3,366 / 13
Floor1 newice Posted 2006-12-14 10:04
初级用户 Posts 24 Credits 70
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%%")
Floor2 newice Posted 2006-12-14 14:52
初级用户 Posts 24 Credits 70
Can anyone give some guidance?
Floor3 lxmxn Posted 2006-12-14 15:30
版主 Posts 4,938 Credits 11,386
###
  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:
Floor4 newice Posted 2006-12-14 16:31
初级用户 Posts 24 Credits 70
The dots at the back are not removed, only the ones at the front are removed.
Floor5 lxmxn Posted 2006-12-15 01:06
版主 Posts 4,938 Credits 11,386

  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.
Floor6 ccwan Posted 2006-12-15 01:30
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Indeed. Brother lxmxn's code is correct. Add points!
Floor7 lh82102849 Posted 2006-12-15 02:58
初级用户 Posts 11 Credits 24
Learned, feel that I am too bad
Thank you
Floor8 newice Posted 2006-12-15 03:20
初级用户 Posts 24 Credits 70
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
Floor9 newice Posted 2006-12-15 03:21
初级用户 Posts 24 Credits 70
<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.
Floor10 lxmxn Posted 2006-12-15 03:46
版主 Posts 4,938 Credits 11,386

  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.  
Floor11 namejm Posted 2006-12-15 03:53
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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:

Floor12 newice Posted 2006-12-15 04:04
初级用户 Posts 24 Credits 70
Thanks to the moderator and the great person for the help, extremely grateful!!!
Floor13 lxmxn Posted 2006-12-15 04:15
版主 Posts 4,938 Credits 11,386

  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.
Floor14 vkill Posted 2006-12-16 10:00
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023