Home Fill object within an loop and output the whole array to a CSV
Reply: 2

Fill object within an loop and output the whole array to a CSV

Logan W
1#
Logan W Published in 2018-01-12 11:17:42Z

I currently have a script that will run through a folder and open each PDF inside and an input box will pop up asking me what I want to rename the file to. This part works fine but I'm trying to populate an array with information on each file such as date created, date modified, old file name and new file name.

The code I currently have is:

Add-Type -AssemblyName Microsoft.VisualBasic 

$folderpath = 'file path'
$items = Get-ChildItem -Recurse $folderpath *.pdf
$newFileName = "" 
$counterID = 0
$amountOfScans = $items.Length
$typeOfScanner = ""

$scanArray = @()
$scanObject = New-Object System.Object

foreach( $i in $items)  {
    Start-Process ((Resolve-Path ("$folderpath$i")).Path)
    Start-Sleep -Seconds 1
    $counterID++ 

    $orderID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the new file name for scan $counterID / $amountOfScans :", $i)

    if ($newFileName -eq "delete"){
        $reasonForDeletion = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a reason for marking $i for deletion :", $i)
    }

    if ($i.Length -eq 24){
        $typeOfScanner = "Xerox"
    }
    elseif ($ie.Length -eq 33) {
        $typeOfScanner = "Lexmark"
    }

    $DateCreated = Get-ItemProperty ((Resolve-Path ("$folderpath$i")).Path) | select -exp CreationTime
    $DateModified1 = Get-Date -Format dd/MM/yyyy        
    $DateModified2 = Get-Date -Format HH:mm:ss
    $DateModifiedTotal = ("$DateModified1 $DateModified2")

    $scanObject[$counterID] | Add-Member -type NoteProperty -name TempName -Value "$i" 
    $scanObject[$counterID] | Add-Member -type NoteProperty -name NewName -Value "$orderID"
    $scanObject[$counterID] | Add-Member -type NoteProperty -name TypeOfScanner -Value "$typeOfScanner"
    $scanObject[$counterID] | Add-Member -type NoteProperty -name DateCreated -Value "$DateCreated"
    $scanObject[$counterID] | Add-Member -type NoteProperty -name DateModified -Value "$DateModifiedTotal"
    $scanObject[$counterID] | Add-Member -type NoteProperty -name ReasonForDeletion -Value "$reasonForDeletion"

    $scanArray += $scanObject[$counterID]

    Stop-Process -Name "Acro*"
}

$scanArray | export-csv C:\Scans\renamed_stats.csv -notypeinformation

However after this script is ran "renamed_stats.csv" is completely blank with no error messages.

Any help would be appreciated :)

gms0ulman
2#
gms0ulman Reply to 2018-01-12 11:41:55Z

Assuming you don't have so many objects as to run out of memory, and that your value variables like $i contain the expected, you can get rid of $scanObject altogether and do this:

[array]$scanArray += [PSCustomObject] @{
    TempName          = "$i"
    NewName           = "$orderID"
    TypeOfScanner     = "$typeOfScanner"
    DateCreated       = "$DateCreated"
    DateModified      = "$DateModifiedTotal"
    ReasonForDeletion = "$reasonForDeletion"
}

You could also improve the date part by using quotes:

$DateModified = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
Justinas Marozas
3#
Justinas Marozas Reply to 2018-01-12 11:37:46Z

Try creating $scanObject inside loop this way:

$scanObject = New-Object PSObject -Property @{
    TempName = "$i"
    NewName = "$orderID"
    TypeOfScanner = "$typeOfScanner"
    DateCreated = "$DateCreated"
    DateModified = "$DateModifiedTotal"
    ReasonForDeletion = "$reasonForDeletion"
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306461 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO