How to generate hyperlinks to .htm files in a directory in Powershell?

大兔子大兔子 提交于 2019-12-31 05:57:12

问题


I would like to scan a directory for .htm files and then generate hyperlinks to those .htm files and output the result to a new HTML document. What is the best way to achieve this?

So far I have this:

Get-ChildItem "C:\test1\*.htm" -Recurse -Force |
  ConvertTo-Html -Fragment FullName, Name `
    -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:\$env:COMPUTERNAME-$(Get-Date -f dd-MM-yyyy-hh-mm-ss).htm"

回答1:


You could try something like this:

Get-ChildItem "C:\*.htm" -Recurse -Force |
  ConvertTo-Html -Fragment FullName, 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:\$env:COMPUTERNAME-$(Get-Date -f dd-MM-yyyy).htm"



回答2:


This could be right up your street, you would need to add a filter to the Get-ChildItem line though: Aaron Lerch Blog - Create Directory Table Of Contents With Powershell



来源:https://stackoverflow.com/questions/17471712/how-to-generate-hyperlinks-to-htm-files-in-a-directory-in-powershell

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