Reputation: 202
I have a script in PowerShell that creates an HTML file with the list of files of specific types (e.g. ".txt") with some basic information about them. Now I wanted to add a feature that one could click the filename in the HTML page created by the script in order to open it. Could anyone at least give a hint on how to do it? This is the current output I receive (I blurred the text though):
Here is the script I'm using (from alroc's answer to this question: Write multiple objects obtained via get-ChildItem into HTML):
$types=@("*.txt", "*.pdf")
$myfiles = @();
foreach($type in $types){
$myFiles += Get-ChildItem C:\POWERSHELL -Filter $type -Recurse
}
$myfiles | ConvertTo-Html -property fullname,lastwritetime,length | out-file c:\status23.html;
Upvotes: 0
Views: 3919
Reputation: 12321
In order to make a clickable link to the file, you need to use this kind of HTML tag:
<a href='file:///filename'>Display Text</a>`
However, if you want to change the HTML, you can't use ConvertTo-Html as in the script you're referencing, because that cmdlet will escape any input you provide it, so you need to create the HTML yourself. For a simple table, it's not that hard, and it lets you have more control over what the output will look like.
Try this (I've compacted your code in addition to adding the HTML output):
$output = "<html>`n<table>`n<tr><th align='left'>Full Name</th><th align='left'>Last Write Time</th><th align='right'>Size (bytes)</th></tr>`n"
Get-ChildItem '*.txt','*.ps1' -Recurse | %{
$output += "<tr><td><a href='file:///$($_.FullName)'>$($_.FullName)</a></td><td>$($_.LastWriteTime)</td><td align='right'>$($_.Length)</td></tr>`n"
}
$output += "</table>`n</html>"
$output | Out-File status23.html
A few notes:
`n
s aren't strictly necessary, they just make the resultant HTML file more readable; the page would be rendered the same if you left them out.Upvotes: 3
Reputation: 4700
To start, you need to wrap them in an html href tag with a path to the file.
If they have access to that txt file, and they are using a browser to view the html file, the text file will open within the browser when clicked.
Example:
$file="C:\test\to.txt"
"<HTML><a href=`"$file`">$file</a></HTML>" | Out-file "C:\test\test.html"
Upvotes: 0