【发布时间】:2021-10-06 22:05:32
【问题描述】:
我正在尝试制作一个可以放入目录中的 powershell 脚本,然后返回所有唯一的扩展名及其各自的计数。根据找到的扩展名,我可以输出该信息。每当我以当前状态运行脚本时,都会创建并导入 CSV,但我似乎无法将变量从 CSV 传回。如果我创建(Extension = "something"),即使它不应该输出,它也会始终输出为真,例如下面代码中显示的 .jsp 的情况,它不在目录中并且不应该返回任何内容。我也不确定如何测量每个扩展的计数,因为Measure-Object 似乎没有正确触发。
我的目标如下:
-
获取所有独特的扩展及其数量,然后放入 CSV
-
从 CSV 导入所有唯一的扩展名并输出一条消息,如果 找到特定的扩展名及其次数 出现
$path = "C:\Users\spawn\Desktop\Powershell"
gci -rec $path -exclude *.ps1, *.csv, *.bat | Select-Object Extension -u | Export-Csv .\Export.csv
#Below is an example of how I think it should work
#gci -rec $path -exclude *.ps1, *.csv, *.bat | Measure-Object Extension | Export-Csv .\Export.csv
$escv = Import-Csv "C:\Users\spawn\Desktop\Powershell\Export.csv"
ForEach ($data in $escv)
{
$Count = $data.count
$Extension = $data.extension
if ($Extension -eq ".cs")
{
echo "A .cs file was found."
echo "There were $Count files found."
}
if ($Extension -eq ".sln")
{
echo "A .sln file was found."
echo "There were $Count files found."
}
if ($Extension -eq ".java")
{
echo "A .java file was found."
echo "There were $Count files found."
}
if ($Extension -eq ".jsp")
{
echo "A .jsp file was found."
echo "There were $Count files found."
}
return
}
【问题讨论】:
标签: powershell csv