2

I've created this script to find some files.

$wpath = Get-ChildItem F:\@software\ -recurse
$outp = $wpath | Where-Object {$_.extension -eq ".wav" -and $_.name -like "*hou*"}
$outp = $outp | Select-Object -Property Fullname
$outp | ConvertTo-Html | Out-File f:\@software\htmlout99.html
ii f:\@software\htmlout99.html

This gives the results I'm after, in the correct format eg.

F:\@software\TFTP-Root\14Hours.wav  
F:\@software\TFTP-Root\ddHours.wav

How can I make the results in HTML output clickable links, ie a clickable link to the file?

3 Answers 3

2

You need to define the links yourself using ex. calculated properties. ConvertTo-HTML will htmlencode some of the characters (like < and >), so we need to decode it before saving. Ex:

#Load System.Web to get HttpUtility
Add-Type -AssemblyName System.Web

$html = Get-ChildItem -Path F:\@software\ -Recurse |
Where-Object {$_.extension -eq ".wav" -and $_.name -like "*hou*"} |
Select-Object -Property @{n="Links";e={ "<a href='$(([uri]$_.FullName).AbsoluteUri)'>$($_.FullName)</a>" }} |
ConvertTo-Html -Property "Links"

#Decode special characters like < > etc.
[System.Web.HttpUtility]::HtmlDecode($html) | Set-Content "f:\@software\htmlout99.html"
Sign up to request clarification or add additional context in comments.

1 Comment

This is a much better way of doing the same thing I was.
1

Well, the brute force method would be to use this:

$wpath = Get-ChildItem F:\@software\ -recurse
$outp = $wpath | Where-Object {$_.extension -eq ".wav" -and $_.name -like "*hou*"}
$outp = $outp | Select-Object -Property @{n='FullName';e={'<a href="file://{0}">{0}</a>' -f $_.FullName}}
$outp | ConvertTo-Html | ForEach-Object { $_.Replace('&quot;','"').Replace('&lt;','<').Replace('&gt;','>') } | Out-File f:\@software\htmlout99.html
ii f:\@software\htmlout99.html

It uses a calculated property to translate the FullName into a link. Then you convert that to HTML. The problem is that ConvertTo-Html escapes control characters because it assumes the contents are not HTML. I'm sure that if your file names actually have control characters in them that you're bound to have some problems.

The real solution would be to ditch ConvertTo-Html altogether and just roll your own.

1 Comment

Well, brute force or not, that works a treat. Thanks for the very quick response.
1

Yet another way kind of like @Bacon Bits was mentioning. I use this method often for my sched task notifications.

$htmltop = "<html><head></head><body><ul>"
$htmlbottom = "</ul></body></html>"

$wpath = Get-ChildItem F:\@software\ -recurse
$outp = $wpath | Where-Object {$_.extension -eq ".wav" -and $_.name -like "*hou*"}

$outp = $outp | % { $("<li><a href='file://" + $($_.FullName) + "'>" + $($_.Name) + "</a></li>")}

$htmltop | Out-File F:\@software\htmlout99.html -Append
$outp | Out-File F:\@software\htmlout99.html -Append
$htmlbottom | Out-File F:\@software\htmlout99.html -Append


ii F:\@software\htmlout99.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.