Search code examples
emailcmdextractfindstr

Extract email adresses from several files with Windows CMD


I have around 1000 txt.files containing a lot of data like email adresses, name, Zip code, services ... On this folder : C:\Users\dupont\Documents\test I would like to extract all the email adresses and only email adresses on this folder and copy it it another .txt file.

So I created a .bat like that to extract email adresses :

findstr @ C:\Users\dupont\Documents\test\ad.txt >C:\Users\dupont\Documents\test\results.txt
pause

The problem is that, it extracts all the line Moreover, I do not know how to ask on the command, to delimit the search of the string with '@' to a space, tab, carriage return Indeed, the command has to match with @, the word before @ (until space) and the word after @( until the space

Thanks for your help Have a nice day


Solution

  • For each textfile (%%f) process each line (%%a) one after the other. Split the line into elements (%%b) by spaces (Standard delimters are space and TAB) and if the element contains a @, print it:

    @echo off
    for %%f in (*.txt) do (
      for /f "tokens=*" %%a in (%%f) do (
        for %%b in (%%a) do (
          echo %%b|find "@"
        )
      ) 
    )