
我有一個包含 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.在每行的開頭和 bat 的最後一行之後新增必要的 html 標籤,並使用 afindstr /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
循環作為命令,僅列出資料夾中的文件,並假設為分隔符_
,從tokens
1 到 8 1-7*
:
for /f tokens^=1-7*delims^=_ %%i in ('where .:*.pdf')do ...
7.使用 anif
來比較具有檔案副檔名的每個可能標記的操作,該標記將匹配帶或不帶逗號的字串的組成:
....)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!"
)
...
其他資源: