Board logo

标题: [文本处理] 请教用for提取文本后为何有的行多出了空格 [打印本页]

作者: wxyeah    时间: 2014-10-30 10:05     标题: 请教用for提取文本后为何有的行多出了空格

代码如下,将附件的temp.txt分割,但是分割出来的第2个和第3个文件每行后面都产生了两个空格,请教是哪里出错了呢?谢谢
  1. echo on
  2. del d:\打印机1.txt d:\打印机2.txt d:\打印机3.txt d:\打印机4.txt d:\打印机5.txt
  3. if exist d:\temp.txt (echo 列表存在) else (goto eof)
  4. for /f "delims=" %%a in ('type d:\temp.txt ^|findstr /I "打印机IP=" ') do (echo %%a>>D:\打印机1.txt&&goto gname1 )
  5. :gname1
  6. for /f "delims=" %%a in ('type d:\temp.txt ^|findstr "打印机名称="') do (echo %%a>>D:\打印机1.txt%&&goto gmodel1)
  7. :gmodel1
  8. for /f "delims=" %%a in ('type  d:\temp.txt ^|findstr "打印机型号="') do (echo %%a>>D:\打印机1.txt&&goto ginf1)
  9. :ginf1
  10. for /f "delims=" %%a in ('type d:\temp.txt ^|findstr "打印机INF位置="') do (echo %%a>>D:\打印机1.txt&&goto p)
  11. :p
  12. setlocal enabledelayedexpansion
  13. set scount=0
  14. echo on
  15. :P1
  16. set /a scount=%scount%+1
  17. set /a fcount=%scount%+1
  18. set pf=打印机%fcount%.txt
  19. echo %pf%
  20. :gip
  21. 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))
  22. :gname
  23. for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt ^|findstr "打印机名称="') do (echo %%a>>D:\%pf% && goto gmodel)
  24. :gmodel
  25. for /f "skip=%scount% delims=" %%a in ('type  d:\temp.txt ^|findstr "打印机型号="') do (echo %%a>>D:\%pf% && goto ginf)
  26. :ginf
  27. 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 编辑

你应该根据最后一行内容做判断,空行直接被过滤了你那个判断没有意义
空格的问题.你把批处理中没必要的空格删除,文件路径加引号就可以了
这是修改后的内容
  1. @echo on
  2. del d:\打印机1.txt d:\打印机2.txt d:\打印机3.txt d:\打印机4.txt d:\打印机5.txt
  3. if exist d:\temp.txt (echo 列表存在) else (goto eof)
  4. for /f "delims=" %%a in ('type d:\temp.txt^|findstr /I "打印机IP="') do (echo %%a>>d:\打印
  5. 机1.txt&&goto gname1 )
  6. :gname1
  7. for /f "delims=" %%a in ('type d:\temp.txt^|findstr "打印机名称="') do (echo %%a>>d:\打印机
  8. 1.txt%&&goto gmodel1)
  9. :gmodel1
  10. for /f "delims=" %%a in ('type  d:\temp.txt^|findstr "打印机型号="') do (echo %%a>>d:\打印
  11. 机1.txt&&goto ginf1)
  12. :ginf1
  13. for /f "delims=" %%a in ('type d:\temp.txt^|findstr "打印机INF位置="') do (echo %%a>>d:\打
  14. 印机1.txt&&goto p)
  15. :p
  16. setlocal enabledelayedexpansion
  17. set scount=0
  18. echo on
  19. :P1
  20. set /a scount=%scount%+1
  21. set /a fcount=%scount%+1
  22. set pf=打印机%fcount%.txt
  23. echo %pf%
  24. :gip
  25. for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt^|findstr /I "打印机IP=" ') do (echo
  26. %%a>>"d:\%pf%"&&goto gname)
  27. :gname
  28. for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt^|findstr "打印机名称="') do (echo
  29. %%a>>"d:\%pf%"&&goto gmodel)
  30. :gmodel
  31. for /f "skip=%scount% delims=" %%a in ('type  d:\temp.txt^|findstr "打印机型号="') do (echo
  32. %%a>>"d:\%pf%"&&goto ginf)
  33. :ginf
  34. for /f "skip=%scount% delims=" %%a in ('type d:\temp.txt^|findstr "打印机INF位置="') do
  35. (echo %%a>>"d:\%pf%"&&goto :p1)
复制代码
我另外做了个批处理
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set a=0
  4. set m=d:\打印机
  5. for /f "skip=1 delims=*" %%a in (d:\temp.txt) do (
  6.     set "k=%%a"
  7.     if "!k:~0,5!"=="打印机IP" (
  8.         set /a a+=1
  9.         echo !k!>"!m!!a!.txt"
  10.     ) else (
  11.         echo !k!>>"!m!!a!.txt"
  12.     )
  13. )
  14. exit
复制代码

作者: wxyeah    时间: 2014-10-31 11:57

回复 3# nnszuo


    高手!非常感谢~~




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2