Board logo

标题: 文本文件行列互置的批处理 [打印本页]

作者: Batcher    时间: 2009-6-14 13:47     标题: 文本文件行列互置的批处理

【问题】将文档内容横列变为纵列 a.txt内容如下: a b c d e f h i j k l m n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 a s d 11 12 13 14 15 要求输出结果如下: a h 1 6 a 11 b i 2 7 s 12 c j 3 8 d 13 d k 4 9 14 e l 5 10 15 f m 11 n 12 13 14 15 【方法1】效率是比较低的了
  1. @echo off
  2. for /f "delims=" %%i in (test.txt) do (
  3. set /a n+=1
  4. call set "str%%n%%=%%i "
  5. )
  6. for /l %%i in (1 1 10) do call :join
  7. pause>nul
  8. :split obj
  9. call set "var=%%%1%%"
  10. if "%var%"=="" set "str=" &goto :eof
  11. for %%i in (%var%) do (
  12. set "str=%%i"
  13. call set "%1=%%var:* =%%"
  14. goto :eof
  15. )
  16. :join
  17. set "tmp="
  18. for /l %%i in (1 1 %n%) do (
  19. call :split str%%i
  20. call set tmp=%%tmp%% %%str%%
  21. )
  22. echo.%tmp%
复制代码
【方法2】来段效率高点的代码,通过构造数组来实现,没有考虑特殊情况:
  1. @echo off
  2. :: 注意: set /p= 语句<nul前的空格是跳格键,在论坛中会被转换为空格
  3. :: 复制下来之后,请重新手工设置跳格,否则,将不能起到对齐的效果。
  4. set /a num=0,_num=0,max=0
  5. setlocal enabledelayedexpansion
  6. for /f "delims=" %%i in (test.txt) do (
  7. set /a num+=1,_num=0
  8. for %%j in (%%i) do (
  9. set /a _num+=1
  10. set str!num!_!_num!=%%j
  11. if !_num! geq !max! set max=!_num!
  12. )
  13. )
  14. for /l %%i in (1,1,%max%) do (
  15. for /l %%j in (1,1,%num%) do (
  16. set /p=!str%%j_%%i! <nul
  17. )
  18. echo.
  19. )
  20. pause
复制代码
【方法3】借用namejm的代码解决对齐的问题:
  1. @echo off
  2. set /a num=0,_num=0,max=0
  3. setlocal enabledelayedexpansion
  4. for /f "delims=" %%i in (test.txt) do (
  5. set /a num+=1,_num=0
  6. for %%j in (%%i) do (
  7. set /a _num+=1
  8. set str!num!_!_num!=%%j
  9. if !_num! geq !max! set max=!_num!
  10. )
  11. )
  12. for /l %%i in (1,1,%max%) do (
  13. set "res="
  14. for /l %%j in (1,1,%num%) do (
  15. set "var=!str%%j_%%i! "
  16. set "res=!res!!var:~0,5!"
  17. )
  18. echo !res!
  19. )
  20. pause
复制代码
【方法4】没想到批处理代码也不是很复杂,我把VBS的也贴上吧:
  1. Const ForReading = 1
  2. Const ForWriting = 2
  3. Set objFSO = CreateObject("Scripting.FileSystemObject")
  4. Set objFile = objFSO.OpenTextFile("In.txt",ForReading)
  5. Dim InputStr,lineIndex,TempArr,OutArr(100,100)
  6. Do Until objFile.AtEndOfStream
  7. InputStr = objFile.ReadLine
  8. TempArr = Split(InputStr," ")
  9. If UBound(TempArr) > MaxY Then MaxY = UBound(TempArr)
  10. For i = 0 To UBound(TempArr)
  11. OutArr(lineIndex,i) = TempArr(i)
  12. Next
  13. lineIndex = lineIndex + 1
  14. Loop
  15. objFile.Close
  16. Set objFile = objFSO.OpenTextFile("Out.txt",ForWriting,True)
  17. For i = 0 To MaxY
  18. For j = 0 To lineIndex - 1
  19. If OutArr(j,i) = "" Then OutArr(j,i) = " "
  20. OutStr = OutStr & OutArr(j,i) & " "
  21. Next
  22. objFile.WriteLine OutStr
  23. OutStr = ""
  24. Next
  25. objFile.Close
  26. Set objFile = Nothing
  27. Set objFSO = Nothing
复制代码
原文地址:http://www.cn-dos.net/forum/viewthread.php?tid=32931
作者: keen    时间: 2009-6-14 14:52

感谢batcher,总能给我们带回经典的批处理。




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