Search code examples
batch-filefindstrerrorlevel

FINDSTR not working with ERRORLEVEL in for loop


This is my code right now

@echo off
if exist Files.tmp del /f /q Files.tmp
dir /b /s "D:\User\Desktop\Test\*.*">>Files.tmp
set "tempnum=0"
setlocal enableDelayedExpansion
for /f %%C in ('Find /V /C "" ^< Files.tmp') do set Count=%%C
for /F "tokens=1,* delims=: " %%j in (Files.tmp) do (
set /a "tempnum=!tempnum!+ 1"
cls
echo Scanning "%%j:%%k"
echo       Status: !tempnum! of !count! scanned
echo.
echo Detected;
if exist loggers.txt type loggers.txt
findstr /C:"test" "%%j:%%k"
if errorlevel 0 echo %%j:%%k>>loggers.txt
)
echo Done!
pause

What this script is supposed to do is find any files in the folder that is in the dir command and output any files that have the "test" string in them. When I test it in command prompt it works find and when I type the command it displays the string in the test file and not the others but when I run the batch script it just outputs every file in the directory as a detection

Any one know why?


Solution

  • My comment as a single line answer:

    @FindStr /SIMP "test" "D:\User\Desktop\Test\*.*">"loggers.txt"