Tuesday, September 13, 2011

CMD exercise : filtering for files that don't have a keyword

Problem:

Need to find the keyword "ORDER, " in a large folder of txt files. Need to move all files that don't have this keyword to another folder.

How do I count the number string instances for all files in a directory?

Here is an example of looking for the string "ORDER, " in all txt files.

find /C "ORDER, " *.txt

How do I filter for items with a count of zero?

find /C "ORDER, " *.txt | findstr /c:": 0"

This is because each line in the output of find comes up:

---------- stats-2011-09-13.TXT: 0
---------- stats-2011-09-12.TXT: 12

What if I want to move all files from a list to some folder?

First output your results by doing redirection.

find /C "ORDER, " *.txt | findstr /c:": 0" > list.txt

You can open the file and strip out everything except the filename and extension. Once that's done, just use a for loop to move everything in list.txt.

for /f %a in ('type list.txt') do move %a no_orders