不借助第三方工具的代码
- @echo off
- if exist tmp.txt del tmp.txt
- for /f "tokens=1 delims=" %%a in (test.txt) do set "var=%%a" & call :change
- goto exit
- :change
- set "var=%var:^=^^%"
- set "var=%var:>=^>%"
- set "var=%var:<=^<%"
- set "var=%var:&=^&%"
- set "var=%var:|=^|%"
- set /p "=%var%" <nul >>tmp.txt
- echo. >>tmp.txt
- :exit
BJSH发表于: 2007-03-29 18:08
想说一下第十一行和十二行;
因为几乎每一行都含有特殊符号
虽然都添加了一个^符号但是用echo写入其他文本就会去掉^
使得所得tmp.txt内容和原文本是一模一样的;
如果用^^来做echo 会当作不存在^来执行
比如echo ^^&和echo & 是一样的都会报错
想过用set var >>tmp.txt 可是每一行又都多出var=...
又想过用for来"tokens=2 delims=var="可是后面的又怎么输出呢;用echo?还是前面的问题.
终于发现了set var"= " <nu >>tmp.txtl的有个好的特性;
一般我用它的目的都是不换行;这次用做写入文本的算是一个创新吧
随后再加上 echo.
终于解决了问题....
总结自己的收获
echo 对于特殊字符写入文本的问题;
用 set /p ''= " <nul >> & echo.
解决了
Last edited by bjsh on 2007-3-29 at 06:30 PM ]
Code without third-party tools
- @echo off
- if exist tmp.txt del tmp.txt
- for /f "tokens=1 delims=" %%a in (test.txt) do set "var=%%a" & call :change
- goto exit
- :change
- set "var=%var:^=^^%"
- set "var=%var:>=^>%"
- set "var=%var:<=^<%"
- set "var=%var:&=^&%"
- set "var=%var:|=^|%"
- set /p "=%var%" <nul >>tmp.txt
- echo. >>tmp.txt
- :exit
BJSH posted on: 2007-03-29 18:08
Want to talk about lines eleven and twelve;
Because almost every line contains special symbols
Although each line has added a ^ symbol, but when writing to other text with echo, the ^ will be removed
So that the content of the resulting tmp.txt is exactly the same as the original text;
If you use ^^ for echo, it will be executed as if the ^ does not exist
For example, echo ^^& and echo & are the same and will both report an error
Thought about using set var >>tmp.txt, but each line will have var=... added
Also thought about using for "tokens=2 delims=var=" but how to output the following; use echo? Still the previous problem.
Finally found that set var"= " <nu >>tmp.txtl has a good feature;
Generally, I use its purpose to not wrap lines; this time using it to write to text is an innovation
Then add echo.
Finally solved the problem....
Summarize my gains
echo for the problem of writing special characters to text;
Use set /p ''= " <nul >> & echo.
Solved
Last edited by bjsh on 2007-3-29 at 06:30 PM ]