0

I have studied this: Is it possible for a batch file to delete a file if not found in list from text file?

In my case there is a list of words (one in each line) in a text file e.g:

ext
abcd

in which there are extensions of the files (like ext above) or fragments of filenames' bases (like abcd above).

So, I don't want files like *abcd* or *ext* to be deleted (where * is for any number of any allowed characters) - and I do want to delete any other files from the directory.

I took this code:

for /f "eol=: delims=" %%F in ('dir /b /a-d "SomeFolder" ^| findstr /vibg:"ExcludeFile"') do del "SomeFolder\%%F"

and what I did to it - I deleted /b option of findstr (so now it is ...findstr /vig... - so as I think findstr shoud try to match strings from the list (.txt file) to any part of the 8.3 filenames in directory. But it does not. It does not exclude files witch matching extensions from deleting, (it deletes for instance antything.ext despite the string ext is in the list). Can you help please?

PS Thanks for trailing spaces, it helped - is it possible to use an implication of two conditions e.g a file would be deleted if:

  1. a part of filename is not on exclude list
  2. and a file has a specific extension (let's say .bmp).

In above example the file 123.xyz would be deleted because of the first condition (not a part of it is abcd nor ext), but would not be actually because of the second condition (it's extension is not .bmp)?

maccam
  • 3
  • 3
  • 1
    check if there are trailing spaces in the textfile. – Stephan Mar 28 '21 at 14:22
  • You may also want to get a really in depth explanation of `FINDSTR` by reading [What are the undocumented features and limitations of the Windows FINDSTR command?](https://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman) – Squashman Mar 28 '21 at 15:26
  • I think your best bet would be to make all of the search strings in the search file as regular expressions and then get rid of the `/B` option as one of the arguments. – Squashman Mar 28 '21 at 15:32
  • You would want the search strings in your search file to be `.*abcd.*` and `.*\.ext$` – Squashman Mar 28 '21 at 16:20
  • Thank you Stephan - there were railing spaces ideed. Problem solved by now. – maccam Mar 29 '21 at 06:55

1 Answers1

0

Use the following in your Exclude File.

.*abcd.*
.*\.ext$

Remove the /B option from the FINDSTR command.

for /f "eol=: delims=" %%F in ('dir /b /a-d "SomeFolder" ^| findstr /vig:"ExcludeFile"') do del "SomeFolder\%%F"

I would read the help for the FINDSTR command so that you understand what the regular expressions are doing in your exclude list. Here is brief list of the options I used.

.        Wildcard: any character
*        Repeat: zero or more occurrences of previous character or class
$        Line position: end of line
\x       Escape: literal use of metacharacter x
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Thank, although the problem was because of trailing spaces, I will try this. – maccam Mar 29 '21 at 06:57
  • @maccam however unlikely it might be you could have a file name with one of your file extensions as part of the file name. So just using your example you could have `Court_Text.pdf` would match your exclude list and not get deleted. – Squashman Mar 29 '21 at 12:44
  • Yes, thanks, I do understand this problem and I will implement your advice, although the data format is restricted to maybe a dozen of possible filenames (except some of them are YYYYMMDD or HHMMSS format generated everyday) and maybe 5 or 7 extensions. But I will do it just to sleep easy. – maccam Mar 29 '21 at 13:07
  • well, this works brilliant. Thanks again. – maccam Mar 30 '21 at 05:30
  • @maccam: please help to keep this site clean by [removing your question from the "unanswered" queue](https://stackoverflow.com/help/someone-answers) by accepting the /an answer (if it satisfies you). – Stephan Mar 30 '21 at 18:19