我正在使用下面的代码来生成Index.htm页面。该代码可以很好地工作,但是在Index.htm页面中我唯一不需要的是拥有目录的超链接,因为它们是无用的。 Index.htm应该仅包含 C:\ inetpub \ wwwroot 中的.htm文件的超链接。关于如何获得这种结果的任何建议?
$basedir = 'C:\inetpub\wwwroot'
$exp = [regex]::Escape($basedir)
$server = 'http://172.16.x.x'
Get-ChildItem $basedir -Force |
select Name, LastWriteTime,
@{n="URL";e={$_.FullName -replace $exp, $server -replace '\\', '/'}} |
ConvertTo-Html -Fragment URL, Name, LastWriteTime `
-PreContent '<html><head><title>Test</title></head><body>' `
-PostContent '</body></html>' |
% { $_ -replace '<th>.*</th>','<th>Files</th>' `
-replace '<td>(.*?)</td><td>(.*?)</td><td>(.*?)</td>',
'<td><a href="$1">$2</a> $3</td>'
} | Set-Content "c:\inetpub\wwwroot\index.htm"
最佳答案
您可以通过以下方式过滤掉文件夹:
Get-ChildItem $basedir -Force | ? { -not $_.psiscontainer } | ... rest of your code ...
在Powershell V3中
Get-ChildItem $basedir -Force -file | ... rest of your code ...
关于html - 如何修改Powershell代码,以便仅为文件而非目录创建超链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17589319/