PowerShell错误:导入CSV无法打开文件

时间:2015-12-07 16:46:16

标签: csv powershell

我的PowerShell代码正在绊倒一些奇怪的错误代码,我已经彻底搜索过但无济于事。

我正在尝试创建一个脚本来计算一些CSV文件的一些基本统计信息,这似乎有效,直到我尝试创建和填充新列。

我得到的错误代码是:

  

Import-Csv:无法打开文件“C:\ zMFM \ Microsoft.Powershell.Commands.GenericMeasureInfo”。   在C:\ zMFM \ StatsDebug.ps1:46 Char:21

     

+ $ STATS2 = Import-CSV<<< $ STATS

     

+ CategoryInfo:OpenError(:) [Import-Csv],FileNotFoundException

     

+ FullyQualifiedErrorId:FileOpenFailure.Microsoft.Powershell.Commands.ImportCsvCommand at C:\ zMFM \ statsdebug.ps1:55 char:20

之后是使用空表达式的错误,但我认为使用Import-Csv修复此问题将反过来修复该错误。这是我的代码,任何帮助都会很棒,谢谢!

$i = 1
#$colSTDDEV = New-Object System.Data.DataColumn StdDev,([double])
$colVZA = New-Object System.Data.DataColumn VZA,([double])
#$colVAZ = New-Object System.Data.DataColumn VAZ,([double])

While ($i -le 211) {

#Set the variable to the filename with the iteration number
$filename = "c:\zMFM\z550Output\20dSummer\fixed20dSum550Output$i.csv"


#Check to see if that a file with $filename exists. If not, skip to the next iteration of $i. If so, run the code to collect the 

statistics for each variable and output them each to a different file
If (Test-Path $filename) {


#Calculate the Standard Deviation
#First get the average of the values in the column
$STDEVInputFile = Import-CSV $filename

#Find the average and count for column 'td'
$STDEVAVG = $STDEVInputFile | Measure-Object td -Average | Select Count, Average
$DevMath = 0

# Sum the squares of the differences between the mean and each value in the array
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($Y.Average - $STDEVAVG.Average), 2)

#Divide by the number of samples minus one
$STDEV = [Math]::sqrt($DevMath / ($STDEVAVG.Count-1))

}

#Calculate the basic statistics for column 'td' with the MEASURE-OBJECT cmdlet
$STATS = Import-CSV $Filename |
Measure-Object td -ave -max -min

#Append the standard deviation variable to the statistics array and add the value

$STATS2 = Import-CSV $Stats
$VZA = $filename.VZA
#$VAZ = $filename.VAZ #COMMENTED FOR DEBUGING


#$STATS2.Columns.Add($colSTDDEV) #COMMENTED FOR DEBUGING
#$STATS2[0].StdDev = $STDEV #COMMENTED FOR DEBUGING


$STATS2.Columns.Add($colVZA)
$STATS2[0].VZA = $VZA


#$STATS2.Columns.Add($colVAZ) #COMMENTED FOR DEBUGING
#$STATS2[0].VZA = $VAZ #COMMENTED FOR DEBUGGING

#Export the $STATS file containing everything you need in the correct folder


Export-CSV -notype "c:\zMFM\z550Output\20dSummer\20dSum550Statistics.csv"

}
$i++
}

1 个答案:

答案 0 :(得分:3)

$STDEVInputFile = Import-CSV $filename
...
$STATS = Import-CSV $Filename |
  Measure-Object td -ave -max -min
# $STATS here will be type [GenericMeasureInfo], so you cannot use this as a filename.

$STATS2 = Import-CSV $Stats
# Are you trying to import $filename a third time here? You can't use $Stats because it's a [GenericMeasureInfo] object, not a [string].

根据您的代码,您似乎尝试导入文件名为[Microsoft.PowerShell.Commands.GenericMeasureInfo]的CSV。文件名是字符串。另外,为什么要导入$filename两次?建议导入一次,然后操作保存它的变量。

还建议您将while循环更改为1..211 | ForEach-Object。这样您就不必担心$i是否小于或等于您的静态数字。这不是一个大问题,但会使代码更具可读性。

TL; DR

Import-CSV $Stats是问题所在。 $Stats不是有效的文件名,因此您无法打开/导入它。