Home > database >  How to Create a 'Missing' File(s) List
How to Create a 'Missing' File(s) List

Time:03-29

I am trying to compile a list of missing files between two folder/directories which have text/image files with the same basename.

    # file directory/folder with .txt files
    $filesPathText = "C:\test\test3"                                  
    
    # file directory/folder with .JPG files 
    $filesPathImage = "C:\test\test4"

Content:

Copy of 0002.txt
Copy of 0003.txt
Copy of 0004.txt
Copy of 0006.txt

Copy of 0002.jpg
Copy of 0003.jpg
Copy of 0004.jpg
Copy of 0005.jpg
Copy of 0006.jpg

I want to output that the 'missing' file is: Copy of 0005.txt

I have tried this kind of thing:

$texts = Get-ChildItem -Path $filesPathText
$images = Get-ChildItem -Path $filesPathImage

$result = $images | Where-Object{$texts -notcontains $images}
$result

To me the logic reads correctly but the result is an output of all the image files.

Even though this is a simple example and would seem common usage I have not been able to find a similar question that has been answered.

Any suggestions would be welcome.

CodePudding user response:

Your Where-Object script block is comparing 1 array of objects ($texts) against the other array of object ($images) instead of comparing each object ($_) against an array of objects. You're also not referencing the Property (.BaseName) you want to compare.

$texts = Get-ChildItem -Path $filesPathText
$images = Get-ChildItem -Path $filesPathImage

# missing text files
$images | Where-Object { $texts.BaseName -notcontains $_.BaseName } | ForEach-Object {
    $_.BaseName   '.txt'
}

# missing images
$texts | Where-Object { $images.BaseName -notcontains $_.BaseName } | ForEach-Object {
    $_.BaseName   '.jpg'
}
  • Related