中国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-10 18:03
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Batch modification of blank lines in files
Printable Version  3,058 / 10
Floor1 btandd Posted 2006-11-23 07:38
新手上路 Posts 5 Credits 14
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!
Floor2 redtek Posted 2006-11-23 08:47
金牌会员 Posts 1,147 Credits 2,902
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~ : )
Floor3 lxmxn Posted 2006-11-23 14:28
版主 Posts 4,938 Credits 11,386

You can first determine whether a certain line in the .xml file contains "300". If it does, process it; if not, skip it.
Floor4 不得不爱 Posted 2006-11-23 22:17
超级版主 Posts 2,044 Credits 5,310 From 四川南充
@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)
Floor5 btandd Posted 2006-11-23 22:59
新手上路 Posts 5 Credits 14
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?
Floor6 btandd Posted 2006-11-23 23:08
新手上路 Posts 5 Credits 14
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?
Floor7 hxuan999 Posted 2006-11-25 03:15
中级用户 Posts 161 Credits 337
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 ]
Floor8 不得不爱 Posted 2006-11-25 03:33
超级版主 Posts 2,044 Credits 5,310 From 四川南充
The code on the 4th floor is not incorrect; it's the code that has removed pure blank lines
Floor9 tao0610 Posted 2006-11-25 03:40
高级用户 Posts 218 Credits 579
Adding the judgment of blank lines with findstr /v /n . a.xml will be fine!
Floor10 hxuan999 Posted 2006-11-25 04:24
中级用户 Posts 161 Credits 337
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 ]
Floor11 hxuan999 Posted 2006-11-25 06:36
中级用户 Posts 161 Credits 337
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 ]
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023