China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 14:19
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Resolved] Help: Use a batch file to make webpage pagination links View 2,849 Replies 21
Original Poster Posted 2009-01-29 11:58 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
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 ]
Floor 2 Posted 2009-01-31 09:05 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
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.
Floor 3 Posted 2009-01-31 15:43 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Because it's too complicated...
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 4 Posted 2009-01-31 23:52 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
@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
精简
=> 个人网志
Floor 5 Posted 2009-02-01 16:43 ·  中国 浙江 温州 电信
初级用户
Credits 24
Posts 17
Joined 2008-08-22 23:24
17-year member
UID 123915
Gender Male
Status Offline
It doesn't work as-is, you need to explain how to use it.
Floor 6 Posted 2009-02-02 07:51 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
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 ]
Floor 7 Posted 2009-02-02 12:26 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
@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 ]
精简
=> 个人网志
Floor 8 Posted 2009-02-02 14:58 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
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!
Floor 9 Posted 2009-02-03 00:15 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
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.
精简
=> 个人网志
Floor 10 Posted 2009-02-03 10:16 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
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 ]
Floor 11 Posted 2009-02-03 16:10 ·  中国 广东 广州 番禺区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline

'Double-click to choose the directory to operate on
'Supports drag and drop

Dim oFSO,oSHE,oDCT
Set oFSO=CreateObject("Scripting.FileSystemObject")
Set oDCT=CreateObject("Scripting.Dictionary")
Set oSHE=CreateObject("Shell.Application")

If WScript.Arguments.Count > 0 Then
userPath=WScript.Arguments(0)
Else
userPath=Selectx
End If

If oFSO.FileExists(userPath) Then
userPath=oFSO.GetParentFolderName(userPath)
End If

Set oFolder=oFSO.GetFolder(userPath)
Set oFilesC=oFolder.Files

For Each objtmp In oFilesC
If InStr(1,objtmp.Type,"html",1) >0 Then
fileCount=fileCount+1
oDCT.Add fileCount,objtmp.Name
End If
Next

For j = 1 To oDCT.Count
If (j Mod 5) = 0 Then
k=k+5
Else
i=k+1
End If

otstr=vbCrLf
otstr=otstr & "<table border=""0"" align=""center"" cellpadding=""0"" cellspacing=""15""><tr>" & vbCrLf
otstr=otstr & "<td><a href=""" & oDCT.Item(1) & """>&#60;&#60;</a></td>" & vbCrLf
If oDCT.Exists(j-1) Then otstr=otstr & "<td><a href=""" & oDCT.Item(j-1) & """>Previous page</a></td>" & vbCrLf
otstr=otstr & "<td><a href=""" & oDCT.Item(i) & """>" & i & "</a></td>" & vbCrLf
If oDCT.Exists(i+1) Then otstr=otstr & "<td><a href=""" & oDCT.Item(i+1) & """>" & i+1 & "</a></td>" & vbCrLf
If oDCT.Exists(i+2) Then otstr=otstr & "<td><a href=""" & oDCT.Item(i+2) & """>" & i+2 & "</a></td>" & vbCrLf
If oDCT.Exists(i+3) Then otstr=otstr & "<td><a href=""" & oDCT.Item(i+3) & """>" & i+3 & "</a></td>" & vbCrLf
If oDCT.Exists(i+4) Then otstr=otstr & "<td><a href=""" & oDCT.Item(i+4) & """>" & i+4 & "</a></td>" & vbCrLf
If oDCT.Exists(j+1) Then otstr=otstr & "<td><a href=""" & oDCT.Item(j+1) & """>Next page</a></td>" & vbCrLf
otstr=otstr & "<td>" & j & "/" & oDCT.Count & "</td>" & vbCrLf
otstr=otstr & "<td><a href=""" & oDCT.Item(oDCT.Count) & """>&#62;&#62;</a></td>" & vbCrLf
otstr=otstr & "</tr></table>" & vbCrLf

Set oWrite=oFSO.OpenTextFile(userPath & "\" & oDCT.Item(j),8)
oWrite.WriteLine otstr
oWrite.Close

Next

Function Selectx
Set TMPath=oSHE.BrowseForFolder(0,"Choose the folder to modify:",0)
'oSHE.Open TMPath
If TMPath Is Nothing Then WScript.Quit
Selectx=TMPath.Self.Path
End Function



49206C6F766520796F752067757973 54656C3A3133383238343036373837
Floor 12 Posted 2009-02-03 19:48 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
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 ]
精简
=> 个人网志
Floor 13 Posted 2009-02-04 03:17 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
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 ]
Floor 14 Posted 2009-02-04 09:13 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HAT +2 2009-02-04 14:08
精简
=> 个人网志
Floor 15 Posted 2009-02-04 13:12 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
OK, it finally works. Sorry to trouble you, netbenton. Thanks!
Forum Jump: