简体   繁体   中英

Batch script to read specific word from a text file and write to another file

I need some help on a batch script which has to achieve the following :

  1. Placed into the root structure,with # folders
  2. Open each subfolder, then read out a specific line from a text file (name of file varies).
  3. Create another text file to show "OK" or "NOT OK" based on the presence of the previously searched line.

Thanks in advance to anyone who can give me a hand.

I guess, it would be something like this:

  • It searches for *.txt files in every folder.
  • Read each file for string "This is my line"
  • if found, it would write "filename.txt" is OK in D:\\FileStatus.txt
  • if not found, it would write "filename.txt" is Not OK in D:\\FileStatus.txt

@echo off

Set DirToSearch="D:\MyFolders\"
Set LineToRead="This is my line"

pushd %DirToSearch%
for /r %%f in (*.txt) do (
    For /F %%l in ('Findstr /L /I %LineToRead% "%%f"') do (
     if %%l equ " " (
        echo File:"%%f" is Not OK >> D:\FileStatus.txt
     ) else (
        echo Line: %%l
        echo File:"%%f" is OK >> D:\FileStatus.txt
     )
)
)

Goto :End

:End
popd

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM