如何修改Powershell代码,以便仅为文件而不是目录创建超链接? [英] How to modify Powershell code so that hyperlinks are only created for the files but not the directories?

查看:76
本文介绍了如何修改Powershell代码,以便仅为文件而不是目录创建超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码生成Index.htm页面。代码工作得很好,但我不希望在该Index.htm页面内的唯一内容是为目录提供超链接,因为它们毫无用处。 Index.htm应仅包含 C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ <!关于如何达到这样的结果的任何建议?

  $ 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文件< / th>'
-replace'< td>(。* (。*?)< / td>',
'< td>< t h>< t h> = $ 1 > $ 2'; / A> $ 3< / td>'
} | Set-Contentc:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'



  Get-ChildItem $ basedir -Force | ? {-not $ _。psiscontainer} | ...其余的代码... 

在PowerShell V3中

  Get-ChildItem $ basedir -Force -file | ...其余的代码... 


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" 

解决方案

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 ...

这篇关于如何修改Powershell代码,以便仅为文件而不是目录创建超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆