0

I'm trying to remove all the images which are not listed in the CSV file. Data comes to CSV in the format described below.

Problem is that script is going to delete every file in the images folder. I only want to delete images that do not contain SKU (example "7-5468-XXX7") from the CSV file. What to do here?

Trying to use this:

$filestokeep = Import-Csv "C:\Users\example\filestokeep.csv"
    $files = Get-ChildItem  "C:\Users\example\images\"

    $files |
        Where-Object Name -notin $filestokeep.sku |
        Remove-Item -WhatIf

filestokeep.csv looks like:

"sku"
"1-123-X"
"2-123-XXXXX"
"3-562-XXXX"
"4-215-XXXXXX"
"5-56482-XX-X"
"6-45688"
"7-5468-XXX"
"856-54648-X"

C:\Users\example\images\ containing files such as:

1-123-X.jpg
2-123-XXXXX.jpg
1-123-X_1.jpg
1-123-X_2.jpg
7-5468-XXX7.jpg
7-5468-XXX7_1.jpg
7-5468-XXX7_2.jpg
6-45688.jpg

Images folder should look like this after running:

1-123-X.jpg
2-123-XXXXX.jpg
1-123-X_1.jpg
1-123-X_2.jpg
6-45688.jpg

Link to original code that I am trying to implement: https://www.reddit.com/r/PowerShell/comments/8f2z0l/remove_files_not_in_csv/

Pekka
  • 5
  • 3
  • 2
    `Where-Object Name -notin $filestokeep.sku` --> `Where-Object { $_.BaseName -notin $filestokeep.sku }`. Also, for safety add switch `-File` to the Get-ChildItem cmdlet so you're not processing subfolders. Anyway, your desired output does not match the description because it also shows files that are **IN** the csv.. – Theo Jun 16 '20 at 15:02
  • Thank you for your answer. Adding `-File` sure do makes everything more secure. – Pekka Jun 17 '20 at 07:43

1 Answers1

0

Your FilesToKeep.csv file contains lists of FileNames like 1-123-X.

You're comparing this to the Name property which includes the FileExtension, so you're comparing your list of just file names against something like \1-123-X_1.jpg which includes the extension.

To be clear the comparison looks like this:

Name             Sku
1-123-X_1.jpg    1-123-X_1  #doesn't match because Windows cares about the .jpg piece

See why this isn't working? The extension is in one group and not the other!

The fix is very easy, just compare against the BaseName property instead, like this:

 $files |
        Where-Object BaseName -notin $filestokeep.sku |
        Remove-Item -WhatIf

That will result in this comparison happening instead:

BaseName     Sku
1-123-X_1    1-123-X_1  #MATCHES!

And when you then go ahead and run the full script, you'll see an output of this, showing what would happen if you removed -WhatIf.

What if: Performing the operation "Remove File" on target "C:\temp\Files\1-123-X_1.jpg".
What if: Performing the operation "Remove File" on target "C:\temp\Files\1-123-X_2.jpg".
What if: Performing the operation "Remove File" on target "C:\temp\Files\7-5468-XXX7.jpg".
What if: Performing the operation "Remove File" on target "C:\temp\Files\7-5468-XXX7_1.jpg".
What if: Performing the operation "Remove File" on target "C:\temp\Files\7-5468-XXX7_2.jpg".

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • Basename helped a lot, but I also want keep images which include the sku. For examaple if sku is "1-123-X" files with names like "1-123-X_1.jpg" and "1-123-X_2.jpg" should not be deleted. Can you somehow add wildcards around the sku? Thank you for great info so far. – Pekka Jun 17 '20 at 07:41
  • Ask that as a new question, it's possible but would required a rewrite of your logic entirely and doesn't fit this question. – FoxDeploy Jun 17 '20 at 18:44
  • New question right here: https://stackoverflow.com/questions/62445840/how-to-delete-all-the-files-from-folder-where-name-does-not-include-string-liste – Pekka Jun 18 '20 at 09:58
  • Remember to vote up, and accept as answer if this was helpful to getting the problem solved :) – FoxDeploy Jun 18 '20 at 13:05