【发布时间】:2017-06-15 21:44:35
【问题描述】:
$locations = Get-ChildItem $readLoc -recurse | ? {!$_.psiscontainer} | select-object name | %{$e = $_.name; get-content $e}
$array = @()
for($i = 0; $i -lt $locations.length; $i++){
#if($locations.name[$i].length -eq "9"){
$paths = Resolve-Path $locations.fullname[$i]
$paths.path
get-content $locations.name[$i]
#$array += $paths.path
#}
}
我需要遍历文件系统中的每个文件并打开每个文件。我正在检查文件中的字符串是否与正则表达式匹配,然后将该文件的完整路径输出到数组中。
但是,$locations 不接受获取内容。
获取内容:找不到路径
'C:\Users\xxxxxx\Documents\files\powershell\OWASP_ApplicationThreatModeling.docx'
because it does not exist.
At line:1 char:89
+ ... .psiscontainer} | select-object name |%{$e = $_.name; get-content $e}
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\p61782...atModeling.docx:String) [Get-Content], ItemNotFoundEx
ception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand.
【问题讨论】:
-
删除
|Select-Object Name | %{$e = $_.name; get-content $e},然后在下面将其更改为get-content $locations[$i].fullname。或者更好的是,将您的Get-ChildItem传递给Select-String,因为它默认使用正则表达式进行匹配。