Page 1 of 1
Reconcile files to be deleted with list?
Posted: 10 Mar 2019 10:17
by noprogrammer
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?
Re: Reconcile files to be deleted with list?
Posted: 10 Mar 2019 10:39
by aGerman
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
Re: Reconcile files to be deleted with list?
Posted: 10 Mar 2019 11:45
by noprogrammer
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
So I'm not sure if findstr can be used for the filter logic.
Re: Reconcile files to be deleted with list?
Posted: 10 Mar 2019 11:56
by aGerman
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:
And if there is no trailing space in the variable then use
Also use options /lb instead of only /l.
Steffen
Re: Reconcile files to be deleted with list?
Posted: 10 Mar 2019 20:03
by Compo
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"
Re: Reconcile files to be deleted with list?
Posted: 11 Mar 2019 17:38
by noprogrammer
(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.
Re: Reconcile files to be deleted with list?
Posted: 12 Mar 2019 05:41
by aGerman
Okay but what is your problem? That you have to replace extension .log with extension .xml in the code?
Steffen
Re: Reconcile files to be deleted with list?
Posted: 12 Mar 2019 09:43
by noprogrammer
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.