Reconcile files to be deleted with list?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Reconcile files to be deleted with list?

#1 Post by noprogrammer » 10 Mar 2019 10:17

I've got a directory with many files (all with the same extension) and just need to keep a part of them, the rest should be deleted.

The files to keep are stored in the variable %Exceptions% e.g. "foo bar baz" (generated by another process).
I'd like to keep this as flexible as possible, i.e. the script might exclude 0...n files of totally n matches.

I found an interesting approach dealing with directories, however in that case the exceptions were hardcoded in the script.
I'm also unsure about the underlying algorithm: I'll get the total of files with e.g.

Code: Select all

For /f "delims=" %%f In ('dir /b *.log')
but how to design the filter logic for the output?

In %Exceptions% each substring represents a file name (foo, bar, baz → foo.log, bar.log, baz.log), separated by whitespace.

Has anyone got an idea?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Reconcile files to be deleted with list?

#2 Post by aGerman » 10 Mar 2019 10:39

Code: Select all

... ('dir /a-d /b *.log^|findstr /l "%Exceptions%"') ...
... maybe. Just have a look at the help of FINDSTR. There are a lot of options that may help you to define the restrictions for the filter.

Steffen

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Reconcile files to be deleted with list?

#3 Post by noprogrammer » 10 Mar 2019 11:45

Basically this works, but it will not only match foo.log, bar.log and baz.log but bar-tender.log or baz-2019.log as well.
Findstr will merely do what supposed to when run like

Code: Select all

... findstr /r /c:"^bar.log$" ...
So I'm not sure if findstr can be used for the filter logic.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Reconcile files to be deleted with list?

#4 Post by aGerman » 10 Mar 2019 11:56

Depends on how the content of variable %Exceptions% exactly looks like. If there is only a single space between your search terms then you can replace them like that:

Code: Select all

"%Exceptions: =.log %"
And if there is no trailing space in the variable then use

Code: Select all

"%Exceptions: =.log %.log"
Also use options /lb instead of only /l.

Steffen

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Reconcile files to be deleted with list?

#5 Post by Compo » 10 Mar 2019 20:03

Similarly, but using /X instead of /B and/or /E, as long as your filenames do not themselves contain any space, (or poison) characters:

Code: Select all

FindStr /ILVX "%Exceptions: =.log %.log"
Or alternatively…

Code: Select all

FindStr /XVIC:"%Exceptions: =.log" /C"%.log"

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Reconcile files to be deleted with list?

#6 Post by noprogrammer » 11 Mar 2019 17:38

(Not sure if I understood the previous approach.)
Anyway, here's the actual data. First off, I run the line

Code: Select all

<"%ProgramFiles%\Firefox\search.json" jq -r ".visibleDefaultEngines|join(\",\")"
and save its output to a variable which looks like:

Code: Select all

google-2018 bing amazon-en-GB chambers-en-GB ddg ebay-uk twitter wikipedia
or

Code: Select all

google amazondotcom-de bing ddg ebay-de ecosia leo_ende_de wikipedia-de
They're Mozilla search engines with xml extension, i.e. the files are: google-2018.xml, amazon-en-Gb.xml, leo_ende_de.xml etc. Only a fraction of them is supposed to be kept, the rest should be deleted.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Reconcile files to be deleted with list?

#7 Post by aGerman » 12 Mar 2019 05:41

Okay but what is your problem? That you have to replace extension .log with extension .xml in the code?

Steffen

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Reconcile files to be deleted with list?

#8 Post by noprogrammer » 12 Mar 2019 09:43

Thanks Steffen, yes, it does work. I've got both cases for "use" and "remove" now:

Code: Select all

For /f "delims=" %%f In ('dir /b *.xml^|findstr /lb "%Exceptions: =.xml %"') Do (...)
For /f "delims=" %%f In ('dir /b *.xml^|findstr /lv "%Exceptions: =.xml %"') Do (...)
So far, it seems to work fine.

Post Reply