2009/02/27更新:增加了批量分割,优化了部分代码!
复制代码
- @echo off
- setlocal enabledelayedexpansion
- :begin
- color 31
- mode con lines=25 cols=60
- cls
- echo.
- echo ╭─────────╮
- echo | txt文本分割器 |
- echo ╰─────────╯
- echo ╔═══════════════════════╗
- echo ║ ║
- echo ║ 经常在mp4上看电子书的朋友经常会遇到一个 ║
- echo ║ ║
- echo ║ 问题:不得不把下载的很长的txt文本剪切粘贴为 ║
- echo ║ ║
- echo ║ 几个小的文本,方便阅读。本工具可以实现txt文 ║
- echo ║ ║
- echo ║ 本文件的分割。可以按照自己需要进行两种分割: ║
- echo ║ ║
- echo ║ 按行分割、按字数分割。具体操作请按提示进行, ║
- echo ║ ║
- echo ║ 按字数分割算法还有待提高! ║
- echo ║ ║
- echo ║ 更新(09/2/27):增加了批量操作,优化了部分代码。║
- echo ║ ║
- echo ║ code by lhjoanna ║
- echo ╚═══════════════════════╝
- echo.
- pause>nul
- :choice
- cls
- echo.&echo.&echo.
- echo ╔════════════╗
- echo ║ ║
- echo ║ [1] 批量分割 ║
- echo ║ ║
- echo ║ [2] 单个文件分割 ║
- echo ║ ║
- echo ║ [q] 返回上层 ║
- echo ║ ║
- echo ╚════════════╝
- echo.
- echo.
- set /p "choice_to_cut= 请输入分割方式[1-2]:"
- if !choice_to_cut! equ 1 goto cut_in_batch
- if !choice_to_cut! equ 2 goto cut_in_single
- if !choice_to_cut! equ q goto begin
- goto choice
- :cut_in_batch
- set folder=!folder_to_cut!
- echo.
- set /p "folder_to_cut= 请把包含文本文件的文件夹拖到此窗口(q返回):"
- if "!folder_to_cut!" equ "q" goto choice
- echo.
- cd /d !folder_to_cut! >nul 2>nul || echo INPUT NOT FOLDER && pause>nul && goto
- cut_in_batch
- call :cut_ways
- if !code_to_cut! equ 1 for /f "delims=" %%i in ('dir /b !folder_to_cut!') do call
- :cut_in_line "%%i" "ok"
- if !code_to_cut! equ 2 ( for /f "delims=" %%i in ('dir /b !folder_to_cut!') do call
- :cut_in_number "%%i" "ok")
- if !code_to_cut! equ q goto choice
- if not !code_to_cut! equ 1 ( if not !code_to_cut! equ 2 goto choice )
- echo 分割成功完成,任意键返回!&pause>nul&goto choice
- :cut_in_single
- set /p "txt_to_cut= 请把要分割的文本文件拖到此窗口(q返回):"
- if !txt_to_cut! equ q goto choice
- cd /d !folder_to_cut! >nul 2>nul && goto cut_in_single || echo.
- call :cut_ways
- if !code_to_cut! equ 1 call :cut_in_line "!txt_to_cut!"
- if !code_to_cut! equ 2 call :cut_in_number "!txt_to_cut!"
- if !code_to_cut! equ q goto choice
- if not !code_to_cut! equ 1 ( if not !code_to_cut! equ 2 goto choice )
- echo 分割成功完成,任意键返回!&pause>nul&goto choice
- :cut_ways
- cls
- echo.&echo.
- echo ╔════════════╗
- echo ║ ║
- echo ║ [1] 按行分割 ║
- echo ║ ║
- echo ║ [2] 按字数分割 ║
- echo ║ ║
- echo ║ [q] 返回上层 ║
- echo ║ ║
- echo ╚════════════╝
- echo.
- echo.
- set code_to_cut=
- set /p "code_to_cut= 请输入分割方式[1-2]:"
- goto :eof
- :cut_in_line
- cls
- set "tmp=%~1"
- call :ex_quation_marks "!tmp!"
- md !txt_to_cut_name!
- echo.&echo 当前文件:!txt_to_cut_name!
- echo.&echo 正在计算文本行数,请耐心等待...
- for /f "tokens=1* delims=:" %%i in ('findstr /n .* "!txt_to_cut!"') do (
- set lines=%%i
- set line=%%j
- set line=!line: =!
- echo !line!>>!txt_to_cut_name!_tmp.txt
- )
- echo.&echo 共有!lines!行
- :check
- set quantity_to_cut=
- echo.&set /p "quantity_to_cut= 请输入每段包含的行数:"
- if "!quantity_to_cut!" equ "" goto check
- echo !quantity_to_cut!|findstr "^[0-9]*$">nul||goto check
- if !quantity_to_cut! leq 0 goto check
- if !quantity_to_cut! gtr !lines! goto check
- set /a a=lines/quantity_to_cut
- set /a b=lines%%quantity_to_cut
- if !b! equ 0 set /a a-=1
- set aa=0
- :loop
- set c=0
- set /a bb=aa+1
- cd.>!txt_to_cut_name!\!txt_to_cut_name!!bb!.txt
- if !aa! equ 0 (
- set d=1
- set /p first=<!txt_to_cut!
- if "!first!" equ "" ( echo.>>!txt_to_cut_name!\!txt_to_cut_name!!bb!.txt ) else echo
- !first!>>!txt_to_cut_name!\!txt_to_cut_name!!bb!.txt
- set /a c+=1
- ) else set /a d=aa*quantity_to_cut
- for /f "usebackq skip=%d% delims=" %%j in ("!txt_to_cut_name!_tmp.txt") do (
- set /a c+=1
- if not !c! gtr !quantity_to_cut! (
- set "str=%%j"
- set str=!str: =!
- if "!str!" equ "" (echo.>>!txt_to_cut_name!\!txt_to_cut_name!!bb!.txt) else echo
- %%j>>!txt_to_cut_name!\!txt_to_cut_name!!bb!.txt
- )
- )
- set /a aa+=1
- if !aa! leq !a! goto loop
- echo.&echo 分割成功完成
- echo.&echo 分割后文件!txt_to_cut_name!已保存到!txt_to_cut_path!\!txt_to_cut_name!
- del "!txt_to_cut_name!_tmp.txt"
- if not "%~2" equ "" echo.&echo 准备处理下一个...
- ping -n 3 127.1>nul
- goto :eof
- :cut_in_number
- cls
- set "tmp=%~1"
- call :ex_quation_marks "!tmp!"
- md !txt_to_cut_name!
- echo.&echo 当前文件:!txt_to_cut_name!
- for /f "tokens=1* delims=:" %%i in ('findstr /n .* "!txt_to_cut!"') do (
- set line=%%j
- set line=!line: =!
- echo !line!>>!txt_to_cut_name!_tmp.txt
- )
- for /f "tokens=1 delims=:" %%i in ('findstr /o .* !txt_to_cut_name!_tmp.txt') do set /a
- length=%%i-2
- set /a length/=2
- echo.&echo 共有!length!个字符
- :check2
- set words_to_cut=
- echo.&set /p "words_to_cut= 请输入每段包含的字数:"
- if "!words_to_cut!" equ "" goto check2
- echo !words_to_cut!|findstr "^[0-9]*$">nul||goto check2
- if !words_to_cut! leq 0 goto check2
- if !words_to_cut! gtr !length! goto check2
- set /a a=length/words_to_cut
- set /a b=length%%words_to_cut
- if not !b! equ 0 set /a a+=1
- set part=0
- :loop1
- set /a part+=1
- set /a offset_begin=(part-1)*words_to_cut
- set /a offset_end=part*words_to_cut
- set size=0
- cd.>!txt_to_cut_name!\!txt_to_cut_name!!part!.txt
- for /f "tokens=1* delims=:" %%i in ('findstr /o .* !txt_to_cut_name!_tmp.txt') do (
- set /a size=%%i-2
- set /a size/=2
- if !size! geq !offset_begin! if !size! leq !offset_end! (
- if "%%j" equ "" (echo.>>!txt_to_cut_name!\!txt_to_cut_name!!part!.txt) else echo %%
- j>>!txt_to_cut_name!\!txt_to_cut_name!!part!.txt
- )
- )
- if !part! lss !a! goto loop1
- echo.&echo 分割成功完成
- echo.&echo 文件已保存到!txt_to_cut_path!\!txt_to_cut_name!\
- del "!txt_to_cut_name!_tmp.txt"
- if not "%~2" equ "" echo.&echo 准备处理下一个...
- ping -n 3 127.1>nul
- goto :eof
- :ex_quation_marks
- set "txt_to_cut=%~1"
- set "txt_to_cut_name=!txt_to_cut:~0,-4!"
- set "txt_to_cut_path=%~dp1"
- goto :eof
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |