-1

I would remove all .jpg files in a directory, except those specified in a text file (eg test.txt).
This text file contains a list of some thousands of file names that exactly match those that I want to keep.
It is also essential that the only file names that include the symbol ^ be included for removal.
So in other words, all files without caret should not even be considered for removal by this script.

Below is the example of what is in the text file.

1022SS^BLACK.jpg
1022SS^BROWN.jpg
1022SS^DK GREEN.jpg
1022SS^GREY.jpg
1022SS^MAROON.jpg
1022SS^NAVY.jpg

Desired behavior:

KEEP_GREY.jpg    
KEEP_BLACK.jpg
KEEP_1022SS^BLACK.jpg
KEEP_1022SS^BROWN.jpg
KEEP_1022SS^DK GREEN.jpg
KEEP_1022SS^GREY.jpg
KEEP_1022SS^MAROON.jpg
KEEP_1022SS^NAVY.jpg
DELETE_SS^BLACK.jpg
DELETE_SS^BROWN.jpg
DELETE_SS^DK GREEN.jpg
DELETE_SS^GREY.jpg
DELETE_SS^MAROON.jpg
DELETE_SS^NAVY.jpg

Need to keep the GREY.jpg and the BLACK.jpg because there is NO carrot symbol in the file name.

I found the following on this site that does exactly the opposite. But it might be a good start for someone who knows how to do this: How to delete files from a folder using a list of file names in windows?

JadonR
  • 1
  • 1
  • Unfortunately, we are not a code-writing service. Instead of simply asking for code to perform a particular task, please show us what you've tried so far (including any code you currently have) and where you're stuck so that we can help you with your specific problem. Questions that only ask for code are too broad and are likely to be [put on hold or closed](http://superuser.com/help/closed-questions). – bwDraco Sep 20 '15 at 22:37

1 Answers1

2

Think out next one-liner example:

cmd /E /V /C for /F "tokens=*" %G in ('dir "SU\Files In\972965\*.jpg" /B /S') ^
do @set "xx=%~nxG" ^& if "!xx:^=€!" NEQ "!xx!" (^
findstr /I /C:"!xx!" "SU\Files In\972965.txt" ^
 ^>NUL ^&^& echo retain "%~fG" ^|^| echo delete "%~fG" ) else echo RETAIN "%~fG"

INPUT (directory and exception file):

==>dir "D:\bat\SU\Files In\972965\" /B /S
D:\bat\SU\Files In\972965\1022SS-YELLOW.jpg
D:\bat\SU\Files In\972965\1022SS^BLACK.jpg
D:\bat\SU\Files In\972965\1022SS^BROWN.jpg
D:\bat\SU\Files In\972965\1022SS^DK GREEN.jpg
D:\bat\SU\Files In\972965\1022SS^GREY.jpg
D:\bat\SU\Files In\972965\1022SS^MAROON.jpg
D:\bat\SU\Files In\972965\1022SS^NAVY.jpg
D:\bat\SU\Files In\972965\1022SS_BLUE.jpg

==>type "D:\bat\SU\Files In\972965.txt"
1022SS^BROWN.jpg
1022SS^DK GREEN.jpg
1022SS^NAVY.jpg
1022SS^PINK.jpg

Output:

==>cmd /E /V /C for /F "tokens=*" %G in ('dir "SU\Files In\972965\*.jpg" /B /S') do  @set
"xx=%~nxG" ^& if "!xx:^=€!" NEQ "!xx!" (findstr /I /C:"!xx!" "SU\Files In\972965.txt" ^>NU
L ^&^& echo retain "%~fG" ^|^| echo delete "%~fG" ) else echo RETAIN "%~fG"
RETAIN "d:\bat\SU\Files In\972965\1022SS-YELLOW.jpg"
delete "d:\bat\SU\Files In\972965\1022SS^BLACK.jpg"
retain "d:\bat\SU\Files In\972965\1022SS^BROWN.jpg"
retain "d:\bat\SU\Files In\972965\1022SS^DK GREEN.jpg"
delete "d:\bat\SU\Files In\972965\1022SS^GREY.jpg"
delete "d:\bat\SU\Files In\972965\1022SS^MAROON.jpg"
retain "d:\bat\SU\Files In\972965\1022SS^NAVY.jpg"
RETAIN "d:\bat\SU\Files In\972965\1022SS_BLUE.jpg"

==>

Resources (required reading):

JosefZ
  • 12,837
  • 5
  • 37
  • 69
  • +1. I think there is a mistake in the else statement, it should be `echo DELETE` instead of `echo RETAIN` – Paul Sep 16 '15 at 23:02
  • @Paul it's possible we both don't comprehend this OP's statement the same way: _all files without caret should not even be considered for removal_... See my profile: still learning English:) – JosefZ Sep 17 '15 at 16:08
  • Yes, one of the last sentence is in contradiction with his first. But if you read "I found... that does exactly the opposite." you will understand that seeking an exclusion list instead of an inclusion list. – Paul Sep 17 '15 at 18:18
  • @Paul in that case, all the `if "!xx:^=€!" NEQ "!xx!" ...` test seems to be superabundant as `... echo delete "%~fG" ) else echo DELETE "%~fG"` would give the same (case insensitive) result. [Q.E.D.](https://en.wikipedia.org/wiki/Q.E.D.) – JosefZ Sep 17 '15 at 18:31
  • To be honest I did not understand the `if "!xx:^=€!" NEQ "!xx!"` I am still learning too :) – Paul Sep 17 '15 at 18:52