【发布时间】:2018-05-10 08:46:17
【问题描述】:
我对脚本有点陌生,请原谅我在这里有任何明显的答案。
基本上,我每个月都会更新一个文件夹中存储的大量音乐文件。我创建了一个较小的文件夹用于测试目的:
我有一个应该在文件夹中的所有文件的 CSV 列表:
我正在尝试创建一个 Powershell 脚本:
- 获取 CSV 列“音频文件路径”名称并将其与文件夹“音乐”中的文件名进行比较
- 检查 CSV 列表或音乐文件夹中是否有任何丢失的文件
- 输出丢失文件的名称
到目前为止我的测试脚本(已编辑):
$folder = 'C:/Users/Me/Documents'
$myFolder = Get-ChildItem '/Users/Me/Documents/Music'
$myCSV = Import-Csv -Path 'Documents/AudioMissing.csv'| % {$_.'Audio File Path' -replace "\\", ""}
$compare = Compare-Object -ReferenceObject $myCSV -DifferenceObject $myFolder
foreach($y in $compare){
if($y.SideIndicator -eq "=>"){
write-output "$($y.InputObject) Is present in the Music folder but not in the CSV."
}
if($y.SideIndicator -eq "<="){
write-output "$($y.InputObject) Is present in the CSV but not in Music folder."
}
}
除了我运行脚本时,它会返回列表和文件夹中的所有文件,并且还说有些文件丢失了,即使它们是相同的文件名。
输出如下所示(已编辑):
9913SA_FIle1.wav Is present in the Music folder but not in the CSV.
9914SA_File2.wav Is present in the Music folder but not in the CSV.
9915SA_File3.wav Is present in the Music folder but not in the CSV.
9916SA_File4.wav Is present in the Music folder but not in the CSV.
9917SA_File5.wav Is present in the Music folder but not in the CSV.
9913SA_FIle1.wav Is present in the CSV but not in Music folder.
9914SA_File2.wav Is present in the CSV but not in Music folder.
9915SA_File3.wav Is present in the CSV but not in Music folder.
9916SA_File4.wav Is present in the CSV but not in Music folder.
我希望脚本只输出丢失的文件,如下所示:
9917SA_File5.wav Is present in the Music folder but not in the CSV.
这可能吗?
编辑:
事实证明,我的 CSV 文件实际上在每个“音频文件路径”条目的末尾都有隐藏空格,导致它读起来像一个不同的名称。
新代码
$myFolder = Get-ChildItem '/Users/Me/Documents/Music' | % {$_.Name}
$myCSV = Import-Csv -Path '/Users/Me/Documents/AudioMissing.csv' | % {$_.'Audio File Path' -replace "\\", ""}
$compare = Compare-Object -ReferenceObject $myCSV -DifferenceObject $myFolder -includeequal
Write-Output "`n_____FOLDER_____`n"
$myFolder
Write-Output "`n_____CSV_____`n"
$myCSV
Write-Output "`n_____COMPARE_____`n"
$compare
Write-Output "`n_____Formatted Results____`n"
foreach($y in $compare){
if($y.SideIndicator -eq "=>"){
write-output "$($y.InputObject) Is present in the Music folder but not in the CSV."
}
if($y.SideIndicator -eq "<="){
write-output "$($y.InputObject) Is present in the CSV but not in Music folder."
}
}
if($y.SideIndicator -eq "=="){
write-output "All files present."
}
返回:
_____FOLDER_____
9913SA_File1.wav
9914SA_File2.wav
9915SA_File3.wav
9916SA_File4.wav
9917SA_File5.wav
_____CSV_____
9913SA_File1.wav
9914SA_File2.wav
9915SA_File3.wav
9916SA_File4.wav
_____COMPARE_____
InputObject SideIndicator
----------- -------------
9913SA_File1.wav ==
9914SA_File2.wav ==
9915SA_File3.wav ==
9916SA_File4.wav ==
9917SA_File5.wav =>
_____Formatted Results____
9917SA_File5.wav Is present in the Music folder but not in the CSV.
【问题讨论】:
标签: arrays powershell csv compare directory