Powershell使用CSV列表检查文件夹中是否有丢失的文件

时间:2018-05-10 08:46:17

标签: arrays powershell csv compare directory

我对脚本有点陌生,请原谅我这里有任何明显的答案。

基本上,我有一个存储在每个月更新的文件夹中的大量音乐文件。我创建了一个较小的文件夹用于测试目的:

music folder

我有一个CSV列表,其中包含该文件夹中应包含的所有文件:

CSV File with missing entry

我正在尝试创建一个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.

1 个答案:

答案 0 :(得分:0)

我使用以下内容创建了一个csv文件:

ID  Audio File Path
1   99\13\SA_File1.docx
2   99\13\SA_File2.docx
3   99\13\SA_File3.docx
5   99\13\SA_File5.docx

注意:我从列表中删除了99\13\SA_File4.docx

然后我运行了以下脚本:

$myCSV = Import-Csv -Path '/Users/me/myFiles.csv' | % {$_.'Audio File Path' -replace "\\", ""}
$myFolder = Get-ChildItem '/Users/me/test'

$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 CSV."
    }
    if($y.SideIndicator -eq "<="){
     write-output "$($y.InputObject) Is present in the CSV but not in the Music folder."
    }
 }

我已经测试过从文件夹中删除文件并将该文件包含在CSV中,并且脚本符合要求。

比较对象时不需要-passthru标志,Get-ChildItem适用于此应用程序。

修改

您的搜索结果相当奇怪,请参阅下面的脚本(我已将文件名更新为更像您的文件名):

$myFolder = Get-ChildItem '/Users/me/test'
$myCSV = Import-Csv -Path '/Users/me/myFiles.csv' | % {$_.'Audio File Path' -replace "\\", ""}
$compare = Compare-Object -ReferenceObject $myCSV -DifferenceObject $myFolder

Write-Output "`n_____FOLDER_____`n"
$myFolder.Name

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."
    }
 }

这给了我以下输出:

Output from script 你能不能运行上面的脚本并发送输出?