Powershell 尝试确定每列中的最大字符数
我写了一个脚本,试图确定每列的最大字符数。这是我写的:Powershell 尝试确定每列中的最大字符数,powershell,Powershell,我写了一个脚本,试图确定每列的最大字符数。这是我写的: $path = 'folder path' $file = Get-ChildItem $path\* $FileContent = foreach ($files in $file) { $FileHeader = @( (Get-Content $files -First 1).Split($delimiter) ) $importcsv = @( Import-Csv $files -Delimiter "$delim
$path = 'folder path'
$file = Get-ChildItem $path\*
$FileContent = foreach ($files in $file) {
$FileHeader = @( (Get-Content $files -First 1).Split($delimiter) )
$importcsv = @( Import-Csv $files -Delimiter "$delimiter" )
for ($i=0; $i -lt $FileHeader.Length; $i++) {
#select each column
$Column = @( $importcsv | select $FileHeader[$i] )
#find the max no. of character
$MaxChar = @(($Column[$i] |
Select -ExpandProperty $FileHeader[$i] |
Measure-Object -Maximum -Property Length).Maximum)
$output = New-Object PSObject
$output | Add-Member NoteProperty FullName ($files.FullName)
$output | Add-Member NoteProperty FileName ($files.Name)
$output | Add-Member NoteProperty Time (Get-Date -Format s)
$output | Add-Member NoteProperty FileHeader ($($FileHeader[$i]))
$output | Add-Member NoteProperty MaxCharacter ($($MaxChar[$i]))
Write-Output $output
}
}
上面的脚本只是它的一部分,因此已经定义了$delimiter
。最后,我将把结果导出为CSV
脚本运行时没有任何错误,但当我打开文件时,它只给我第一列/标题最大字符数,其余列/标题丢失
完美的结果是显示每列/标题的最大字符数
我的循环有问题吗
我的老板正在尝试创建一个自动过程,从原始数据中查找所有信息并使用这些信息上传到数据库,因此缺少的部分脚本是关于确定原始文件的分隔符,$CleanHeader
是$FileHeader
的干净版本(删除所有特殊字符,将大写字母转换为小写字母),这些干净的标题将用于数据库表中的标题。他还想知道每列中的最大字符数,以便info可以使用它们创建数据库表中列的大小(他知道这部分可以用sql完成),但他问我是否可以在PowerShell中完成。这应该可以:
$ht = @{}
# import a CSV and iterate over its rows
Import-Csv $f.FullName -Delimiter "$delimiter" | ForEach-Object {
# iterate over the columns of each row
$_.PSObject.Properties | ForEach-Object {
# update the hashtable if the length of the current column value is greater
# than the value in the hashtable
if ($_.Value.Length -gt $ht[$_.Name]) {
$ht[$_.Name] = $_.Value.Length
}
}
}
# create an object for each key in the hashtable
$date = Get-Date -Format s
$ht.Keys | ForEach-Object {
New-Object -Type PSObject -Property @{
FullName = $f.FullName
Name = $f.Name
Time = $date
FileHeader = $_
MaxCharacter = $ht[$_]
}
}
这应该起作用:
$ht = @{}
# import a CSV and iterate over its rows
Import-Csv $f.FullName -Delimiter "$delimiter" | ForEach-Object {
# iterate over the columns of each row
$_.PSObject.Properties | ForEach-Object {
# update the hashtable if the length of the current column value is greater
# than the value in the hashtable
if ($_.Value.Length -gt $ht[$_.Name]) {
$ht[$_.Name] = $_.Value.Length
}
}
}
# create an object for each key in the hashtable
$date = Get-Date -Format s
$ht.Keys | ForEach-Object {
New-Object -Type PSObject -Property @{
FullName = $f.FullName
Name = $f.Name
Time = $date
FileHeader = $_
MaxCharacter = $ht[$_]
}
}
FileHeader[$i]
正在返回带引号的列名:“ColumnName”
而不是ColumnName
若要修复,只需在拉动收割台的直线上添加修剪:
$FileHeader=@((获取内容$files-第一个1).拆分($delimiter).修剪(““)
FileHeader[$i]
正在返回带引号的列名:“ColumnName”
而不是ColumnName
若要修复,只需在拉动收割台的直线上添加修剪:
$FileHeader=@((获取内容$files-First 1)。拆分($delimiter)。修剪(““)
为什么需要知道最大字符数?列的含义是什么?您是否在问如何查找具有长名称的文件?在PowerShell中几乎没有无法完成的操作,因此回答了一个问题“XXX可以在PowerShell中完成吗?”实际上总是“是”。真正的问题是:实现XXX需要付出多少努力?为什么需要知道最大字符数?列的含义是什么?您是在问如何查找具有长名称的文件吗?在PowerShell中几乎没有什么做不到的事,所以回答了一个问题“XXX可以在PowerShell中完成吗?”实际上总是“是”。真正的问题是:实现XXX需要付出多少努力?我已经测试了它,它可以工作了,我对powershell很陌生,所以我有一些初级问题,是$(导入csv…),对吗?那么$是什么意思?我知道$ht是哈希表,那么$ht[$\名称]是什么是什么意思?谢谢你$\u是当前的对象变量(将当前对象保存在管道中)。请看。我已经测试了它,它可以工作了,我对powershell很陌生,所以我有一些初级问题,$\u=(导入csv…)对吗?那么$\u.Name的意思是什么?我知道$ht是哈希表,那么$ht[$\u.Name]是什么平均值?thank you$\u
是当前对象变量(将当前对象保存在管道中)。请参阅。