有如下文件夾:

需要通過(guò)一個(gè)批處理文件自動(dòng)建立一個(gè)鏈接到文件夾中每一個(gè)文件的網(wǎng)頁(yè)文件,且有文件更新時(shí),只要運(yùn)行一次批處理文件,即可自動(dòng)更新網(wǎng)頁(yè)文件。 編寫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
代碼思路: 先寫網(wǎng)頁(yè)文件的頭文件部分,并建立index.html; 然后寫網(wǎng)頁(yè)文件的內(nèi)容部分: 內(nèi)容1:寫文件夾中的文件到網(wǎng)頁(yè)文件,通過(guò)單循環(huán)實(shí)現(xiàn),逐一取得文件名和擴(kuò)展名,并建立超鏈接添加到網(wǎng)頁(yè)文件; 內(nèi)容2:寫子文件夾中的子文件夾的文件到網(wǎng)頁(yè)文件(通過(guò)雙循環(huán)實(shí)現(xiàn)):先寫子文件夾名稱到網(wǎng)頁(yè)文件中做為標(biāo)題,然后寫文件夾內(nèi)的文件做為超鏈接添加到網(wǎng)頁(yè)文件。 最后寫網(wǎng)頁(yè)文件的尾部分;
自動(dòng)建立網(wǎng)頁(yè),打開后效果如下: 
每次有文件夾或文件增加或刪除時(shí),只需要運(yùn)行一次批處理文件,即可自更更新網(wǎng)頁(yè)文件。 -End-
|