html - 如何修改Powershell代码,以便仅为文件而非目录创建超链接?

标签 html powershell hyperlink

我正在使用下面的代码来生成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/

相关文章:

php - 一种在 PHP 中编写大型 elseif 语句的简单方法

powershell - 如果 Powershell 中存在另一个文件,则重命名文件

powershell - 如何在运行时在 powershell 脚本中显示 xcopy 的输出

javascript - 如何让网页上的所有链接都调用javascript函数?

javascript - 水平和垂直对齐伪元素的内容

html - ionic 滑动以增加问题

javascript - 播放前少做一个html5视频缓冲

xml - 使用 Xpath 和 GetElementbyID 访问 XML 数据

c# - 通过超链接打开新的gridview

html - 使用 sed 将 URL 转换为 HTML 链接?