How to modify Powershell code so that hyperlinks are only created for the files but not the directories?

↘锁芯ラ 提交于 2019-12-11 07:35:48

问题


I'm using the code below to generate Index.htm page. The code works great, but the only thing that I don't want inside that Index.htm page is to have the hyperlinks for directories, as they are useless. Index.htm should contain only hyperlinks to the .htm files inside the C:\inetpub\wwwroot. Any suggestions on how to achieve such result?

$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" 

回答1:


You can filter out folder in this way:

Get-ChildItem $basedir -Force | ? { -not $_.psiscontainer } | ... rest of your code ...

in powershell V3

Get-ChildItem $basedir -Force -file | ... rest of your code ...



回答2:


What C.B. said. Another option, since you want only .htm files, would be filtering those files in Get-ChildItem:

Get-ChildItem $basedir -Filter "*.htm" -Force | ...


来源:https://stackoverflow.com/questions/17589319/how-to-modify-powershell-code-so-that-hyperlinks-are-only-created-for-the-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!