如何让Powershell读取文件内容?

时间:2013-01-11 17:24:07

标签: powershell file-read

我正在调整这个脚本来计算文件中单个单词的实例。

$txtPath = "c:\users\xxxxxx\desktop\tx"
$srcfiles = Get-ChildItem $txtPath -filter "*.txt*"
#
function wordCount ($docs) {
    Write-Host "Processing Word Count: " $docs
$s = "I saw the cat. The cat was black."
",",".","!","?",">","<","&","*","=","`n","_" |% {$s = $s.replace($_,' ')}     # Remove Chars
$w = $s.Split() |? {$_.Length -gt 0 }                                     # Array of words, spaces removed
$w | select -Unique                                                       # Unique words
$w | group                                                                # Tally
$W | group | sort name | ft name,count -AutoSize              # Sort and format
#>
}
#
ForEach ($doc in $srcfiles) { 
    Write-Host "Calling: " $doc.FullName         
    wordCount -docs  $doc.FullName    
}

目前,表示要计数的字符串的输入变量$s是硬编码的。我想将每个文档放在$srcFiles路径中并对每个文档进行计数。但是,$s = $docs计算标题中的单词,而不是文档内容。我该怎么做?

此外,$W | group | sort name | ft name,count -AutoSize会返回以下错误:

out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not
 in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with th
e default formatting.
    + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperationException
    + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

我应该在哪里寻找格式问题?我无法在TechNet上发现任何类型的默认格式信息;而这段代码最初来自的site没有提及它们是如何工作的,也没有提到它们覆盖的默认格式。我怀疑我可能需要以不同方式管道,但我需要更好地理解确切的错误,以便我知道从哪里开始狩猎。

1 个答案:

答案 0 :(得分:2)

您需要使用Get-Content来读取文件的内容。您需要组合Get-Content返回的行以正确计算字符数。见post