일괄 처리 — 많은 파일 이름을 HTML 코드로 변환하는 방법은 무엇입니까?

일괄 처리 — 많은 파일 이름을 HTML 코드로 변환하는 방법은 무엇입니까?

다음과 같은 이름의 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"

삼.쉼표로 구분된 루프 에 사용되도록 설정하기 위해 대문자로 변수를 정의합니다 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 태그를 추가하고 a를 사용하여 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까지 _의 구분 기호로 가정합니다 .tokens1-7*

  for /f tokens^=1-7*delims^=_ %%i in ('where .:*.pdf')do ...

7.if쉼표 유무에 관계없이 문자열 구성과 일치하는 파일 확장자가 있는 가능한 각 토큰에 대한 조치를 비교하려면 an을 사용하십시오 .

....)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_alfif /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!"
    )
...


추가 리소스:

관련 정보