0

I've researched this and haven't been able to come up with a solid solution. Basically, I have a separate hard drive containing thousands of music files. I have a CSV list with the names of all the files that should be in the hard drive. Example:

My List

I want to be able to test if each of the files on my list exist in the hard drive, and if not, export it to a separate "missing files" list. The thing is each of the files in the hard drive exist under multiple folders.

As my script is now, I am trying to test if the path exists by using join-path. Here is my code right now - it's returning all of the files in the directory instead of just the missing files:

$documents = 'C:\Users\Me\Documents\ScriptTest'
$CSVexport = 'C:\Users\Me\Documents\ScriptTest\TestResults.csv'

$obj = @()
    Write-host "`n_____Results____`n" #Write the results and export to a CSV file
    $NewCsv = Import-CSV -Path 'C:\Users\Me\Documents\ScriptTest\Test.csv' |
        Select-Object ID,'File Path' |  
            ForEach-Object {
                if (!(Test-Path (Join-Path $documents $_.'File Path'))){
                    write-host "`n$($_.'File Path') is missing from the folder.`n"
                    $ObjectProperties = @{
                        ID = $_.ID
                        'File Path' = $_.'File Path'
                    }
                $obj += New-Object PSObject -Property $ObjectProperties
                }
            }
            $obj | export-csv $CSVexport -NoTypeInformation

How do I account for the sub-directories that vary with each file?


Edit - Resolved

$myFolder = 'C:\Users\Me\Documents\ScriptTest'
$CSVexport = 'C:\Users\Me\Documents\ScriptTest\Results.csv'
$csvPath = 'C:\Users\Me\Documents\ScriptTest\Test.csv'

$FileList = Get-ChildItem $myFolder -Recurse *.wav | Select-Object -ExpandProperty Name -Unique
Import-CSV -Path $csvPath | 
    Where-Object {$FileList -notcontains $_.'File Path'} |
    export-csv $CSVexport -NoTypeInformation
4

1 回答 1

0

您可以从递归文件夹生成文件名列表,然后检查文件是否在该列表中。

$documents = 'C:\Users\Me\Documents\ScriptTest'
$CSVexport = 'C:\Users\Me\Documents\ScriptTest\TestResults.csv'

$FileList = Get-ChildItem $documents -Recurse |
    Where-Object { -not $_.PSIsContainer } |
    Select-Object -ExpandProperty Name -Unique
Import-CSV -Path 'C:\Users\Me\Documents\ScriptTest\Test.csv' | 
    Where-Object {$FileList -notcontains $_.File} |
    Export-CSV $CSVexport -NoTypeInformation

编辑:根据 Bruce Payette 和 mklement0 的建议更新答案以使用 PowerShell 2.0

于 2018-06-20T22:05:43.527 回答