首先说一下空行和空格问题,如下代码:
- @echo off
- echo --------------------------
- for /f %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- for /f "tokens=*" %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- for /f "tokens=* delims=" %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- for /f "tokens=* delims=/" %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- pause
hxuan?表ー: 2006-11-24 11:22
四个FOR中,第一个结果是a.xml文件中的所有空行,空格行及跳格符都不输出,并且每行中只输出了第一个空格和空格,空格和跳格,跳格和跳格之间的字符,也就是说默认以空格及跳格符为分格符,且只输出了分隔后的第一个内容,此时用%%j也不可输出第二个内容.
第二个结果是空行不输出,空格行变成了空行输出,跳格行变成了空行输出,并且每行最前面的空格和跳格也不输出了,这也是为什么空格行和跳格行成了空行.
第三个结果没有输出空行,其它是按原样输出了.
第四个和第三个一样的结果,这果的分隔符可以改成其它的一样,当然除了一些特殊的字符.
所以,空格和跳格主要是分隔符的问题,可以解决.
代码修改如下:
- @echo off
- setlocal enabledelayedexpansion
- set /a a=1
- for %%x in (*.xml) do (
- for /f "tokens=* delims=" %%i in (%%x) do (
- set "var=%%i"
- set "var=!var:300=100!"
- echo.!var!>>!a!.xml.love
- )
- set /a a=!a!+1
- )
hxuan?表ー: 2006-11-24 13:33
再说一下空行.1楼的
该脚本是要替换某目录下xml文本中的300为100。脚本能够正确修改,但是如果遇到空行(比如只有一个回车符,或者还有其它空格符,跳格符),空行会被替换成“300=100”,很郁闷,请教解决方案!谢谢啦!
好像不是空行会被替换成“300=100”,而是空格行和跳格行才对,空行就没有输出.
执行如下代码:
- @echo off
- setlocal enabledelayedexpansion
- set a=1
- for /f "tokens=* delims=" %%i in (a.xml) do (
- echo --!a!--%%i--
- set /a a+=1
- )
- pause
hxuan?表ー: 2006-11-24 14:10
结果是,FOR的选项怎么样设置,空行都不会输出.其实空行就没有被读取,直接跳过了.所以4楼的代码也是错误的,空行还是没有输出的.
我也是新手,说的不对的请高手指点一下,另外哪位高手可以解决一下空行的问题,我也想请教一下.
Last edited by hxuan999 on 2006-11-24 at 03:21 PM ]
First, let's talk about the issues of blank lines and spaces. The following is the code:
- @echo off
- echo --------------------------
- for /f %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- for /f "tokens=*" %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- for /f "tokens=* delims=" %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- for /f "tokens=* delims=/" %%i in (a.xml) do echo --"%%i"--
- echo --------------------------
- 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:
- @echo off
- setlocal enabledelayedexpansion
- set /a a=1
- for %%x in (*.xml) do (
- for /f "tokens=* delims=" %%i in (%%x) do (
- set "var=%%i"
- set "var=!var:300=100!"
- echo.!var!>>!a!.xml.love
- )
- set /a a=!a!+1
- )
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:
- @echo off
- setlocal enabledelayedexpansion
- set a=1
- for /f "tokens=* delims=" %%i in (a.xml) do (
- echo --!a!--%%i--
- set /a a+=1
- )
- 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 ]