Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Get information from excel, then use unc path to find file sizes, then export all information back to excel

$
0
0

I hope my question it clear enough, I will try to explain.

My company made a csv and xlsx file from sql.
Sadly the csv is not a correct csv file to work with so I cannot work with it.
I made a script to read the excel document and get the UNC path from it.
Also get the file size in a system.array.

But now I want to combine the 2 system.arrays so I have 1 excel (or csv) document with the file size of the unc path.

It looks like this: (import-excel is not made by me.but downloaded from: https://podlisk.wordpress.com/2011/11/20/import-excel-spreadsheet-into-powershell/ )

function Import-Excel
{   param (     [string]$FileName,     [string]$WorksheetName,     [bool]$DisplayProgress = $true   )  if ($FileName -eq "") {     throw "Please provide path to the Excel file"     Exit   }  if (-not (Test-Path $FileName)) {     throw "Path '$FileName' does not exist."     exit   }  $FileName = Resolve-Path $FileName   $excel = New-Object -com "Excel.Application"   $excel.Visible = $false   $workbook = $excel.workbooks.open($FileName)  if (-not $WorksheetName) {     Write-Warning "Defaulting to the first worksheet in workbook."     $sheet = $workbook.ActiveSheet   } else {     $sheet = $workbook.Sheets.Item($WorksheetName)   }   if (-not $sheet)   {     throw "Unable to open worksheet $WorksheetName"     exit   }   $sheetName = $sheet.Name   $columns = $sheet.UsedRange.Columns.Count   $lines = $sheet.UsedRange.Rows.Count   Write-Warning "Worksheet $sheetName contains $columns columns and $lines lines of data"   $fields = @()   for ($column = 1; $column -le $columns; $column ++) {     $fieldName = $sheet.Cells.Item.Invoke(1, $column).Value2     if ($fieldName -eq $null) {       $fieldName = "Column" + $column.ToString()     }     $fields += $fieldName   }   $line = 2   for ($line = 2; $line -le $lines; $line ++) {     $values = New-Object object[] $columns     for ($column = 1; $column -le $columns; $column++) {       $values[$column - 1] = $sheet.Cells.Item.Invoke($line, $column).Value2     }     $row = New-Object psobject     $fields | foreach-object -begin {$i = 0} -process {       $row | Add-Member -MemberType noteproperty -Name $fields[$i] -Value $values[$i]; $i++     }     $row     $percents = [math]::round((($line/$lines) * 100), 0)     if ($DisplayProgress) {       Write-Progress -Activity:"Importing from Excel file $FileName" -Status:"Imported $line of total $lines lines ($percents%)" -PercentComplete:$percents     }   }   $workbook.Close()   $excel.Quit()
}
[System.Collections.ArrayList]$reportLines = Import-Excel "C:\Test123.xlsx"

foreach ($bestand in $reportLines.bestandslocatie) {
$Filesize = "{0:N2}" -f ((Get-item $bestand).length/1Mb) + " Mb" | Out-string
$FileSizeArray = @($FileSize)
$object = New-Object -TypeName psobject
$object | Add-Member –MemberType NoteProperty –Name bestandslocatie –Value $FileSizeArray
$Bestandlocatie = $object.bestandslocatie
write-host $Bestandlocatie
}


Ventrex



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>