imecmario
imecmario

Reputation: 11

Problem with variables and if statement within a for loop in a batch script

been working in this batch code for a couple of days and I cannot make it work right. To give you a perspective, this code is intended to read a file that contains some "selected" numbers which refer to other files contained in another folder. These files contain a lot of data arranged by rows and columns. The first rows are not important so the code is skipping 50 rows to save time, then I am interested in the 2nd and 7th columns so these are exported to variables (A,B) for each row. If there is a combination of values between these two variables (e.g. A>1000 & B>0.03) on the same row the code is supposed to export the file number where this happened. Problem: the IFs that compare these columns don't work as expected, the GTR/GEQ don't seem to respect the "more than" and the code exports file numbers even when there is no such combination in the file. I have tried two codes so far:

Code 1

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%s in (selected.txt) do (
cd %~dp0\Melts\MeltNPTdis-8-%%s
    for /f "skip=50 tokens=2,7 delims= " %%A in (file.txt) do (
        if %%A GTR 1000 if %%B GTR 0.03 (
            cd %~dp0
            >>dummy.txt echo %%s
)))

Code 2, with check variables

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%s in (selected.txt) do (
cd %~dp0\Melts\MeltNPTdis-8-%%s
    for /f "skip=50 tokens=2,7 delims= " %%A in (file.txt) do (
        set /a x=0
        set /a y=0
        if %%A GTR 999 (
            set /a x=1
            )
        if %%B GTR 0.03 (
            set /a y=1
            )
        set /a z= !x! + !y!
        if !z! EQU 2    (
        cd %~dp0
        >>dummy.txt echo %%s
)))

Can someone light me up on this one? I have isolated the problem to the IFs and have tried to define the variables differently like %%A or %%A%% not solving the issue. Thanks a lot!

Upvotes: 1

Views: 44

Answers (1)

user7818749
user7818749

Reputation:

You would need to test both sides of the fractional numbers as cmd can only work with 32bit integers. You could however get a little help from powershell to overcome this. Note this is untested as I am on my mobile device and obviously do not have the environment you have to test:

@echo off
for /f "usebackq delims=" %%s in ("selected.txt") do (
(for /f "usebackq skip=50 tokens=2,7 delims= " %%A in ("%~dp0Melts\MeltNPTdis-8-%%s\file.txt") do powershell "if (%%A -gt 0.04) {if (%%A -gt 2) {Write-host "%%s"}}"
   )>"%~dp0dummy.txt"
)

Upvotes: 1

Related Questions