![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-12 16:47 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » [Resolved] Help: Use a batch file to make webpage pagination links |
| Printable Version 2,855 / 21 |
| Floor1 ganjie | Posted 2009-01-29 11:58 |
| 初级用户 Posts 37 Credits 49 | |
|
I recently downloaded some e-books, and after extracting them they were all htm webpages. Since I want to package them into CHM format, I want to add pagination links to the webpages.
![]() Suppose the htm filenames are: A, B, C, D, ... (these filenames are just assumed for the convenience of posting, don't misunderstand), and suppose the last one is S. For example, the pagination links for the C file are as follows: <table widtd="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td scope="col"><a href="A.htm">First page</a></td> <td scope="col"><a href="B.htm">Previous page</a></td> <td scope="col"><a href="A.htm">1</a></td> <td scope="col"><a href="B.htm">2</a></td> <td scope="col"><a href="C.htm">3</a></td> <td scope="col"><a href="D.htm">4</a></td> <td scope="col"><a href="E.htm">5</a></td> <td scope="col"><a href="F.htm">Next page</a></td> <td scope="col">3/19</td> <td scope="col"><a href="S.htm">Last page</a></td> </tr> </table> To keep the layout simpler, below is a simplified form, but the effect produced by the batch file should match the htm code above. The pagination rules are as follows: (the numbers below mean the filename's position after sorting by name, so A is 1, C is 3, F is 5, while the 19 in 1/19, 3/19, etc. is because we assume there are 19 files total) A First page A A1 B2 C3 D4 E5 1/19 Last page S make up the four after it B First page A A1 B2 C3 D4 E5 2/19 Last page S make up the three after it C First page A A1 B2 C3 D4 E5 3/19 Last page S because there are two before and two after it, no compensation is needed D First page A B2 C3 D4 E5 F6 4/19 Last page S E First page A C3 D4 E5 F6 G7 5/19 Last page S F First page A D4 E5 F6 G7 H8 6/19 Last page S If S is the last one and its corresponding number is 19, then make up the four before it S First page A O15 P16 Q17 R18 S19 Last page S If R is the last one and its corresponding number is 19, then make up the three before it R First page A O15 P16 Q17 R18 S19 Last page S If Q is the third from last, then since it already has two before and two after it, no compensation is needed Q First page A O15 P16 Q17 R18 S19 Last page S All of the above is for when the file count is 5 or more If there is only one file: A A1 1/1 If there are two files: A First page A A1 B2 1/2 Last page B make up one A First page A A1 B2 2/2 Last page B If there are three files: A First page A A1 B2 C3 1/3 Last page C make up two A First page A A1 B2 C3 2/3 Last page C make up one C A First page A A1 B2 2/3 Last page C If there are four files: A First page A A1 B2 C3 D4 1/4 Last page D make up three A First page A A1 B2 C3 D4 2/4 Last page D make up two C First page A A1 B2 C3 D4 3/4 Last page D make up one A First page A A1 B2 C3 D4 4/4 Last page D I've been hanging around the forum for quite a while, but to realize the pagination above I actually have to make 5 separate batch files, so I'm asking the experts to help me write a batch file. Thanks! [ Last edited by HAT on 2009-2-4 at 14:08 ] |
|
| Floor2 ganjie | Posted 2009-01-31 09:05 |
| 初级用户 Posts 37 Credits 49 | |
|
Can someone help me? I'm asking the experts for a hand. If there's anything in the post that's unclear and that's why you can't reply, you can reply and ask. My ability to express myself is really limited; I don't know whether I've still left out something in the post.
|
|
| Floor3 slore | Posted 2009-01-31 15:43 |
| 铂金会员 Posts 2,478 Credits 5,212 | |
|
Because it's too complicated...
|
|
| Floor4 netbenton | Posted 2009-01-31 23:52 |
| 银牌会员 Posts 752 Credits 1,916 From 广西 | |
|
@echo off&setlocal enabledelayedexpansion
set/a n=0 for %%a in (*.htm) do ( set /a n+=1 set fn!n!=%%a ) set "z1=<table widtd="500" border="0" align="center" cellpadding="0" cellspacing="0">" set "z2=<tr>" set "z3=<td scope="col"><a href="!fn1!">First page</a></td>" set "z8=<td scope="col"><a href="!fn%n%!">Last page</a></td>" set "z9=</tr>" set "z10=</table>" if !n! gtr 1 goto noly (echo !z1!&echo !z2! set "z5=<td scope="col"><a href="!fn1!">1</a></td>" set "z7=<td scope="col">1/1</td>" echo !z5!&echo !z7!&echo !z9!&echo !z10!)>>!fn1! goto :eof :noly set/a ms=1-4,mb=1+4 for /l %%a in (1,1,!n!) do ( echo Writing file !fn%%a! set sn=0 for /l %%b in (!ms!,1,!mb!) do ( set/a sn+=1 set/a xo!sn!=%%b ) set xn=0 set "z7=<td scope="col">%%a/!n!</td>" (echo.!z1!&echo.!z2!&echo.!z3!&call :paup call :save !xo1!,!xo6! !xo2!,!xo7! !xo3!,0 !xo4!,0 !xo5!,0 !xo6!,0 !xo7!,0 !xo8!,0 !xo9!,0 call :pado echo.!z7!&echo.!z8!&echo.!z9!&echo.!z10!)>>!fn%%a! set/a ms+=1,mb+=1 ) echo Writing complete pause goto :eof :paup if defined fn%xo4% set "z4=<td scope="col"><a href="!fn%xo4%!">Previous page</a></td>"&echo !z4! goto :eof :pado if defined fn%xo6% set "z6=<td scope="col"><a href="!fn%xo6%!">Next page</a></td>"&echo !z6! goto :eof :save if "%2"=="" goto :eof if defined fn%1 ( if not defined fn%2 (set "pr=<td scope="col"><a href="!fn%1!">%1</a></td>"&!echo !pr!&set/a xn+=1) ) if !xn!==5 goto :eof shift shift goto :save |
|
| Floor5 haolongo | Posted 2009-02-01 16:43 |
| 初级用户 Posts 17 Credits 24 | |
|
It doesn't work as-is, you need to explain how to use it.
|
|
| Floor6 ganjie | Posted 2009-02-02 07:51 |
| 初级用户 Posts 37 Credits 49 | |
|
Test succeeded, thanks netbenton, but your script reads all the htm files in the current directory. I want to read the htm files in all folders under the current directory, and save them in their respective folders as %%k\%%i.txt
@echo off setlocal enabledelayedexpansion set "route=%cd%" for /f "delims=" %%k in ('dir /ad /b "%route%"') do ( for /f "delims=" %%i in ('dir /b /a-d %%k\*.htm') do ( ………………………… ………………………… ) ) Because folders like the novels of Huang Yi, Gu Long, Liu Sheng, etc. are separate and independent, please help me modify it again, okay? Thanks! [ Last edited by ganjie on 2009-2-2 at 07:58 ] |
|
| Floor7 netbenton | Posted 2009-02-02 12:26 |
| 银牌会员 Posts 752 Credits 1,916 From 广西 | |
|
@echo off
::Automatically build CHM-format webpage links ::Traverse all subdirectories under the specified directory; each subdirectory gets its own independent CHM ::A chmchm.abc file will be generated in a processed directory as a processed marker. if '%1'=='' echo.&echo Please drag the root directory to be processed onto this batch file!&echo Or specify the root directory on the command line!&echo For example: %~n0 d:\&pause&exit if '%1'=='#benton#' goto :begin pushd %1 if %errorlevel% gtr 0 echo.Sorry! The directory you specified does not exist.&pause&exit for /f "tokens=* delims=" %%a in ('dir /ad /s /b^&cd') do ( pushd %%a call %0 #benton# popd ) if not '%test%=='t echo No *.htm files found under directory "%cd%" popd pause goto :eof :begin if not exist *.htm goto :eof set test=t&echo. if exist chmchm.abc echo Directory "%cd%" already has a chm, skipping this time,&echo.If you want to add it again, please delete chmchm.abc in that directory and try again.&goto :eof echo Directory %cd% echo.>chmchm.abc setlocal enabledelayedexpansion set/a n=0 for %%a in (*.htm) do ( set /a n+=1 set fn!n!=%%a ) set "z1=<table widtd="500" border="0" align="center" cellpadding="0" cellspacing="0">" set "z2=<tr>" set "z3=<td scope="col"><a href="!fn1!">First page</a></td>" set "z8=<td scope="col"><a href="!fn%n%!">Last page</a></td>" set "z9=</tr>" set "z10=</table>" if !n! gtr 1 goto noly (echo !z1!&echo !z2! set "z5=<td scope="col"><a href="!fn1!">1</a></td>" set "z7=<td scope="col">1/1</td>" echo !z5!&echo !z7!&echo !z9!&echo !z10!)>>!fn1! goto :eof :noly set/a ms=1-4,mb=1+4 for /l %%a in (1,1,!n!) do ( echo Writing file: !fn%%a! set sn=0 for /l %%b in (!ms!,1,!mb!) do ( set/a sn+=1 set/a xo!sn!=%%b ) set xn=0 set "z7=<td scope="col">%%a/!n!</td>" (echo.!z1!&echo.!z2!&echo.!z3!&call :paup call :save !xo1!,!xo6! !xo2!,!xo7! !xo3!,0 !xo4!,0 !xo5!,0 !xo6!,0 !xo7!,0 !xo8!,0 !xo9!,0 call :pado echo.!z7!&echo.!z8!&echo.!z9!&echo.!z10!)>>!fn%%a! set/a ms+=1,mb+=1 ) endlocal echo Writing complete goto :eof :paup if defined fn%xo4% set "z4=<td scope="col"><a href="!fn%xo4%!">Previous page</a></td>"&echo !z4! goto :eof :pado if defined fn%xo6% set "z6=<td scope="col"><a href="!fn%xo6%!">Next page</a></td>"&echo !z6! goto :eof :save if "%2"=="" goto :eof if defined fn%1 ( if not defined fn%2 (set "pr=<td scope="col"><a href="!fn%1!">%1</a></td>"&!echo !pr!&set/a xn+=1) ) if !xn!==5 goto :eof shift shift goto :save [ Last edited by netbenton on 2009-2-4 at 09:04 ] |
|
| Floor8 ganjie | Posted 2009-02-02 14:58 |
| 初级用户 Posts 37 Credits 49 | |
|
netbenton, I don't know how to run this batch file, but I looked through the code and found that you misunderstood what I meant. What I want is to add pagination to the htm files in each novel collection, for example Huang Yi's novels have 01.htm, 02.htm, etc., and Gu Long's also has similar htm-format novels. I want each subfolder to generate its own separate pagination. Note that they should be independent, not related. For example, the code in your post #4 generates it very well; I just want every subfolder to have that same effect!
|
|
| Floor9 netbenton | Posted 2009-02-03 00:15 |
| 银牌会员 Posts 752 Credits 1,916 From 广西 | |
|
Save all the code as a batch file.
In Explorer, drag the directory to be processed (the root directory) onto this batch file. What it does is add them independently to each subdirectory, and it also marks subdirectories that have already been processed, so even if you drag it onto it again, it won't add them a second time. |
|
| Floor10 ganjie | Posted 2009-02-03 10:16 |
| 初级用户 Posts 37 Credits 49 | |
|
Still no good. I've already dragged the root directory or a subfolder onto this batch file many times, but the batch window disappears in an instant, and none of the htm files have any content changed. I tried several directories, still no response at all! Please help modify it, change the root directory to the current directory %cd%, thanks!
[ Last edited by ganjie on 2009-2-3 at 10:21 ] |
|
| Floor11 everest79 | Posted 2009-02-03 16:10 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
| Floor12 netbenton | Posted 2009-02-03 19:48 |
| 银牌会员 Posts 752 Credits 1,916 From 广西 | |
|
No way, did you copy it completely? I tested it myself!
I'll just upload it directly, download it and try it. http://upload.cn-dos.net/img/1289.rar [ Last edited by netbenton on 2009-2-3 at 19:50 ] |
|
| Floor13 ganjie | Posted 2009-02-04 03:17 |
| 初级用户 Posts 37 Credits 49 | |
|
Thanks to netbenton for specially uploading the file. I tested it carefully myself, and after dragging the directory onto it, the batch window still disappears immediately, and none of the htm files have any content changes. There really is no response at all! Could you change the root directory to the current directory %cd%, or let me specify the folder path? Thanks! everest79's vbs doesn't support a root directory, and the link order in the pagination isn't what I want either; I want the current page centered, with two on the left and two on the right!
[ Last edited by ganjie on 2009-2-4 at 04:09 ] |
|
| Floor14 netbenton | Posted 2009-02-04 09:13 |
| 银牌会员 Posts 752 Credits 1,916 From 广西 | |
|
Try this. Copy it into the root directory to be processed and open it directly.
@echo off ::If %1 specifies the directory to process, then process that directory; if the given directory does not exist or is not specified, then process the current directory. if '%1'=='#benton#' goto :bengin if not '%1==' pushd %1 for /f "tokens=* delims=" %%a in ('dir /ad /s /b^&cd') do ( pushd %%a call %0 #benton# popd ) if not '%test%=='t echo No *.htm files found under directory "%cd%" if not '%1==' popd pause goto :eof :bengin if not exist *.htm goto :eof set test=t&echo. if exist chmchm.abc echo Directory "%cd%" already has a chm, skipping this time,&echo.If you want to add it again, please delete chmchm.abc in that directory and try again.&goto :eof echo Directory %cd% echo.>chmchm.abc setlocal enabledelayedexpansion set/a n=0 for %%a in (*.htm) do ( set /a n+=1 set fn!n!=%%a ) set "z1=<table widtd="500" border="0" align="center" cellpadding="0" cellspacing="0">" set "z2=<tr>" set "z3=<td scope="col"><a href="!fn1!">First page</a></td>" set "z8=<td scope="col"><a href="!fn%n%!">Last page</a></td>" set "z9=</tr>" set "z10=</table>" if !n! gtr 1 goto noly (echo !z1!&echo !z2! set "z5=<td scope="col"><a href="!fn1!">1</a></td>" set "z7=<td scope="col">1/1</td>" echo !z5!&echo !z7!&echo !z9!&echo !z10!)>>!fn1! goto :eof :noly set/a ms=1-4,mb=1+4 for /l %%a in (1,1,!n!) do ( echo Writing file: !fn%%a! set sn=0 for /l %%b in (!ms!,1,!mb!) do ( set/a sn+=1 set/a xo!sn!=%%b ) set xn=0 set "z7=<td scope="col">%%a/!n!</td>" (echo.!z1!&echo.!z2!&echo.!z3!&call :paup call :save !xo1!,!xo6! !xo2!,!xo7! !xo3!,0 !xo4!,0 !xo5!,0 !xo6!,0 !xo7!,0 !xo8!,0 !xo9!,0 call :pado echo.!z7!&echo.!z8!&echo.!z9!&echo.!z10!)>>!fn%%a! set/a ms+=1,mb+=1 ) endlocal echo Writing complete goto :eof :paup if defined fn%xo4% set "z4=<td scope="col"><a href="!fn%xo4%!">Previous page</a></td>"&echo !z4! goto :eof :pado if defined fn%xo6% set "z6=<td scope="col"><a href="!fn%xo6%!">Next page</a></td>"&echo !z6! goto :eof :save if "%2"=="" goto :eof if defined fn%1 ( if not defined fn%2 (set "pr=<td scope="col"><a href="!fn%1!">%1</a></td>"&!echo !pr!&set/a xn+=1) ) if !xn!==5 goto :eof shift shift goto :save |
|
| Floor15 ganjie | Posted 2009-02-04 13:12 |
| 初级用户 Posts 37 Credits 49 | |
|
OK, it finally works. Sorry to trouble you, netbenton. Thanks!
|
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |