[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理将多个文本第二列内容行行对应合并成一行并在首行添加每个文本相应的文件名

批处理提取多个文本文件第二列数据并合并在一个文本里同时把所有的文本文件名字合并为新文本的第一列

一个主文件夹里有多个子文件夹,各个子文件夹里有多个文本文件,每个文本文件里有两列数据,先需要批处理将每个子文件夹里的文本文件的第二列数据提取出来各并,并将该子文件夹里的所有文本文件名插入到第一列
如:一个子文件夹里的文本文件的其中2个文本数据a,b
a.txt    b.txt                        a  b
1 2      1 3                           2 3
2 1      2 4                           1 4
3 6      3 4                           6 4
4 6      4 6                           6 6
5 3      5 7     最终结果:       3 7
6 5      6 3                           5 3
7 8      7 2                           8 2

  1. :on error resume next
  2. :sub class_bat
  3. echo off&cls
  4. cscript -nologo -e:vbs "%~0"
  5. pause
  6. exit sub
  7. :end sub
  8. dim file(1),list(1),result(1)
  9. set fso=createobject("scripting.filesystemobject")
  10. set regexp=new regexp
  11. File(0)="1.txt"
  12. File(1)="2.txt"
  13. regexp.global=-1:regexp.ignorecase=-1
  14. regexp.pattern="[\u4e00-\u9fff0\w]* *([\u4e00-\u9fff0\w]*)"
  15. for i=0 to 1
  16. read=fso.opentextfile(File(i)).readall
  17. list(i)=RegExp.Replace(read,"$1")
  18. result(i)=split(list(i),vbcrlf)
  19. next
  20. redim s(ubound(result(0)))
  21. for j=0 to ubound(result(0))
  22. s(j)=result(0)(j)&chr(32)&result(1)(j)
  23. next
  24. change=join(s,vbcrlf)
  25. wsh.echo "演示结果:"
  26. fso.createtextfile("new_result.txt").write change
  27. wsh.echo change
  28. set fso=nothing:set regexp=nothing
复制代码

TOP

回复 2# Nsqs


  说:  1>Microsoft VBScript 编译器错误:无效字符
并且子文件里面不是只有两个文件 是有几百个文件呢

TOP

本帖最后由 pcl_test 于 2016-12-7 23:21 编辑
  1. @echo off
  2. for /f "delims=" %%a in ('dir /a-d/b *.txt') do (
  3.     if not defined f (
  4.         (echo;%%~na&for /f "tokens=2" %%b in ('type "%%a"') do echo;%%b)>"合并.log"
  5.         set f=1
  6.     ) else (
  7.         setlocal enabledelayedexpansion
  8.         (
  9.             set /p str=
  10.             echo;!str! %%~na
  11.             for /f "tokens=2" %%b in ('type "%%a"') do (
  12.                 set /p str=
  13.                 echo;!str! %%b
  14.             )
  15.         )<"合并.log">"tmp.log"
  16.         endlocal
  17.         move /y "tmp.log" "合并.log"
  18.     )
  19. )
  20. pause
复制代码

TOP

回复 3# newbat123


    这个错误不用管它,正常的

TOP

回复 4# pcl_test


    我重新添加了附件结果,麻烦大神帮忙看一下,谢谢

TOP

回复 5# Nsqs


    但是没有出结果,我上传了个新的压缩包,解释需要的结果

TOP

回复 4# pcl_test


    就是相当于把第二列提取出来竖着合并

TOP

本帖最后由 newbat123 于 2016-12-13 18:19 编辑

就是相当于把第二列提取出来竖着合并

TOP

返回列表