标题: [文本处理] 请教用for提取文本后为何有的行多出了空格 [打印本页]
作者: wxyeah 时间: 2014-10-30 10:05 标题: 请教用for提取文本后为何有的行多出了空格
代码如下,将附件的temp.txt分割,但是分割出来的第2个和第3个文件每行后面都产生了两个空格,请教是哪里出错了呢?谢谢- echo on
- del d:\打印机1.txt d:\打印机2.txt d:\打印机3.txt d:\打印机4.txt d:\打印机5.txt
-
- if exist d:\temp.txt (echo 列表存在) else (goto eof)
- for /f "delims=" %%a in ('type d:\temp.txt ^|findstr /I "打印机IP=" ') do (echo %%a>>D:\打印机1.txt&&goto gname1 )
- :gname1
- for /f "delims=" %%a in ('type d:\temp.txt ^|findstr "打印机名称="') do (echo %%a>>D:\打印机1.txt%&&goto gmodel1)
- :gmodel1
- for /f "delims=" %%a in ('type d:\temp.txt ^|findstr "打印机型号="') do (echo %%a>>D:\打印机1.txt&&goto ginf1)
- :ginf1
- for /f "delims=" %%a in ('type d:\temp.txt ^|findstr "打印机INF位置="') do (echo %%a>>D:\打印机1.txt&&goto p)
-
- :p
- setlocal enabledelayedexpansion
- set scount=0
- echo on
- :P1
- set /a scount=%scount%+1
- set /a fcount=%scount%+1
- set pf=打印机%fcount%.txt
- echo %pf%
-
- :gip
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt ^|findstr /I "打印机IP=" ') do (if %%a=="" (pause) else (echo %%a>>D:\%pf% && goto gname))
- :gname
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt ^|findstr "打印机名称="') do (echo %%a>>D:\%pf% && goto gmodel)
- :gmodel
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt ^|findstr "打印机型号="') do (echo %%a>>D:\%pf% && goto ginf)
- :ginf
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt ^|findstr "打印机INF位置="') do (echo %%a>>D:\%pf% && goto :p1)
复制代码
另外gip那段本来想用if来判断是否分割完了,但好像不成功,麻烦帮忙改进一下,谢谢
作者: wxyeah 时间: 2014-10-30 10:10
不好意思刚才忘了传附件,已经上传了,谢谢
作者: nnszuo 时间: 2014-10-31 00:43
本帖最后由 nnszuo 于 2014-10-31 11:14 编辑
你应该根据最后一行内容做判断,空行直接被过滤了你那个判断没有意义
空格的问题.你把批处理中没必要的空格删除,文件路径加引号就可以了
这是修改后的内容- @echo on
- del d:\打印机1.txt d:\打印机2.txt d:\打印机3.txt d:\打印机4.txt d:\打印机5.txt
-
- if exist d:\temp.txt (echo 列表存在) else (goto eof)
- for /f "delims=" %%a in ('type d:\temp.txt^|findstr /I "打印机IP="') do (echo %%a>>d:\打印
-
- 机1.txt&&goto gname1 )
- :gname1
- for /f "delims=" %%a in ('type d:\temp.txt^|findstr "打印机名称="') do (echo %%a>>d:\打印机
-
- 1.txt%&&goto gmodel1)
- :gmodel1
- for /f "delims=" %%a in ('type d:\temp.txt^|findstr "打印机型号="') do (echo %%a>>d:\打印
-
- 机1.txt&&goto ginf1)
- :ginf1
- for /f "delims=" %%a in ('type d:\temp.txt^|findstr "打印机INF位置="') do (echo %%a>>d:\打
-
- 印机1.txt&&goto p)
-
- :p
- setlocal enabledelayedexpansion
- set scount=0
- echo on
- :P1
- set /a scount=%scount%+1
- set /a fcount=%scount%+1
- set pf=打印机%fcount%.txt
- echo %pf%
-
- :gip
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt^|findstr /I "打印机IP=" ') do (echo
-
- %%a>>"d:\%pf%"&&goto gname)
- :gname
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt^|findstr "打印机名称="') do (echo
-
- %%a>>"d:\%pf%"&&goto gmodel)
- :gmodel
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt^|findstr "打印机型号="') do (echo
-
- %%a>>"d:\%pf%"&&goto ginf)
- :ginf
- for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt^|findstr "打印机INF位置="') do
-
- (echo %%a>>"d:\%pf%"&&goto :p1)
复制代码
我另外做了个批处理- @echo off
- setlocal enabledelayedexpansion
- set a=0
- set m=d:\打印机
- for /f "skip=1 delims=*" %%a in (d:\temp.txt) do (
- set "k=%%a"
- if "!k:~0,5!"=="打印机IP" (
- set /a a+=1
- echo !k!>"!m!!a!.txt"
- ) else (
- echo !k!>>"!m!!a!.txt"
- )
- )
- exit
复制代码
作者: wxyeah 时间: 2014-10-31 11:57
回复 3# nnszuo
高手!非常感谢~~
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |