for-loopbatch-filenotepad

Bat file that copy text.files after applying 2 filters


I'm a biologist, with no coding knowledge, trying to create a script that reads every *rprt.txt file in a folder.

In line 11 of each file, the fifth word is a number, If that number is 6000<number<14000 then I want to read the fifth word in line 13 and if that number is greater than 600. Copy the file into another folder in that directory.

At this point I've tried a lot of things. I know the next code is exiting the loop but is the best I got.

@echo off
for %%f in (*rprt.txt) do set "name=%%f" &goto first

:first
for /F "skip=10 tokens=5" %%i in (%name%) do set "var1=%%i" &goto nextline

:nextline
for /F "skip=12 tokens=5" %%i in (%name%) do set "var2=%%i" &goto nextline2

:nextline2
if %var1% geq 6000 (if %var2% geq 600 echo.%name% >> valid.txt)

I've also tried this to test the for loop but I don't understand what's wrong. This prints "echo is off" 3 times

@echo off
for %%f in (*rprt.txt) do (set "name=%%f" & echo %name% >> valid.txt)

Solution

  • @ECHO OFF
    SETLOCAL
    rem The following settings for the directories and filenames are names
    rem that I use for testing and deliberately includes spaces to make sure
    rem that the process works using such names. These will need to be changed to suit your situation.
    
    SET "sourcedir=u:\your files"
    SET "destdir=u:\your results"
    
    FOR %%e IN ("%sourcedir%\*rprt.txt") DO (
     rem %%e has filename
     SET "line11="
     FOR /f "usebackqskip=10tokens=5" %%y IN ("%%e") DO IF NOT DEFINED line11 (
      SET "line11=y"
      SET "line13="
      FOR /f "usebackqskip=12tokens=5" %%o IN ("%%e") DO IF NOT DEFINED line13 (
       SET "line13=y"
       IF %%y gtr 6000 IF %%y lss 14000 IF %%o gtr 600 ECHO COPY "%%e" "%destdir%"
      )
     )
    )
    
    GOTO :EOF
    

    Always verify against a test directory before applying to real data.

    Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around "%%e" can be omitted.

    I'm assuming that the values in token 5 of the two lines are guaranteed numeric.