Board logo

标题: [文本处理] 【已解决】如何将文本数据由横排变竖排,且删除空格 [打印本页]

作者: 思想之翼    时间: 2018-2-11 12:37     标题: 【已解决】如何将文本数据由横排变竖排,且删除空格

本帖最后由 思想之翼 于 2018-4-7 01:16 编辑

欲将文本数据横排变竖排,下述代码是将文本数据由
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
变成
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

欲删除空格,变成
1111
2222
3333
4444

下述代码如何修改?
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "D:\统计\"
  4. for %%f in (*.txt) do (
  5. set /p str=<"%%f"
  6. set n=0
  7. for %%i in (!str!) do (
  8. set /a n+=1
  9. )
  10. for /l %%a in (1 1 !n!) do call :aa %%a "%%f"
  11. move $ "%%f"
  12. )
  13. pause & exit
  14. :aa
  15. set s=
  16. for /f "tokens=%1" %%b in ('type %2') do (
  17. set "s=!s!%%b "
  18. )
  19. >>$ echo,!s:~0,-1!
  20. goto :eof
复制代码

作者: yhcfsr    时间: 2018-2-11 16:45

  1. @echo off
  2. set "SrcDir=D:\统计"
  3. for /f "delims=" %%a in ('dir/a-d/s/b "%SrcDir%\*.txt"') do CALL :convert "%%a"
  4. exit
  5. :convert (FILE_PATH)
  6. setlocal enabledelayedexpansion
  7. set/a n=1
  8. :lp
  9. set "str="
  10. for /f "tokens=%n% delims= " %%a in ('type "%~1"') do set "str=!str!%%a"
  11. if "%str%"=="" (endlocal&goto:eof) else (echo.%str%>>"%~1.log")
  12. set/a n+=1&goto:lp
复制代码

作者: 思想之翼    时间: 2018-2-11 18:11

回复 2# yhcfsr

感谢!代码满足要求。如何才能覆盖原文本数据?
作者: yhcfsr    时间: 2018-2-11 18:17

  1. @echo off
  2. set "SrcDir=D:\统计"
  3. for /f "delims=" %%a in ('dir/a-d/s/b "%SrcDir%\*.txt"') do CALL :convert "%%a"&&move /y "%%a.log" "%%a"
  4. exit
  5. :convert (FILE_PATH)
  6. setlocal enabledelayedexpansion
  7. set/a n=1
  8. :lp
  9. set "str="
  10. for /f "tokens=%n% delims= " %%a in ('type "%~1"') do set "str=!str!%%a"
  11. if "%str%"=="" (endlocal&goto:eof) else (echo.%str%>>"%~1.log")
  12. set/a n+=1&goto:lp
复制代码
回复 3# 思想之翼




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