AggroCrag
AggroCrag

Reputation: 317

PowerShell Error: Import-CSV Cannot Open File

My PowerShell code is tripping some strange error codes, which I have thoroughly searched for to no avail.

I am trying to create a script that calculates some basic statistics for a number of CSV files, which seems to work until I try to create and populate new columns.

The error code I'm getting is:

Import-Csv: Cannot open file "C:\zMFM\Microsoft.Powershell.Commands.GenericMeasureInfo". At 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

That's followed by an error using a null expression, but I assume fixing this problem with Import-Csv will in turn fix that error. Here's my code, any help would be great, thanks!

$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++
}

Upvotes: 1

Views: 6350

Answers (1)

Benjamin Hubbard
Benjamin Hubbard

Reputation: 2917

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

Based on your code, it looks like you're trying to import a CSV with a filename of type [Microsoft.PowerShell.Commands.GenericMeasureInfo]. Filenames are strings. Also, why are you importing $filename twice? Would recommend importing it once, then operating off of the variable you saved it to.

Would also recommend changing your while loop to 1..211 | ForEach-Object. That way you won't have to worry about whether $i is less than or equal to your static number. Not a huge deal, but would make the code a little more readable.

TL;DR

Import-CSV $Stats is the problem. $Stats is not a valid filename, so you can't open/import it.

Upvotes: 3

Related Questions