
次のような名前のPDFファイルが入ったフォルダがあります
page_1_excercise_2.pdf
page_23_excercise_3_4_and_5.pdf
page_456_excercise_16_and_17.pdf
数字には 1 桁、2 桁、または 3 桁が含まれる場合があります。次のような HTML コードを生成する必要があります。
<a href="pdfs/page_1_excercise_2.pdf">Page 1, Excercise 2</a>
<a href="pdfs/page_23_excercise_3_4_and_5.pdf">Page 23, Excercise 3, 4 and 5</a>
<a href="pdfs/page_456_excercise_16_and_17.pdf">Page 456, Excercise 16 and 17</a>
Windows でバッチファイルを使用してこれを行う方法はありますか?
これまでに私が見つけたものは次のとおりです:
@echo off
Setlocal EnableDelayedExpansion
for /f "tokens=*" %%i in ('dir /b /a:-d *.pdf') do (
set filename=%%i
set filename=!filename:.pdf=!
set filename=!filename:_= !
echo ^<a href="pdfs/%%i"^>!filename!^</a^> >> files.html)
問題なく動作しますが、各演習番号の後にカンマを追加し、最初の文字 (「ページ 456...」の「P」など) を大文字に変換したいと思います。
答え1
@echo off && setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0" && for %%i in ("%cd%")do set "_dir=%%~nxi"
set "_alf=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
>.\Output.html (
findstr /be ^<.*^> "%~f0"
for /f tokens^=1-7*delims^=_ %%i in ('where .:*.pdf')do (
call %:^) "%%~ni" "%%~k" _str_1 _str_2 "%_alf%" && if "%%~xl" == ".pdf" (
echo;^<a href="%_dir%/%%~ni_%%~j_%%~k_%%~nxl"^>!_str_1! %%~j, !_str_2! %%~nl^</a^>
)else if "%%~xn" == ".pdf" (
echo;^<a href="%_dir%/%%~ni_%%~j_%%~k_%%~l_%%~m_%%~nxn"^>!_str_1! %%~j, !_str_2! %%~l %%~m %%~nn^</a^>
)else if "%%~xo" == ".pdf" (
echo;^<a href="%_dir%/%%~ni_%%~j_%%~k_%%~l_%%~m_%%~n_%%~nxo"^>!_str_1! %%~j, !_str_2! %%~l, %%~m %%~n %%~no^</a^>
)
)
echo\^</body^>&echo\^</html^>) & endlocal & goto :eof
%:^)
set "_str#1=%~1" && set "_str#2=%~2" && for %%i in (%~5)do (
if /i "!_str#1:~0,1!" == "%%~i" set "%~3=%%~i!_str#1:~1!"
if /i "!_str#2:~0,1!" == "%%~i" set "%~4=%%~i!_str#2:~1!"
)
exit /b
<!doctype html>
<html>
<head>
<title>Our Funky HTML Page</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
</head>
<body>
- HTML ファイル出力:
<!doctype html>
<html>
<head>
<title>Our Funky HTML Page</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
</head>
<body>
<a href="Q1728787/page_1_excercise_2.pdf">Page 1, Excercise 2</a>
<a href="Q1728787/page_23_excercise_3_4_and_5.pdf">Page 23, Excercise 3, 4 and 5</a>
<a href="Q1728787/page_456_excercise_16_and_17.pdf">Page 456, Excercise 16 and 17</a>
</body>
</html>
1.PDF ファイルがあるフォルダーを入力するか、同じフォルダーで bat を実行します。
cd /d "%~dp0"
:: or
cd /d "D:\Full\Path\To\Your\PDFs\Folder"
2.現在のフォルダ名のみを取得して変数に保存します:
for %%i in ("%cd%")do set "_dir=%%~nxi"
3.for
カンマで区切られたループ内で使用するため、大文字で変数を定義します。
set "_alf=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
4.HTML ファイル内の文字列処理ブロックをリダイレクトします。
>.\Output.html (
...
...
)
5.必要な HTML タグを bat の各行の先頭と最後の行の後に追加し、を使用してfindstr /Begin /End <one or more characters>
HTML ファイルにフィルター処理します。
...
findstr /be ^<.*^> "%~f0"
...
<!doctype html>
<html>
<head>
<title>Our Funky HTML Page</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
</head>
<body>
6.for /f
フォルダー内のファイルのみをリストするコマンドとしてループを使用し、 1 から 8 まで_
の区切り文字を想定します。tokens
1-7*
for /f tokens^=1-7*delims^=_ %%i in ('where .:*.pdf')do ...
7。if
ファイル拡張子を持つ各トークンに対して、コンマの有無にかかわらず文字列の構成に一致するアクションを比較するには、を使用します。
....)do ... && if "%%~xl" == ".pdf" (
echo ...
)else if "%%~xn" == ".pdf" (
echo ...
)else if "%%~xo" == ".pdf" (
echo ...
)
8.拡張子が に等しいすべてのケースでは.pdf
、関数内の文字列を処理して最初の文字を大文字に置き換え、各変数のトークンとその名前を既に取得し、文字列を保存して目的の出力を作成します。
...)do call %:^) "%%~ni" "%%~k" _str_1 _str_2 "%_alf%" && if ... (
echo;^<a href="%_dir%/%%~ni_%%~j_%%~k_%%~nxl"^>!_str_1! %%~j, !_str_2! %%~nl^</a^>
)else if ... (
echo;^<a href="%_dir%/%%~ni_%%~j_%%~k_%%~l_%%~m_%%~nxn"^>!_str_1! %%~j, !_str_2! %%~l %%~m %%~nn^</a^>
)else if ... (
echo;^<a href="%_dir%/%%~ni_%%~j_%%~k_%%~l_%%~m_%%~n_%%~nxo"^>!_str_1! %%~j, !_str_2! %%~l, %%~m %%~n %%~no^</a^>
)
9.文字列を関数に渡し、大文字のアルファベット変数も渡しますpage
。excercise
ループ_alf
内で、の使用時に無感覚ケースに一致する最初の文字の置換によって、if /i
置換された部分文字列が実行されます。
...)do call %:^) "%%~ni" "%%~k" _str_1 _str_2 "%_alf%" ... (
....
)
%:^)
set "_str#1=%~1" && set "_str#2=%~2" && for %%i in (%~5)do (
if /i "!_str#1:~0,1!" == "%%~i" set "%~3=%%~i!_str#1:~1!"
if /i "!_str#2:~0,1!" == "%%~i" set "%~4=%%~i!_str#2:~1!"
)
...
追加リソース: