Batch script to search for a string and return the result

Zorc -  
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   -
Hello,

I am facing a problem with my batch script.
Here is the process of the script that I would like to set up:

For all the files in c:\dossier\*.txt, search for the string "toto". If the file contains this string "toto", return the path of this file in c:\resultat.txt

Here is what I have tried but does not work

FOR /F "tokens=1,2* delims=: " %%A IN ('FIND /C "toto" c:\*.txt) DO (
IF %%A GTR 0 ( %%B >>resultat.txt )
)

Thank you for your help

Configuration: Windows 7 / Internet Explorer 9.0

3 answers

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Hello,

    You're not looking in the right folder:

    FIND /C "toto" c:\*.txt
    0
    1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      And I think there is an easier way to do it:

      FOR %%A IN (FIND "toto" c:\folder\*.txt) DO (%%A >> c:\result.txt)
      0
  2. TAC
     
    Et can we replace "toto" with a previously declared variable?

    SET VARIABLE=toto

    FOR /F "tokens=1,2* delims=: " %%A IN ('FIND /C "%VARIABLE%" c:\*.txt) DO (
    IF %%A GTR 0 ( %%B >>resultat.txt ) )

    Thank you
    0
    1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      Yes, it works with a variable.
      0
  3. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    hello
    for /f "tokens=1-3 delims=: " %%A in ('find /c "toto" C:\dossier\*.txt ') do if %%C NEQ 0 echo %%B >> c:\resultat.txt 
    0