Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Create a 'Missing' File(s) List

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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'
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading