问题
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