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

[文本处理] [已解决]怎样用批处理实现根据特定字符串切分多个TXT文件?

比如当前目录下有多个TXT文件和多个嵌套目录,每个子目录下都有一批TXT文件,同时,所有TXT文件中都有多个特定字符串对文本内容进行了分隔(假设分隔符标志为#fengefu#),现在想遍历当前目录及各子目录包含的所有TXT文件,并根据#fengefu#这个符号将所有TXT文件进行切分,每两个#fengefu#分隔符之间的内容生成一个单独的TXT文件,并且在当前文件夹下新建一个qiefen目录,所有切分好的TXT文件按照序号放在qiefen目录里,请问用批处理怎样实现呢?谢谢了

已解决,感谢del兄及孤星兄

[ 本帖最后由 zxzl 于 2010-5-22 14:57 编辑 ]
1

评分人数

    • Batcher: 感谢主动给标题标注[已解决]字样PB + 2

这程序太完美了,DEL你是我偶像:)

TOP

  1. @echo off
  2. set "分隔符=zheshifengefu"
  3. set 分隔符_字数=13
  4. set "输出目录=qiefen"
  5. if exist "%输出目录%\*.txt" (
  6.     echo 错误:
  7.     echo,
  8.     echo     "%输出目录%" 内已经有 .txt 文件,请转移到其他地方!
  9.     echo,
  10.     set /p =按任意键退出. . . <nul
  11.     pause >nul
  12.     exit /b
  13. )
  14. echo 将分割当前目录名下的 .txt 文件
  15. echo,
  16. pause
  17. cls
  18. set /p =正在分割文件,请稍后. . . <nul
  19. echo 0 > .num
  20. md "%输出目录%"
  21. for /f "delims=" %%a in ('dir /a:-d /s /b *.txt') do (
  22.     findstr /n .* "%%a" > .tmp
  23.     for /f "delims=" %%b in (.tmp) do (
  24.         set Var=%%b
  25.         for /f %%n in (.num) do set /a n = %%n
  26.         SetLocal EnableDelayedExpansion
  27.         if "!Var:*:=!"=="" (
  28.             if !n! neq 0 echo,>>"!输出目录!\!n!.txt"
  29.         ) else (
  30.             set Var=!Var:*:=!
  31.             if "!Var:~0,%分隔符_字数%!"=="!分隔符!" (
  32.                 set /a n += 1
  33.                 echo !n!>.num
  34.                 set Var=!Var:~%分隔符_字数%!
  35.             )
  36.             echo,!Var!>>"!输出目录!\!n!.txt"
  37.         )
  38.         EndLocal
  39.     )
  40. )
  41. cls
  42. del .tmp .num
  43. echo 文件分割完成!
  44. echo,
  45. set /p =按任意键退出. . . <nul
  46. pause >nul
复制代码
1

评分人数

TOP

请问孤星这个功能怎样用批处理来实现啊?

TOP

如果可以,我希望是如何用批处理整理这些文件
努力学习,努力挣分

TOP

原帖由 hanyeguxing 于 2010-5-20 23:01 发表 [url=redirect.php?goto=findpost&pid=52577&ptid=8001][/url]
看了楼主提供的示例:
1,一共四行,为什么不直接按行分隔呢?
2,如果每个文本实际超过四行,要只提取存在分隔符的行,那么是否存在的分隔符都在行首?


1、这个一共四行只是我举的一个例子,实际情况每个TXT文件都有很多不固定行和很多分隔符,肯定无法按行分割的。
2、是的,存在的分隔符都在行首,但是不能只提取存在分隔符的行。这个具体是什么情况呢,比如我有1000篇的文章需要二次整理,每一篇文章都是一个独立的TXT文件,但是一个一个整理太麻烦,所以就先在每一个TXT文件最前面插入一个分隔记号,然后以50个文件为一组,将所有TXT文件合并成20个大的TXT文件,等整理完毕之后再将这20个大的TXT文件拆分成原来的1000个TXT文件,这个分隔记号就要我要拆分的依据,我这样说是否更明白点呢?

TOP

看了楼主提供的示例:
1,一共四行,为什么不直接按行分隔呢?
2,如果每个文本实际超过四行,要只提取存在分隔符的行,那么是否存在的分隔符都在行首?
寒夜孤星:在没有说明的情况下,本人所有代码均运行在 XP SP3 下 (有问题请发贴,QQ临时会话已关闭)

TOP

孤星兄的这个代码我不太明白,既然以“#fengefu#”作为分隔标记,为什么代码里没有“#fengefu#“呢?#号只是我举的一个分割标记的例子没什么特殊含义,为了说的更清楚一点,我再举个例子吧。比如像下面这个TXT文件

zheshifengefuThere are many ways on how you can lose weight quickly. Rapid weight loss is achieved through sheer dedication and proper motivation. The way you change your lifestyle is also important to sustain a physically fit body. You should consciously set your goals so that you can motivate yourself in losing that unwanted weight, the most important thing to remember is to maintain a healthy quality way of life.

zheshifengefuThe best way to lose weight fast is to reduce your daily calorie intake. You do not have to starve yourself to death. You simply have to stop heaving junk food. You can readily have an instant soup instead of a greasy burger for lunch. You should also avoid eating fried foods. You will find that the grilled chicken meat is as good as the fried one, but much healthier. You also need to reduce the sugary foods you consume to the very minimum. You have to forget about the chocolate bars you have for snacks and about the ice cream after dinner.

zheshifengefuYou can have any other foods including meat and dairy, but you have to improve your eating habits as well. This is another effective method with which you can slim down quickly. It is essential for you to have a hearty, but not fattening breakfast. The oatmeal is an excellent choice. You should also have a good meal at lunch. Your dinner meal has to be as small as possible. It is good to start your lunch and dinner with a big filling salad without any dressing. The two snacks in between the meals are important for effective weight loss. You can have any fruit or nuts.

zheshifengefuOne of the really fast ways to lose weight is to drink more fluids. The water should be your top choice. By having eight glasses a day you will improve the functioning of your metabolism. Also, when you drink water before a meal you will eat less. You can readily replace this drink with green tea. It speeds up the metabolism even more. Plus, it aids digestion and gives you more energy.


假设上面的内容是一个TXT文件,这里我以”zheshifengefu“这个字符串作为分隔标记(分隔标记是所有TXT文件在未切分之前就已加好的),目的是把这个TXT文件分成下面4个单独的TXT文件:

There are many ways on how you can lose weight quickly. Rapid weight loss is achieved through sheer dedication and proper motivation. The way you change your lifestyle is also important to sustain a physically fit body. You should consciously set your goals so that you can motivate yourself in losing that unwanted weight, the most important thing to remember is to maintain a healthy quality way of life.

The best way to lose weight fast is to reduce your daily calorie intake. You do not have to starve yourself to death. You simply have to stop heaving junk food. You can readily have an instant soup instead of a greasy burger for lunch. You should also avoid eating fried foods. You will find that the grilled chicken meat is as good as the fried one, but much healthier. You also need to reduce the sugary foods you consume to the very minimum. You have to forget about the chocolate bars you have for snacks and about the ice cream after dinner.

You can have any other foods including meat and dairy, but you have to improve your eating habits as well. This is another effective method with which you can slim down quickly. It is essential for you to have a hearty, but not fattening breakfast. The oatmeal is an excellent choice. You should also have a good meal at lunch. Your dinner meal has to be as small as possible. It is good to start your lunch and dinner with a big filling salad without any dressing. The two snacks in between the meals are important for effective weight loss. You can have any fruit or nuts.

One of the really fast ways to lose weight is to drink more fluids. The water should be your top choice. By having eight glasses a day you will improve the functioning of your metabolism. Also, when you drink water before a meal you will eat less. You can readily replace this drink with green tea. It speeds up the metabolism even more. Plus, it aids digestion and gives you more energy.


这里我设定的分隔标记是”zheshifengefu“,那么遍历整个目录所有TXT文件并按照这个标记进行切分的完整代码是什么呢?谢谢了

[ 本帖最后由 zxzl 于 2010-5-20 22:52 编辑 ]

TOP

1:#fengefu#前后没有其他字符
2:分隔符号为#
  1. @echo off&setlocal enabledelayedexpansion
  2. set n=1&md qiefen\
  3. for /f "delims=" %%a in ('dir *.txt /b/s/a-d') do if /i not "%%~dpa"=="%cd%\qiefen\" (for /f "delims=#" %%b in ('findstr "#.*#" "%%a"') do (
  4.     set/a a+=1
  5.     echo.%%b
  6.     if !a!==2 set a=0&set/a n+=1
  7. ))>>qiefen\!n!.txt
复制代码

楼主最好提供文本内容示例!

[ 本帖最后由 hanyeguxing 于 2010-5-20 19:59 编辑 ]
1

评分人数

寒夜孤星:在没有说明的情况下,本人所有代码均运行在 XP SP3 下 (有问题请发贴,QQ临时会话已关闭)

TOP

返回列表