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

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

user2401
1#
user2401 Published in April 26, 2018, 5:18 pm

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 :)

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO