Powershell 中的剩余行数和百分比
Remaining rows and percentage in Powershell
我正在尝试创建一个 powershell 程序来读取 XML 文件,并将数据存储到 excel 文件中以更有效地分析它.所以我可以毫无问题地打开文档,阅读 XML 并毫无问题地写入 CSV。但是当用户启动他的程序时,我想用 percentage 向他展示剩余的行数。
这是我的代码:
$Packages = $xdoc.DataProfile.DataProfileOutput.Profiles.ColumnValueDistributionProfile
$PackageNumber = $Packages.Count
# --- For each couple, get the table name, the number of rows, the name of the field, and the number of different values existing in each fields --- #
$i = 0
while($i -ne $PackageNumber){
$state = $i/$PackageNumber * 100
$state = [math]::floor($state)
Write-Host $state "of 100 %"
# Here are commands to get infos about the content of the xml
$j = 0
while($j -lt $ItemNumber){
$Value = #value in the xml ..
$Count = #number of the row in the xml ..
# --- If the number of rows in the fields is different from 0, calculate the purcentage for each value and add it to the CSV --- #
if($table_rows -ne 0){
$Purcentage = $Count / $table_rows * 100
$csv_content = $table_name + ";" + $table_field + ";" + $table_rows + ";" + $Value + ";" + $Count + ";" + $Purcentage
ADD-content -path $csv_file -value $csv_content
}
$j++
}
$i++
}
这样,我可以计算总行数,以及缓冲区正在读取的行数。通过简单的 除法 我获得了百分比。
但结果是:
50 of 100 %
50 of 100 %
....
50 of 100 %
51 of 100 %
51 of 100 %
....
51 of 100 %
我怎样才能为每个百分比值获得 只有一行 而不是像下面这样的很多?
50 of 100 %
51 of 100 %
52 of 100 %
可能有更好的方法,但首先想到的是获取文件中除最后一行之外的所有行,然后附加百分比。在此示例中,我在文件末尾添加了额外的一行,以确保我没有删除重要信息。
'asdf' > C:\temp\test.txt
' ' >> C:\temp\test.txt
0..100 | % {
sleep 1
$file = Get-Content 'C:\temp\Test.txt'
$file = $file[0..$($file.Count-2)]
$file += "$_%"
$file > C:\temp\test.txt
Get-Content C:\temp\test.txt
}
想到了这样做的想法。在脚本的前面,添加行 $oldState = 0
,为我们提供基准百分比。
然后,在您的主脚本块中,在此行 $state = [math]::floor($state)
下方,添加:
if ($state -ne $oldState){
Write-Host $state "of 100 %"
$oldState = $state
}
最终结果将是一个比较,以查看当前状态是否与最后写入屏幕的 $state
相同。如果它 不是 ,那么我们写入百分比并为状态设置一个新值。最终结果将在脚本执行时仅显示一次每个百分比。
我正在尝试创建一个 powershell 程序来读取 XML 文件,并将数据存储到 excel 文件中以更有效地分析它.所以我可以毫无问题地打开文档,阅读 XML 并毫无问题地写入 CSV。但是当用户启动他的程序时,我想用 percentage 向他展示剩余的行数。
这是我的代码:
$Packages = $xdoc.DataProfile.DataProfileOutput.Profiles.ColumnValueDistributionProfile
$PackageNumber = $Packages.Count
# --- For each couple, get the table name, the number of rows, the name of the field, and the number of different values existing in each fields --- #
$i = 0
while($i -ne $PackageNumber){
$state = $i/$PackageNumber * 100
$state = [math]::floor($state)
Write-Host $state "of 100 %"
# Here are commands to get infos about the content of the xml
$j = 0
while($j -lt $ItemNumber){
$Value = #value in the xml ..
$Count = #number of the row in the xml ..
# --- If the number of rows in the fields is different from 0, calculate the purcentage for each value and add it to the CSV --- #
if($table_rows -ne 0){
$Purcentage = $Count / $table_rows * 100
$csv_content = $table_name + ";" + $table_field + ";" + $table_rows + ";" + $Value + ";" + $Count + ";" + $Purcentage
ADD-content -path $csv_file -value $csv_content
}
$j++
}
$i++
}
这样,我可以计算总行数,以及缓冲区正在读取的行数。通过简单的 除法 我获得了百分比。
但结果是:
50 of 100 %
50 of 100 %
....
50 of 100 %
51 of 100 %
51 of 100 %
....
51 of 100 %
我怎样才能为每个百分比值获得 只有一行 而不是像下面这样的很多?
50 of 100 %
51 of 100 %
52 of 100 %
可能有更好的方法,但首先想到的是获取文件中除最后一行之外的所有行,然后附加百分比。在此示例中,我在文件末尾添加了额外的一行,以确保我没有删除重要信息。
'asdf' > C:\temp\test.txt
' ' >> C:\temp\test.txt
0..100 | % {
sleep 1
$file = Get-Content 'C:\temp\Test.txt'
$file = $file[0..$($file.Count-2)]
$file += "$_%"
$file > C:\temp\test.txt
Get-Content C:\temp\test.txt
}
想到了这样做的想法。在脚本的前面,添加行 $oldState = 0
,为我们提供基准百分比。
然后,在您的主脚本块中,在此行 $state = [math]::floor($state)
下方,添加:
if ($state -ne $oldState){
Write-Host $state "of 100 %"
$oldState = $state
}
最终结果将是一个比较,以查看当前状态是否与最后写入屏幕的 $state
相同。如果它 不是 ,那么我们写入百分比并为状态设置一个新值。最终结果将在脚本执行时仅显示一次每个百分比。