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-07 02:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [fixed] How to replace links in HTML files View 822 Replies 10
Original Poster Posted 2007-12-31 12:09 ·  中国 北京 电信
初级用户
Credits 22
Posts 9
Joined 2007-12-28 13:08
18-year member
UID 107031
Gender Male
Status Offline
I want to append a fixed string after href="/ in HTML files, for example: change href="/abc.html"
to href="/aaa/abc.html"
When I tried using set, I found that
1. The = " in the original string all need to be escaped, otherwise it can't be replaced correctly;
2. All the Chinese in the new file turns into garbled text
Below is the code I wrote. Please give me some guidance, experts, I would be very grateful! :)
---------------------------
@echo off
for /f "delims=" %%i in ('dir /a /b /s *.html') do (
for /f "delims=" %%a in (%%i) do (
set var=%%a
set "var=!var:%%href="/=href=”/aaa/%%!"
echo !var!>>%%i.txt
)
del %%i
ren "%%i.txt" %%i
)
---------------------

[ Last edited by samuel on 2008-1-1 at 07:46 PM ]
Floor 2 Posted 2007-12-31 12:45 ·  中国 浙江 杭州 余杭区 电信
高级用户
★★
Credits 537
Posts 219
Joined 2007-08-04 09:43
19-year member
UID 94620
Gender Male
From 杭州--半山
Status Offline
Floor 3 Posted 2007-12-31 17:32 ·  中国 北京 电信
初级用户
Credits 22
Posts 9
Joined 2007-12-28 13:08
18-year member
UID 107031
Gender Male
Status Offline
Floor 4 Posted 2008-01-01 00:05 ·  中国 浙江 杭州 电信
高级用户
★★
Credits 537
Posts 219
Joined 2007-08-04 09:43
19-year member
UID 94620
Gender Male
From 杭州--半山
Status Offline
Post the test file so we can take a look
Floor 5 Posted 2008-01-01 00:46 ·  中国 北京 电信
初级用户
Credits 22
Posts 9
Joined 2007-12-28 13:08
18-year member
UID 107031
Gender Male
Status Offline
It's the homepage below
http://www.panjueshu.com/

I want to add a . before the / in
href="/ href=‘/ href=/
so they become href="./ href=‘./ href=./

:)
Happy New Year!
Floor 6 Posted 2008-01-01 00:57 ·  中国 北京 电信
初级用户
Credits 22
Posts 9
Joined 2007-12-28 13:08
18-year member
UID 107031
Gender Male
Status Offline
H:\新建文件夹>sed "s/\(href=\/\)/\(href=\).\//g" www.panjueshu.html >1panjueshu.html
^C Terminate batch job (Y/N)? Y
Running this seems to go into an infinite loop or something, I have to force it to stop

[ Last edited by samuel on 2008-1-1 at 01:13 AM ]
Floor 7 Posted 2008-01-01 01:39 ·  中国 浙江 杭州 电信
高级用户
★★
Credits 537
Posts 219
Joined 2007-08-04 09:43
19-year member
UID 94620
Gender Male
From 杭州--半山
Status Offline
sed "s/\(<a href=\?\)\(*\)/\1.\2/g" www.panjueshu.html >test.html


Happy New Year!




curl "http://www.panjueshu.com/"|sed "s/\(<a href=\?\)\(*\)/\1.\2/g" >test.html

[ Last edited by junchen2 on 2008-1-1 at 01:54 AM ]
Attachments
curl.rar (165.01 KiB, Downloads: 7)
Floor 8 Posted 2008-01-01 04:15 ·  中国 北京 电信
初级用户
Credits 22
Posts 9
Joined 2007-12-28 13:08
18-year member
UID 107031
Gender Male
Status Offline
Thanks, junchen2 :)
A strange thing:
I already downloaded the www.panjueshu.html page to my local machine
H:\新建文件夹>sed "s/\(<a href=\?\)\(*\)/\1.\2/g" www.panjueshu.html >1panjueshu.html
Even after running this for ten minutes, the replacement still didn't finish, so I had to force it to stop. After stopping it, I found that the replaced file was only 8.33k, while the original file size was 17.7k. I tried several times and it was always like this, the generated file was only 8.33k.


After forcibly ending the program, the end of the newly generated file was as follows:
------------------------
<p>…………………………………………………………………………………………………………
2121012080056205). If it is not paid by the deadline, the appeal will be treated as automatically withdrawn.
</p>

[ Last edited by samuel on 2008-1-1 at 10:00 AM ]
Floor 9 Posted 2008-01-01 11:30 ·  中国 浙江 杭州 电信
高级用户
★★
Credits 537
Posts 219
Joined 2007-08-04 09:43
19-year member
UID 94620
Gender Male
From 杭州--半山
Status Offline
It may be your SED that's the problem. It tests fine on my side. Try running the following again, I'll upload a SED.
curl "http://www.panjueshu.com/"|sed "s/\(<a href=\?\)\(*\)/\1.\2/g" >test.html



--------------------------------------------------------------------------------
<td><a href="./shaanxi/shaanxi.shtml">Shaanxi</a></td><td><a href="./shanxi/shanxi.shtml">Shanxi</a></td>
<td><a href="./xinjiang/xinjiang.shtml">Xinjiang</a></td><td><a href="./gansu/gansu.shtml">Gansu</a></td>
<td><a href="./ningxia/ningxia.shtml">Ningxia</a></td><td><a href="./qinghai/qinghai.shtml">Qinghai</a></td>
<td><a href="./xizang/xizang.shtml">Tibet</a></td><td><a href="./hongkong/hongkong.shtml">Hong Kong</a></td>
<td><a href="./macau/macau.shtml">Macau</a></td><td><a href="./taiwan/taiwan.shtml">Taiwan</a></td>


<li><a href=./lawyer/lawyer-3.shtml>Lawyer Directory 3</a></li>
<li><a href=./lawyer/lawyer-4.shtml>Lawyer Directory 4</a></li>



<li><a href='./hunan/gaoyuan/zhangdeyuan.shtml'>Bribery case of Zhang Deyuan, chairman of Hunan International Trust and Investment Corporation</a></li>
<li><a href='./guizhou/gaoyuan/yanjianhong.shtml'>Embezzlement case of Yan Jianhong, chairman of Guizhou International Trust and Investment Corporation</a></li>
<li><a href='./gaoyuan/court/gaosunxiang.shtml'>Bribery case of Gao Senxiang, former president of CITIC Industrial Bank Shenzhen Branch</a></li>

--------------------------------------
Above is part of the result

[ Last edited by junchen2 on 2008-1-1 at 11:32 AM ]
Floor 10 Posted 2008-01-01 11:34 ·  中国 浙江 杭州 电信
高级用户
★★
Credits 537
Posts 219
Joined 2007-08-04 09:43
19-year member
UID 94620
Gender Male
From 杭州--半山
Status Offline
I'll upload a sed, I couldn't upload it just now. Below is a SED collected by a senior member
http://www.vkill.net/tools.html

[ Last edited by junchen2 on 2008-1-1 at 11:38 AM ]
Floor 11 Posted 2008-01-01 19:45 ·  中国 北京 电信
初级用户
Credits 22
Posts 9
Joined 2007-12-28 13:08
18-year member
UID 107031
Gender Male
Status Offline
Thanks, junchen2 :)
Using the sed you gave me, there really is no problem running it
Below is the final code,
----------------------
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /a /b /s *.shtml') do (
echo %%i
sed -i "s/\(<p>*\)[\r\n]<\/p>/\1\n<\/p>/g" "%%i"
sed -i "s/\(<a href=['|\x22]\?\)\([^.>]*\)/\1.\2/g" "%%i"
time/t
)
endlocal
-----------------------
Forum Jump: