[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
把n个1列的文件合并为一个n列的文件:output.txt
file1.txt  file2.txt file3.txt file4.txt .........
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /a n=2
  4. set str=
  5. type nul>output.txt
  6. for /l %%a in (1,1,!n!) do (
  7.   set /a n1=0
  8.   for /f %%b in ('type file%%a.txt') do (
  9.     set /a n1+=1
  10.     for /l %%c in (!n1!,1,!n1!) do (
  11.        set file%%a.row%%c=%%b
  12.     )
  13.   )
  14. )
  15. for /l %%a in (1,1,!n1!) do (
  16.   for /l %%b in (1,1,!n!) do (
  17.     set "str=!str!!file%%b.row%%a!   "
  18.   )
  19.   echo !str!>>output.txt
  20.   set str=
  21. )
复制代码

TOP

修改了下,不足的用0来补。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /a n=3
  4. set /a m=0
  5. set str=
  6. type nul>output.txt
  7. for /l %%a in (1,1,!n!) do (
  8.   set /a n1=0
  9.   for /f %%b in ('type file%%a.txt') do (
  10.     set /a n1+=1
  11.     for /l %%c in (!n1!,1,!n1!) do (
  12.        set file%%a.row%%c=%%b
  13.     )
  14.   )
  15.   if !n1! gtr !m! (set /a m=!n1!)
  16. )
  17. for /l %%a in (1,1,!m!) do (
  18.   for /l %%b in (1,1,!n!) do (
  19.     if "!file%%b.row%%a!"=="" (set /a file%%b.row%%a=0)
  20.     set "str=!str!!file%%b.row%%a!   "
  21.   )
  22.   echo !str!>>output.txt
  23.   set str=
  24. )
复制代码

TOP

  1. @echo off
  2. echo {>yiwuyun.txt
  3. echo FS=",">>yiwuyun.txt
  4. echo if(FNR==37){a1=$4}>>yiwuyun.txt
  5. echo if(FNR==38){a2=$4}>>yiwuyun.txt
  6. echo if(FNR==39){a3=$4;printf "%%s,%%s,%%s\n",a1,a2,a3}>>yiwuyun.txt
  7. echo }>>yiwuyun.txt
  8. gawk -f yiwuyun.txt *.csv>new.csv
  9. if exist yiwuyun.txt del yiwuyun.txt
复制代码

TOP

  1. @echo off & setlocal enabledelayedexpansion
  2. for /l %%i in (37,1,39) do (
  3. set /a n=%%i-1
  4. set /a n1=%%i-36
  5. for /f "delims=" %%a in ('dir /b *.csv') do (
  6.         for /f "skip=!n! delims=, tokens=4" %%b in ('type "%%a"') do (
  7.                 if "!a!"=="" >>file!n1!.txt echo %%b
  8.                 set a=1
  9.         )
  10.         set a=
  11. )
  12. )
  13. set /a n=3
  14. set /a m=0
  15. set str=
  16. type nul>new.csv
  17. for /l %%a in (1,1,!n!) do (
  18.   set /a n1=0
  19.   for /f %%b in ('type file%%a.txt') do (
  20.     set /a n1+=1
  21.     for /l %%c in (!n1!,1,!n1!) do (
  22.        set file%%a.row%%c=%%b
  23.     )
  24.   )
  25.   if !n1! gtr !m! (set /a m=!n1!)
  26. )
  27. for /l %%a in (1,1,!m!) do (
  28.   for /l %%b in (1,1,!n!) do (
  29.     if "!file%%b.row%%a!"=="" (set /a file%%b.row%%a=0)
  30.     set "str=!str!!file%%b.row%%a!   "
  31.   )
  32.   echo !str!>>new.csv
  33.   set str=
  34. )
复制代码
试试。

TOP

  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. set /a start=36
  4. for /l %%a in (37,1,39) do (
  5.   set /a fileNumber=%%a-!start!
  6.   set /a skip=0
  7.   for /f "delims=" %%b in ('dir /b *.csv') do (
  8.      for /f "tokens=4 delims=," %%c in ('type %%b') do (
  9.        set /a skip+=1
  10.        if "!skip!"=="%%a" (
  11.          echo %%c>>file!fileNumber!.txt
  12.        )
  13.      )
  14.      set /a skip=0
  15.   )
  16. )
  17. set /a n=3
  18. set /a m=0
  19. set str=
  20. type nul>new.csv
  21. for /l %%a in (1,1,!n!) do (
  22.   set /a n1=0
  23.   for /f %%b in ('type file%%a.txt') do (
  24.     set /a n1+=1
  25.     for /l %%c in (!n1!,1,!n1!) do (
  26.        set file%%a.row%%c=%%b
  27.     )
  28.   )
  29.   if !n1! gtr !m! (set /a m=!n1!)
  30. )
  31. for /l %%a in (1,1,!m!) do (
  32.   for /l %%b in (1,1,!n!) do (
  33.     if "!file%%b.row%%a!"=="" (set /a file%%b.row%%a=0)
  34.     set "str=!str!!file%%b.row%%a!   "
  35.   )
  36.   echo !str!>>new.csv
  37.   set str=
  38. )
复制代码

TOP

  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. set /a start=36
  4. for /l %%a in (37,1,39) do (
  5.   set /a fileNumber=%%a-!start!
  6.   set /a skip=0
  7.   for /f "delims=" %%b in ('dir /b *.csv') do (
  8.      for /f "tokens=4 delims=," %%c in ('type %%b') do (
  9.        set /a skip+=1
  10.        if "!skip!"=="%%a" (
  11.          echo %%c>>file!fileNumber!.txt
  12.        )
  13.      )
  14.      set /a skip=0
  15.   )
  16. )
  17. set /a n=3
  18. set /a m=0
  19. set str=
  20. type nul>new.csv
  21. for /l %%a in (1,1,!n!) do (
  22.   set /a n1=0
  23.   for /f %%b in ('type file%%a.txt') do (
  24.     set /a n1+=1
  25.     for /l %%c in (!n1!,1,!n1!) do (
  26.        set file%%a.row%%c=%%b
  27.     )
  28.   )
  29.   if !n1! gtr !m! (set /a m=!n1!)
  30. )
  31. for /l %%a in (1,1,!m!) do (
  32.   for /l %%b in (1,1,!n!) do (
  33.     if "!file%%b.row%%a!"=="" (set /a file%%b.row%%a=0)
  34.     if "%%b"=="!n!" (
  35.       set "str=!str!!file%%b.row%%a!"
  36.     ) else (
  37.       set "str=!str!!file%%b.row%%a!,"
  38.     )
  39.   )
  40.   echo !str!>>new.csv
  41.   set str=
  42. )
复制代码
逗号分隔

TOP

  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. set /a rowStart=37
  4. set /a rowEnd=39
  5. set /a colNumber=4
  6. set "fileSeperator=,"
  7. set "fileType=csv"
  8. if "!fileType!"=="csv" (
  9.   set "allFile=*.csv"
  10. ) else (
  11.   set "allFile=*.txt"
  12. )
  13. if "!fileSeperator!"=="," (
  14.   set outputFile=new.csv
  15. ) else (
  16.   set outputFile=new.txt
  17. )
  18. set /a fileCount=!rowEnd!-!rowStart!+1
  19. for /l %%a in (!rowStart!,1,!rowEnd!) do (
  20.   set /a fileNumber=%%a-!rowStart!+1
  21.   set /a skip=0
  22.   for /f "delims=" %%b in ('dir /b !allFile!') do (
  23.      for /f "tokens=%colNumber% delims=," %%c in ('type %%b') do (
  24.        set /a skip+=1
  25.        if "!skip!"=="%%a" (
  26.          echo %%c>>file!fileNumber!.txt
  27.        )
  28.      )
  29.      set /a skip=0
  30.   )
  31. )
  32. set /a n=!fileCount!
  33. set /a m=0
  34. set str=
  35. type nul>!outputFile!
  36. for /l %%a in (1,1,!n!) do (
  37.   set /a n1=0
  38.   for /f %%b in ('type file%%a.txt') do (
  39.     set /a n1+=1
  40.     for /l %%c in (!n1!,1,!n1!) do (
  41.        set file%%a.row%%c=%%b
  42.     )
  43.   )
  44.   if !n1! gtr !m! (set /a m=!n1!)
  45. )
  46. for /l %%a in (1,1,!m!) do (
  47.   for /l %%b in (1,1,!n!) do (
  48.     if "!file%%b.row%%a!"=="" (set /a file%%b.row%%a=0)
  49.     if "%%b"=="!n!" (
  50.       set "str=!str!!file%%b.row%%a!"
  51.     ) else (
  52.        if "!fileSeperator!"=="," (
  53.          set "str=!str!!file%%b.row%%a!,"
  54.        ) else (
  55.          set "str=!str!!file%%b.row%%a!    "
  56.        )
  57.     )
  58.   )
  59.   echo !str!>>!outputFile!
  60.   set str=
  61. )
  62. for /l %%a in (1,1,!fileCount!) do (
  63.   if exist ".\file%%a.txt" del ".\file%%a.txt"
  64. )
复制代码
这个稍稍通用一点

TOP

回复 44# haichuan5121
你用40楼的代码,很快的,几百个文件也不会超过10秒吧。
我是写着玩的,哪晓得你要那么多文件。根本就不考虑效率。
你把40楼的代码的文件名改成vbs结尾,不要用bat结尾。主要是你没把用途写清楚,一直都在变。别人当然就不考虑那么多了。
1

评分人数

TOP

返回列表