0

I have a txt file that contains about 500 values, one per line. I need to check to see of any of those 500 values appear in any of 6 csv files each containing 100k lines. I can search for one value in those 6 csv files using

 for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"

but how do I do multiple searches automatically via command-line or batch file (CaSe SenSiTIve)?

2
  • Have you looked into batch scripts? You will probably have better luck there. Commented Feb 12, 2014 at 1:59
  • install a unix work-alike system. fgrep -lf srchFile file1 file* .. will list all files that contain at least one word in srchFile. If you take out the l and just leave -f, then the search will print the filename and the complete line that matches the word in your listFile. Good luck. Commented Feb 12, 2014 at 2:58

3 Answers 3

1
@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=C:\destdir"
for /f "delims=" %%a in ('dir /s /b "%sourcedir%\*.csv"') do (
  FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt"
GOTO :EOF

What you are asking is rather unclear. I used c:\sourcedir as the location of the .csv files and c:\destdir as the location for the reports. Replacing FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt with your original (with the double > would accumulate the lines into a single file - if that's what you want. As it stands, a new file will be created with name the same as your .csv+found.txt

Sign up to request clarification or add additional context in comments.

2 Comments

This is great! I would like to use the >> to append all results to one file, but is there any way to also print the name of the file it was found in, not just the line #? The /M flag for FINDSTR won't allow both.
How about a line echo(===== file %%~fa ===== just before the findstr line? That should give you an obvious separator. You could change the ~f to ~dpnx in this line to show (your selection of) drive, path, name and e xtension. Don't want a part of the name? omit the letter.
0

An easy way is to use a batch script. You can loop through each of the files one by one. If you want to do them all at once you need to thread your program.

for /L %%A in (1,1,6) do (
     Your code goes here
)

That batch script will loop six times. I am not really sure how you specify the file but if you loop through each file it will work.

So put your current batch script where I said "Your code goes here"

for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"

But you need to edit it to point to the file that you want to search. If your files are 1.txt, 2.txt 3.txt then all you need to do is set your file name to the current loop iteration number.

Comments

0

I've been using variants of this shell function for years:

ematch () {
    for f in $(find . -type f | grep -v '~' | grep -v \.svn\/) ; do 
    egrep "$1" "$f" /dev/null 2> /dev/null
    done
}

-> ematch "(string1|string2|string3)"

Feel free to adapt to your needs and post your mods here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.