我目前有一个脚本,它将运行一个文件夹并打开其中的每个 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/