Home > Net >  Searching for keyword in file content located in a folder
Searching for keyword in file content located in a folder

Time:09-16

I am trying to produce a batch script that will locate some text inside of a .txt file. The folder contains hundreds of these txt files and need a script to split them. If the file contains the phrase Initialisation Failed then I would like to store that .txt in another folder. If not then just leave it in the current folder.

Below I have an attempt that I used, but this would only work on a single file.

findstr/mc:"Initialisation Failed"
for /f "delims=" %%a in ('findstr /mc:"Initialisation Failed" *.txt') do move "%%a" "T:\Jack Lythgoe\Production Test\InitialisaionError_Review\batch test\error"

Expected Output - error (folder)

log223.txt
log24324.txt
log546.txt
log234.txt

Once the script has run, the files containing Initialisation failed should be transferred into error folder which is in that directory.

Error output from cmd

for /F "delims=" %a in ('findstr /mc:"Initialisation Failed" *.txt') do move "%a" "T:\Jack Lythgoe\Production Test\InitialisaionError_Review\batch test\error"
FINDSTR: Cannot open *.txt

CodePudding user response:

The code @Stephan told me to use was to complicated. I had now just used my brain a bit and figured it out myself. This code block will search within the files of the current directory and subdirectories and find the exact match of the string in the .log files, and store the name of the file in results.csv. If any errors it will say no match found

@echo off
findstr /s /m /c:"INITIALISATION FAILED-32775" *.log > results.csv
if %errorlevel%==0 (
echo Found! logged files into results.txt
) else (
echo No matches found
)

  • Related