- <# :
- echo off&cls
- rem 将多个html文件生成列表导航链接,取title标题作为文字导航标签
- mode con lines=5000
- echo 欢迎使用html文件索引生成器 提示:请把该工具放入html文件夹内运行
- echo 正在生成索引,请稍等......
- title %#% +%$%%$%/%_% %z%
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312')))) -Args '%~f0'"
- echo;%#% +%$%%$%/%_% %z%
- pause>nul
- exit
- #>
- $ext=@(".htm",".html");
- $outfile=".\首页.html";
- $self=get-item -liter $args[0];
- $path=$self.Directory.FullName;
- $outfile=$outfile -replace '^\.',$path;
-
-
- function checkencoding($t){
- $tmpenc=New-Object System.Text.UTF8Encoding $False;
- $m=[regex]::match($t, '(?i)<meta .*?charset=["'']?(gbk|gb2312)');
- if($m.Success){$tmpenc=[Text.Encoding]::GetEncoding('GB2312');};
- return $tmpenc;
- };
-
- $outtext=@'
- <html>
- <head><meta charset="UTF-8"></head>
- <body>
- x
- </body>
- </html>
- '@;
-
- [System.Collections.ArrayList]$s=@();
- $files=@(dir -liter $path|?{($ext -contains $_.Extension) -and ($_ -is [System.IO.FileInfo])});
- for($i=0;$i -lt $files.length;$i++){
- $enc=New-Object System.Text.UTF8Encoding $False;
- $tmptext=[IO.File]::ReadAllText($files[$i].FullName, $enc);
- $enc=checkencoding $tmptext;
- $tmptext=[IO.File]::ReadAllText($files[$i].FullName, $enc);
- $m=[regex]::match($tmptext,'(?i)<title(?: [^>]+)?>([\s\S]+)</title>');
- if($m.success){
- $line='<a href="'+$files[$i].Name+'">'+$m.groups[1].value+'</a><br>';
- [void]$s.add($line);
- };
- };
- $outtext=$outtext.replace('x', ($s -join "`r`n"));
- [IO.File]::WriteAllText($outfile, $outtext, [Text.Encoding]::GetEncoding('UTF-8'));
- echo 生成索引完成,按Enter退出......
- pause
复制代码
|