有如下文件夹:

需要通过一个批处理文件自动建立一个链接到文件夹中每一个文件的网页文件,且有文件更新时,只要运行一次批处理文件,即可自动更新网页文件。 编写bat文件代码: echo ^<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >index.html echo "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"^> >>index.html echo ^<html xmlns="http://www./1999/xhtml"^> >>index.html echo ^<base target="_blank" /^> >>index.html echo ^<head^> >>index.html echo ^<title^>contents^</title^> >>index.html echo ^<link href="backup/index.css" rel="stylesheet" type="text/css" /^> >>index.html echo ^<style type=text/css^> >>index.html echo ^</style^>^</head^> >>index.html echo ^<body^>^<div^> >>index.html for /f "tokens=1,2 usebackq delims=." %%a in (`dir /o:n /b`) do ( if not "%%a.%%b"=="%%a." ( if not "%%a.%%b"=="index.html" ( if not "%%b"=="bat" ( echo ^<li^>^<a href="%%a.%%b"^>%%a.%%b^</a^>^</li^> >>index.html ) ) ) ) for /f "tokens=1,2 usebackq delims=." %%a in (`dir /o:n /b`) do ( if "%%a.%%b"=="%%a." ( if not "%%a"=="backup" ( echo ^<h4^>%%a^</h4^> >>index.html for /f "tokens=1,2 usebackq delims=." %%f in (`dir %%a /o:n /b`) do ( if "%%g"=="html" ( if not "%%f.%%g"=="%%f." ( echo ^<li^>^<a href="%%a/%%f.%%g"^>%%f^</a^>^</li^> >>index.html )) ) )) ) echo ^</div^> >>index.html echo ^</body^> >>index.html echo ^</html^> >>index.html
代码思路: 先写网页文件的头文件部分,并建立index.html; 然后写网页文件的内容部分: 内容1:写文件夹中的文件到网页文件,通过单循环实现,逐一取得文件名和扩展名,并建立超链接添加到网页文件; 内容2:写子文件夹中的子文件夹的文件到网页文件(通过双循环实现):先写子文件夹名称到网页文件中做为标题,然后写文件夹内的文件做为超链接添加到网页文件。 最后写网页文件的尾部分;
自动建立网页,打开后效果如下: 
每次有文件夹或文件增加或删除时,只需要运行一次批处理文件,即可自更更新网页文件。 -End-
|