青空文庫をSony Reader PRS-650用のePubに変換する その2 [IT]
以前青空文庫のファイルを分割するバッチファイルを載せたが、
・1行が長い文章だとうまく3分割できない
・ファイルの大きさでファイルを分割したい
など問題が生じたので、ファイルの大きさで分割するようバッチファイルを修正した。バッチファイル内の定数部分を変えれば好きな大きさでファイルを分割することができるようにした。
新たにperlとhead.batを使っているので、必要に応じてインストールされたし。
----------↓バッチファイルの中身-------------
@echo off
setlocal enabledelayedexpansion
REM ドラッグアンドドロップした青空文庫のZIPを展開して、短く分割する
REM SED.exeをパスの通った場所にインストールすること
REM head.batとtail.batをパスの通った場所にインストールすること
REM busybox.exeをパスの通った場所にインストールすること
REM ActivePerlをインストールすること
REM 2016/07/10 初版作成
REM 2016/11/18 動的にファイルを分割するようにした
REM ファイルをsplitする時のサイズ(kbyte)
SET split_size=150
for %%a in (%*) do (
REM 展開済みのフォルダを削除
rmdir /q /s "%%~na"
REM ZIPファイルを展開
"C:\Program Files\7-Zip\7z.exe" x -o"%%~na\" "%%~npxa" -y
REM 展開先フォルダに移動
cd "%%~na\"
REM 展開したファイルを元フォルダにコピー
copy *.txt ..\
for %%b in (*.txt) do (
REM 表題のファイルを作成(temp100)
sed -n -e 1,/---/p "%%~nb.txt" > "%%~nb_temp100.txt"
REM 【テキスト中に現れる記号について】ファイル(temp200)を作成
sed -n -e /---/,/---/p "%%~nb.txt" > "%%~nb_temp200.txt"
REM 表題を削ったファイル(temp210)を作成
sed -n -e /---/,$p "%%~nb.txt" > "%%~nb_temp210.txt"
REM 表題を削ったファイルからファイル1行目の----を削る(temp220)を作成
sed -n -e 2,$p "%%~nb_temp210.txt" > "%%~nb_temp220.txt"
REM 【テキスト中に現れる記号について】までを削ったファイル(temp230)を作成
sed -n -e /----/,$p "%%~nb_temp220.txt" > "%%~nb_temp230.txt"
REM 1行目の----を削る(temp240)
sed -n -e 2,$p "%%~nb_temp230.txt" > "%%~nb_temp240.txt"
REM 表題を削ったファイルからファイル1行目の----を削る(temp220)を作成
sed -n -e 2,$p "%%~nb_temp210.txt" > "%%~nb_temp220.txt"
REM 【テキスト中に現れる記号について】ファイル1行目の----を削る(temp300)
sed -n -e 2,$p "%%~nb_temp200.txt" > "%%~nb_temp300.txt"
REM 表題と【テキスト中に現れる記号について】ファイルを結合(ヘッダ部完成)(header)
type "%%~nb_temp100.txt" "%%~nb_temp300.txt" > "%%~nb_header.txt"
REM ヘッダ部分を削る(元の文から2回----が出るまで)削る(temp430)
sed -n -e /----/,$p "%%~nb.txt" > "%%~nb_temp405.txt"
sed -n -e 2,$p "%%~nb_temp405.txt" > "%%~nb_temp410.txt"
sed -n -e /----/,$p "%%~nb_temp410.txt" > "%%~nb_temp420.txt"
sed -n -e 2,$p "%%~nb_temp420.txt" > "%%~nb_temp430.txt"
REM フッタ部分を削る(本文完成)(main)
perl -0pe "s/\n\n\n\n/\njgokrpyht9iguyt\n\n\n/m" "%%~nb_temp430.txt" > "%%~nb_temp440.txt"
sed -n -e 1,/jgokrpyht9iguyt/p "%%~nb_temp440.txt" > "%%~nb_temp450.txt"
perl -0pe "s/jgokrpyht9iguyt\n//m" "%%~nb_temp450.txt" > "%%~nb_main.txt"
REM フッタ部を作成(footer)
sed -n -e /jgokrpyht9iguyt/,$p "%%~nb_temp440.txt" > "%%~nb_temp460.txt"
perl -0pe "s/jgokrpyht9iguyt//m" "%%~nb_temp460.txt" > "%%~nb_footer.txt"
REM 本文を指定したサイズ[kbyte]で分割
busybox split -b !split_size!k "%%~nb_main.txt" "%%~nb_splited_main.txt"
REM 各ファイルの先頭の行だけ抜き出す
set /a num=1
for %%c in (%%~nb_splited_main.txt*) do (
REM 先頭の行を抜き出す
call head "%%c" 1 > "%%~nc_sentou_!num!.txt"
set /a num=num+1
)
REM 次のファイルの先頭の行を足して、改行でファイルが切れるようにする
set /a num=1
set /a num2=2
for %%c in (%%~nb_splited_main.txt*) do (
if exist "%%~nc_sentou_!num2!.txt" (
type "%%c" "%%~nc_sentou_!num2!.txt" > "%%~nc_seiri_tail_!num!.txt"
) else (
type "%%c" > "%%~nc_seiri_tail_!num!.txt"
)
set /a num=num+1
set /a num2=num2+1
)
REM 次のファイルの先頭の行を削る(最初のファイル以外)(分割した本文完成)
set /a num=1
for %%c in (%%~nb_splited_main.txt*) do (
if !num!==1 (
type "%%~nc_seiri_tail_!num!.txt" > "%%~nc_seiri_head_tail_!num!.txt"
) else (
sed -n -e 2,$p "%%~nc_seiri_tail_!num!.txt" > "%%~nc_seiri_head_tail_!num!.txt"
)
set /a num=num+1
)
REM 分割した本文にヘッダとフッタを足して完成
set /a num=1
for %%c in (%%~nb_splited_main_seiri_head_tail_*.txt) do (
REM ヘッダに番号を付与
sed -e "2s/$/ !num!/" "%%~nb_header.txt" > "%%~nb_header_!num!.txt"
type "%%~nb_header_!num!.txt" "%%~nxc" "%%~nb_footer.txt" > "%%~nb_!num!.txt"
REM 生成ファイルをコピー
copy "%%~nb_!num!.txt" ..\
set /a num=num+1
)
)
REM 元フォルダに移動
cd ..\
REM 展開フォルダを削除
rmdir /q /s "%%~na\"
)
REM pause
exit
----------↑バッチファイルの中身-------------
コメント 0