How to modify Powershell code so that hyperlinks a

2019-08-08 15:49发布

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" 

2条回答
倾城 Initia
2楼-- · 2019-08-08 15:55

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 | ...
查看更多
Root(大扎)
3楼-- · 2019-08-08 16:06

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 ...
查看更多
登录 后发表回答