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-21 11:59
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch modification of blank lines in files View 2,991 Replies 10
Original Poster Posted 2006-11-23 07:38 ·  中国 北京 联通
新手上路
Credits 14
Posts 5
Joined 2006-11-23 06:44
19-year member
UID 71449
Gender Male
Status Offline
I want to use a piece of code to batch modify part of the content in *.xml files. The script is as follows:
@echo off
setlocal enabledelayedexpansion
set /a a=1
for %%x in (*.xml) do (
for /f "tokens=*" %%i in (%%x) do (
set var=%%i
set "var=!var:300=100!"
echo !var!>>!a!.xml.love
)
set /a a=!a!+1
)
This script is to replace 300 with 100 in the xml text of a certain directory. The script can modify correctly, but if it encounters an empty line (such as only one carriage return, or there are other spaces, tab characters), the empty line will be replaced with "300=100", which is very annoying. Please ask for a solution! Thank you!
Floor 2 Posted 2006-11-23 08:47 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Brother btandd, it's best for you to pass or paste the content of the .XML file that has the problem so that everyone can take a look, and then we can correctly and accurately help you test~ : )
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-11-23 14:28 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

You can first determine whether a certain line in the .xml file contains "300". If it does, process it; if not, skip it.
Floor 4 Posted 2006-11-23 22:17 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
@echo off
setlocal enabledelayedexpansion
set /a a=1
for %%x in (*.xml) do (
for /f "tokens=*" %%i in (%%x) do (set "var=%%i"
if not !var!.==. (set "var=!var:300=100!"
echo !var!!>>!a!.xml.love))
set /a a=!a!+1)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
btandd +1 2006-11-23 23:26
Floor 5 Posted 2006-11-23 22:59 ·  中国 北京 联通
新手上路
Credits 14
Posts 5
Joined 2006-11-23 06:44
19-year member
UID 71449
Gender Male
Status Offline
Originally posted by Must Love at 2006-11-23 09:17 AM:
@echo off
setlocal enabledelayedexpansion
set /a a=1
for %%x in (*.xml) do (
for /f "tokens=*" %%i in (%%x) do (set "var=%%i"
if not !var!.==. (set "var=!var:300=100! ...


Must Love is a good comrade! Correct. Can you explain why this situation occurs? Also, I found that the spaces in front of each line in the replaced file are deleted. Can this situation be avoided?
Floor 6 Posted 2006-11-23 23:08 ·  中国 北京 联通
新手上路
Credits 14
Posts 5
Joined 2006-11-23 06:44
19-year member
UID 71449
Gender Male
Status Offline
There is another problem. That is, if the characters that need to be escaped, such as <, > and other characters, it seems that it doesn't work normally. How to escape?
Floor 7 Posted 2006-11-25 03:15
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
First, let's talk about the issues of blank lines and spaces. The following is the code:



  1. @echo off
  2. echo --------------------------
  3. for /f %%i in (a.xml) do echo --"%%i"--
  4. echo --------------------------
  5. for /f "tokens=*" %%i in (a.xml) do echo --"%%i"--
  6. echo --------------------------
  7. for /f "tokens=* delims=" %%i in (a.xml) do echo --"%%i"--
  8. echo --------------------------
  9. for /f "tokens=* delims=/" %%i in (a.xml) do echo --"%%i"--
  10. echo --------------------------
  11. pause
hxuan?表ー: 2006-11-24 11:22


Among the four FORs, the first result is that all blank lines, space lines, and tab lines in the a.xml file are not output, and only the characters between the first space and space, space and tab, tab and tab in each line are output. That is to say, spaces and tabs are used as default delimiters, and only the first content after separation is output. At this time, using %%j cannot output the second content.

The second result is that blank lines are not output, space lines become blank lines output, tab lines become blank lines output, and the spaces and tabs at the front of each line are also not output. This is why space lines and tab lines become blank lines.

The third result has no blank lines output, and the others are output as original.

The fourth result has the same result as the third. The delimiter here can be changed to others, of course, except for some special characters.

So, spaces and tabs are mainly issues of delimiters, which can be solved.

The code is modified as follows:



  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /a a=1
  4. for %%x in (*.xml) do (
  5. for /f "tokens=* delims=" %%i in (%%x) do (
  6. set "var=%%i"
  7. set "var=!var:300=100!"
  8. echo.!var!>>!a!.xml.love
  9. )
  10. set /a a=!a!+1
  11. )
hxuan?表ー: 2006-11-24 13:33


Then talk about blank lines. The post on floor 1:


This script is to replace 300 with 100 in the xml text in a certain directory. The script can modify correctly, but if there is a blank line (such as only one carriage return, or there are other space characters, tab characters), the blank line will be replaced with "300=100", which is very depressed. Please ask for a solution! Thank you!


  It seems that it's not that blank lines are replaced with "300=100", but space lines and tab lines. Blank lines have no output.

  Execute the following code:



  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set a=1
  4. for /f "tokens=* delims=" %%i in (a.xml) do (
  5. echo --!a!--%%i--
  6. set /a a+=1
  7. )
  8. pause
hxuan?表ー: 2006-11-24 14:10


The result is that no matter how the options of FOR are set, blank lines are not output. In fact, blank lines are not read and are directly skipped. So the code on floor 4 is also wrong, and blank lines are still not output.

  I am also a newbie. Please give pointers if what I said is wrong. Also, if any expert can solve the problem of blank lines, I also want to ask.

[ Last edited by hxuan999 on 2006-11-24 at 03:21 PM ]
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 8 Posted 2006-11-25 03:33 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 9 Posted 2006-11-25 03:40 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
Adding the judgment of blank lines with findstr /v /n . a.xml will be fine!

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 10 Posted 2006-11-25 04:24
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
Originally posted by Must Love at 2006-11-24 15:33:
The code on the 4th floor is not incorrect; it's code that removes pure blank lines.


That's right, it's not incorrect itself, just removes blank lines. But it doesn't solve the problem in this thread.

Above,

Adding findstr /v /n . a.xml for blank line judgment will work!


I'm not familiar with FINDSTR. Could you write out your code? Thanks.

[ Last edited by hxuan999 on 2006-11-24 at 04:27 PM ]
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 11 Posted 2006-11-25 06:36
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
How to save the last line of text as another text?

Saw this post and found the solution, thanks to the 9th floor first.



  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /a a=1
  4. for %%x in (*.xml) do (
  5. for /f "tokens=1* delims=:" %%i in ('findstr /n .* %%x') do (
  6. set "var=.%%j"
  7. set "var=!var:300=100!"
  8. echo.!var:~1!>>!a!.xml.love
  9. )
  10. set /a a=!a!+1
  11. )
hxuan?表ー: 2006-11-24 17:36


In this way, the problem of empty lines is also solved.

[ Last edited by hxuan999 on 2006-11-24 at 06:38 PM ]
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Forum Jump: