gpt4 book ai didi

arrays - 在循环中填充对象并将整个数组输出到CSV

转载 作者:行者123 更新时间:2023-12-02 23:09:15 24 4
gpt4 key购买 nike

我目前有一个脚本,该脚本将通过文件夹运行并打开其中的每个PDF,然后将弹出一个输入框,询问我要将文件重命名为什么。这部分工作正常,但是我试图用每个文件的信息填充数组,例如创建日期,修改日期,旧文件名和新文件名。

我目前拥有的代码是:

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

但是,运行此脚本后,“renamed_stats.csv”完全空白,没有错误消息。

任何帮助,将不胜感激 :)

最佳答案

假设您没有太多的对象来耗尽内存,并且像$i这样的值变量包含了预期值,则可以完全摆脱$scanObject并执行以下操作:

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

您还可以通过使用引号来改善日期部分:
$DateModified = Get-Date -Format "dd/MM/yyyy HH:mm:ss"

关于arrays - 在循环中填充对象并将整个数组输出到CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48225180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com