![]() |
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-07 03:27 |
48,040 topics / 350,125 posts / today 1 new / 48,252 members |
| DOS批处理 & 脚本技术(批处理室) » [fixed] How to replace links in HTML files |
| Printable Version 824 / 10 |
| Floor1 samuel | Posted 2007-12-31 12:09 |
| 初级用户 Posts 9 Credits 22 | |
|
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 ] |
|
| Floor2 junchen2 | Posted 2007-12-31 12:45 |
| 高级用户 Posts 219 Credits 537 From 杭州--半山 | |
| Floor3 samuel | Posted 2007-12-31 17:32 |
| 初级用户 Posts 9 Credits 22 | |
| Floor4 junchen2 | Posted 2008-01-01 00:05 |
| 高级用户 Posts 219 Credits 537 From 杭州--半山 | |
|
Post the test file so we can take a look
|
|
| Floor5 samuel | Posted 2008-01-01 00:46 |
| 初级用户 Posts 9 Credits 22 | |
|
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! |
|
| Floor6 samuel | Posted 2008-01-01 00:57 |
| 初级用户 Posts 9 Credits 22 | |
|
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 ] |
|
| Floor7 junchen2 | Posted 2008-01-01 01:39 |
| 高级用户 Posts 219 Credits 537 From 杭州--半山 | |
|
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) |
|
| Floor8 samuel | Posted 2008-01-01 04:15 |
| 初级用户 Posts 9 Credits 22 | |
|
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 ] |
|
| Floor9 junchen2 | Posted 2008-01-01 11:30 |
| 高级用户 Posts 219 Credits 537 From 杭州--半山 | |
|
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 ] |
|
| Floor10 junchen2 | Posted 2008-01-01 11:34 |
| 高级用户 Posts 219 Credits 537 From 杭州--半山 | |
|
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 ] |
|
| Floor11 samuel | Posted 2008-01-01 19:45 |
| 初级用户 Posts 9 Credits 22 | |
|
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 ----------------------- |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |